Well, you can use the Connection.GetSchema("TABLES") method.
Returns a DataTable that will contain the rows of all tables in your database. From here you can check this out and see if the table exists.
Then you can take the next step:
private static bool DoesTableExist(string TableName) { using (SqlConnection conn = new SqlConnection("Data Source=DBServer;Initial Catalog=InitialDB;User Id=uname;Password=pword;")) { conn.Open(); DataTable dTable = conn.GetSchema("TABLES", new string[] { null, null, "MyTableName" }); return dTable.Rows.Count > 0; } }
If you are using .NET 3.5, you can also do this with an extension method.
Kyle rozendo
source share