This C# program demonstrates how to list all SQL table names in a database for Microsoft SQL in C#.
using System;
using System.Data.SqlClient;
namespace ListTableNames {
class Program {
static void Main(string[] args) {
// 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 sTableList = "";
using (SqlCommand qCommand = new SqlCommand(
"SELECT * FROM information_schema.tables", qConnection)) {
using (SqlDataReader qReader = qCommand.ExecuteReader()) {
// Each row holds the properties of a table.
while (qReader.Read()) {
object[] qValues = new object[qReader.FieldCount];
qReader.GetValues(qValues);
// The third entry, at index 2, is the table name
sTableList += qValues[2].ToString() + "\n";
}
}
}
Console.WriteLine(sTableList);
}
}
}
}
sacraments apostles Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.