This C# program demonstrates how to read data with an SQLReader in a database for Microsoft SQL in C#.
using System; using System.Data.SqlClient; namespace ReadingWithAnSqlDataReader { class Program { static void Main(string[] args) { Console.Write(ReadTableData("apostles")); } public static string ReadTableData(string sTable) { // The using block ensures that the connection is closed when it exits this block. using (SqlConnection qConnection = new SqlConnection( @"Server=localhost\SQLEXPRESS01;Database=catholic;Trusted_Connection=True;")) { qConnection.Open(); string sTableEntries = ""; // The selection command using (SqlCommand qCommand = new SqlCommand("SELECT name FROM apostles", qConnection)) { using (SqlDataReader qReader = qCommand.ExecuteReader()) { // Read the next line, while available while (qReader.Read()) { // The count is the number of elements in each line, one in this case. object[] qaValues = new object[qReader.FieldCount]; qReader.GetValues(qaValues); // Run through the elements in each line, only one in this case foreach (object qData in qaValues) { sTableEntries += String.Format("{0,-20}", qData); } sTableEntries += "\n"; } } } return sTableEntries; } } } }
John Andrew Peter Press any key to continue . . .
© 20072024 XoaX.net LLC. All rights reserved.