这个查询应该给你答案:
select count(id) from sysobjects where name = 'thetable' and type = 'U'
Run Code Online (Sandbox Code Playgroud)
如果count是1表存在,如果是0,则不存在.
包装成方法:
private bool TableExists(string tableName)
{
using (SqlConnection conn = new SqlConnection(GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("select count(id) from sysobjects where name = @tableName and type = 'U'", conn))
{
cmd.Parameters.AddWithValue("@tableName", tableName);
conn.Open();
int count = (int)cmd.ExecuteScalar();
conn.Close();
return count == 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)