SQL C#

Read a Single Row

This C# program demonstrates how to read a single row of an SQL table in a database for Microsoft SQL in C#.

Program.cs

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();
            }
        }
    }
}
 

Output

45.29, 87.41, 92.59, 95.17
Press any key to continue . . .
 

Tables

Tables
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.