我是想在.net中判断表是否存在
存在就删除 不存在就报错
语句是这样的
if (exists("select name from sysobjects where name='" + carbarn.Text + "'"))
{
delete = " drop table '" + carbarn.Text + "'";
MessageBox.Show("表成功删除!",
"提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show("该表不存在", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}运行时 出现当前上下文中不存在名称“exists”
可是exists不是SQL吗 怎么会不存在 麻烦 大大们帮忙解决下
如果语句有错误也请指点下谢谢
存在就删除 不存在就报错
语句是这样的
if (exists("select name from sysobjects where name='" + carbarn.Text + "'"))
{
delete = " drop table '" + carbarn.Text + "'";
MessageBox.Show("表成功删除!",
"提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show("该表不存在", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}运行时 出现当前上下文中不存在名称“exists”
可是exists不是SQL吗 怎么会不存在 麻烦 大大们帮忙解决下
如果语句有错误也请指点下谢谢
解决方案 »
- C#下导入excel到DataTable,不能读取工作簿表名,纠结啊!
- 怎么样用OleDB写一个添加资料到数据库Access???
- 判断时间段交叉问题,请大家给看看。
- 窗体最大化之后,查询数据库出错
- 大虾们帮帮忙吧,我这个下载程序只能一个字节一个字节的下载,怎么办呀!
- 问你一个简单的问题关于richtextbox
- ???为什么我在使用vs2003 dotfuscator进行模糊处理时有问题?!!
- 无法调试,怎么回事?
- 主窗口菜单如何调用子窗口中的某一个函数?急啊~~
- WebFrom程序(C#)使用下面3个控件来实现读取excel
- 求助winfrom下vs2005如何连接access?
- 请高手帮忙,SQL中附加数据库时提示日志出错,不能添加附加数据库,急
sqlConn.ConnectionString = "Data Source=[数据库服务器地址],[数据库端口];Initial Catalog=[数据库名称];Integrated Security=True";
string sql=@"if exists (select * from dbo.sysobjects where id = object_id('"+carbarn.Text +"') and OBJECTPROPERTY(id, N'IsUserTable') = 1) begin drop table [dbo].[A] select @Result=0 end else select @Result=1";
sqlConn.Open();
System.Data.SqlClient.SqlCommand cmd = sqlConn.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.Add("@Result", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
if (0 == (int)cmd.Parameters["@Result"].Value)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("表不存在");
}
sqlConn.Close();给分吧!
System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
sqlConn.ConnectionString = "Data Source=[数据库服务器地址],[数据库端口];Initial Catalog=[数据库名称];Integrated Security=True";
string sql = @"if exists (select * from dbo.sysobjects where id = object_id('" + carbarn.Text + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1) begin drop table "+carbarn.Text +" select @Result=0 end else select @Result=1";
sqlConn.Open();
System.Data.SqlClient.SqlCommand cmd = sqlConn.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.Add("@Result", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
if (0 == (int)cmd.Parameters["@Result"].Value)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("表不存在");
}
sqlConn.Close();