I am a novice on C# so please help.
The code is to list the names of all the tables in a database and count number of records in each table, error in using the variable tabname, please help, many thanks:
The code is to list the names of all the tables in a database and count number of records in each table, error in using the variable tabname, please help, many thanks:
C#:
private void button1_Click(object sender, EventArgs e)
{
var list = new List<string>();
int maxcount;
String connetionString;
String tabname;
SqlConnection cnn;
connetionString = @"Server=DESKTOP-E441IBF\SQLEXPRESS2017;Database=TZ_Demo;Trusted_Connection=true";
cnn = new SqlConnection(connetionString);
cnn.Open();
DataTable schemaTable = cnn.GetSchema("Tables");
foreach (DataRow row in schemaTable.Rows)
{
list.Add((string)row["TABLE_NAME"]);
}
maxcount = list.Count;
for (int i = 0; i < maxcount; i++)
{
tabname = list; // list of table names
SqlCommand comd = new SqlCommand("select count(*) FROM 'tabname' ", cnn); // error is here, syntax error
int count = Convert.ToInt32(comd.ExecuteScalar());
Console.WriteLine(count);
}
cnn.Close();
}
Last edited by a moderator: