This C# program demonstrates how to read a single row of an SQL table in a database for Microsoft SQL in C#.
using System;
using System.Data.SqlClient;
namespace ReadASingleRow {
class Program {
static void Main(string[] args) {
String sConnectionString = @"Server=LAPTOP-FHJLCP14\SQLEXPRESS;Trusted_Connection=True;Database=XoaX;";
string sSelectString = "SELECT [First],[Second],[Third],[Fourth] FROM [XoaX].[dbo].[TestScores];";
using (SqlConnection qConnection = new SqlConnection(sConnectionString)) {
SqlCommand qSqlCommand = new SqlCommand(sSelectString, qConnection);
qConnection.Open();
SqlDataReader qSqlReader = qSqlCommand.ExecuteReader();
// Read a single row, if it exists
if (qSqlReader.Read()) {
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}", qSqlReader[0], qSqlReader[1], qSqlReader[2], qSqlReader[3]));
}
qSqlReader.Close();
}
}
}
}
45.29, 87.41, 92.59, 95.17 Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.