This C# program demonstrates how to read all of the data in an SQL table in a database for Microsoft SQL in C#.
using System;
using System.Data.SqlClient;
namespace ReadTableRows {
class Program {
static void Main(string[] args) {
Console.WriteLine(ReadTableRows("apostles"));
}
public static string ReadTableRows(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 = "";
using (SqlCommand qCommand = new SqlCommand("SELECT * FROM apostles", qConnection)) {
using (SqlDataReader qReader = qCommand.ExecuteReader()) {
while (qReader.Read()) {
object[] qaValues = new object[qReader.FieldCount];
qReader.GetValues(qaValues);
foreach (object qData in qaValues) {
sTableEntries += String.Format("{0,-20}", qData) + " ";
}
sTableEntries += "\n";
}
}
}
return sTableEntries;
}
}
}
}
John Chalice Andrew Transverse Cross Peter Keys Press any key to continue . . .
© 20072024 XoaX.net LLC. All rights reserved.