急用:怎么用C#语言判断SQL数据库中已经存在某个表格了啊?????????
答案:3 悬赏:20
解决时间 2021-02-27 09:17
- 提问者网友:战魂
- 2021-02-26 12:49
急用:怎么用C#语言判断SQL数据库中已经存在某个表格了啊?????????
最佳答案
- 二级知识专家网友:迷人又混蛋
- 2021-02-26 13:31
这人最好,直接给你个过程,调用即可。
#region 判断数据库表是否存在,通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。
///
/// 判断数据库表是否存在,返回页头,通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。
///
/// bhtsoft表
///
public static bool CheckExistsTable(string tablename)
{
String tableNameStr = "select count(1) from sysobjects where name = '" + tablename + "'";
using (SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand(tableNameStr, con);
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result == 0)
{
return false;
}
else
{
return true;
}
}
}
#endregion
#region 判断数据库表是否存在,通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。
///
/// 判断数据库表是否存在,返回页头,通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。
///
/// bhtsoft表
///
public static bool CheckExistsTable(string tablename)
{
String tableNameStr = "select count(1) from sysobjects where name = '" + tablename + "'";
using (SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand(tableNameStr, con);
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result == 0)
{
return false;
}
else
{
return true;
}
}
}
#endregion
全部回答
- 1楼网友:西风乍起
- 2021-02-26 16:21
把这个sql语句在C#中写一个方法进行判断就可以了啊
select * from dbo.sysobjects where id = object_id(N '[dbo].[warn_info] ') and OBJECTPROPERTY(id, N 'IsUserTable ') = 1
select * from dbo.sysobjects where id = object_id(N '[dbo].[warn_info] ') and OBJECTPROPERTY(id, N 'IsUserTable ') = 1
- 2楼网友:过活
- 2021-02-26 14:59
我暂时保留我的看法!
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯