Reputation: 550
I'm coding a tool to count all entries in a table. My question is, how can I get all the tables of an existing database? I'm using Microsoft SQL Server 2008 R2 and I already have a method to get a string array of a table:
List<string> lResult = new List<string>();
SqlConnection sqlConn10 = new SqlConnection(sConnStr);
sqlConn10.Open();
SqlCommand sqlComm10 = new SqlCommand("SELECT " + sColumn + " FROM " + sTable + " WHERE " + sWhere, sqlConn10);
SqlDataReader myReader10 = sqlComm10.ExecuteReader();
int i = 0;
try
{
while (myReader10.Read())
{
lResult.Add(myReader10.GetString(0));
i++;
}
}
catch
{
myReader10.Close();
sqlConn10.Close();
}
return lResult.ToArray();
}
Upvotes: 1
Views: 4033
Reputation: 505
Either you use the sql-command
select * from sys.tables
or you use a DataTable
DataTable dt = SQLConnection.GetSchema("Tables");
foreach (DataRow row in dt.Rows)
{
...
}
Upvotes: 2
Reputation: 247670
to get the list of all tables you need to use a query similar to this:
SELECT *
FROM information_schema.tables
Upvotes: 1
Reputation: 44316
SELECT * FROM INFORMATION_SCHEMA.TABLES
INFORMATION_SCHEMA
has a lot of useful metadata about your database.
Upvotes: 2