如何使用c#检查sql server 2008中是否存在表。
问题描述:
如何使用c#检查sql server 2008中的数据库中是否存在表。
how to check if a table exist in a database in sql server 2008 using c#.
答
是这样的:
like that:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_DbTableExistance
{
class Program
{
static void Main(string[] args)
{
string connStr = @"data source=.\sqlexpress; initial catalog=KKD; integrated security=true";
string tableQuery = @"select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME='{0}'";
try
{
string cmdText = string.Format(tableQuery, "DISPLAY_STAT");
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
object o = cmd.ExecuteScalar();
Console.WriteLine(o == null ? "none" : "exists");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
看看这个:我如何检查在sql server 2008 R2上存在我的表C#? [ ^ ]