62,271
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 查询数据库是否存在表
/// </summary>
/// <param name="TableName">表名</param>
public bool ExostsTable(string TableName)
{
SqlCommand com = new SqlCommand("SELECT * FROM sysobjects where name='" + TableName + "'", new SqlConnection(DBHelps.ConnectionString));
com.CommandType = CommandType.Text;
com.Connection.Open();
int num=com.ExecuteNonQuery;
com.Connection.Close();
if(num>0)
return true;
else
return false;
}
为什么查询结果返回总是-1 应该怎么查呢?
public bool ExostsTable(string TableName)
{
int count=0;
SqlCommand com = new SqlCommand("SELECT count(1) FROM sysobjects where name='" + TableName + "'", new SqlConnection(DBHelps.ConnectionString));
com.CommandType = CommandType.Text;
com.Connection.Open();
using(SqlDatareader dr = com.ExecuteReader())
{
while(dr.read())
{
count=dr.getint32(0);
}
}
if(count>0)
return true;
else
return false;
}
