如何删除 table
How to drop a table
我正在使用以下 C# 方法执行 SQL 查询:
public bool ExecuteQuery(String pQuery)
{
SqlConnection con = new SqlConnection("MyConnectionString");
con.Open();
SqlTransaction trans = con.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
SqlCommand cmd = new SqlCommand(pQuery, con, trans);
cmd.ExecuteNonQuery();
trans.Commit();
con.Close();
trans.Dispose();
return true;
}
catch (Exception exp)
{
trans.Rollback();
con.Close();
MessageBox.Show(exp.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
当我通过这条语句时:
ExecuteQuery("DROP TABLE MyTable");
然后方法 returns 为真,这意味着它工作正常,但是当我检查 SQL Server, myTable
was not dropped. If I run the same statement in SQL Server Management Studio 时,MyTable
被删除了...
我哪里错了?
在回答你的问题之前,先评论一下:
避免使用查询文本对此类操作进行编码,这很可能会导致安全问题。最好创建 executes table drop:
的存储过程
create procedure sp_DropTable
@tablename varchar(200)
as
BEGIN
DECLARE @SQL VARCHAR(MAX);
SET @SQL = 'IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N''' + @tableName + ''') AND type = (N''U'')) DROP TABLE [' + @tableName + ']'
EXEC (@SQL);
END
GO
然后将存储过程的名称作为参数传递给您的函数。现在回到你的错误。
Table drop 不是 transaction,但您尝试在事务模式中执行它。这使它失败。尝试:
public bool ExecuteQuery(String pQuery)
{
SqlConnection con = new SqlConnection("MyConnectionString");
con.Open();
try
{
SqlCommand cmd = new SqlCommand(pQuery, con);
// if you pass just query text
cmd.CommandType = CommandType.Text;
// if you pass stored procedure name
// cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
con.Close();
return true;
}
catch (Exception exp)
{
con.Close();
MessageBox.Show(exp.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
我正在使用以下 C# 方法执行 SQL 查询:
public bool ExecuteQuery(String pQuery)
{
SqlConnection con = new SqlConnection("MyConnectionString");
con.Open();
SqlTransaction trans = con.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
SqlCommand cmd = new SqlCommand(pQuery, con, trans);
cmd.ExecuteNonQuery();
trans.Commit();
con.Close();
trans.Dispose();
return true;
}
catch (Exception exp)
{
trans.Rollback();
con.Close();
MessageBox.Show(exp.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
当我通过这条语句时:
ExecuteQuery("DROP TABLE MyTable");
然后方法 returns 为真,这意味着它工作正常,但是当我检查 SQL Server, myTable
was not dropped. If I run the same statement in SQL Server Management Studio 时,MyTable
被删除了...
我哪里错了?
在回答你的问题之前,先评论一下:
避免使用查询文本对此类操作进行编码,这很可能会导致安全问题。最好创建 executes table drop:
的存储过程create procedure sp_DropTable @tablename varchar(200) as BEGIN DECLARE @SQL VARCHAR(MAX); SET @SQL = 'IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N''' + @tableName + ''') AND type = (N''U'')) DROP TABLE [' + @tableName + ']' EXEC (@SQL); END GO
然后将存储过程的名称作为参数传递给您的函数。现在回到你的错误。
Table drop 不是 transaction,但您尝试在事务模式中执行它。这使它失败。尝试:
public bool ExecuteQuery(String pQuery)
{
SqlConnection con = new SqlConnection("MyConnectionString");
con.Open();
try
{
SqlCommand cmd = new SqlCommand(pQuery, con);
// if you pass just query text
cmd.CommandType = CommandType.Text;
// if you pass stored procedure name
// cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
con.Close();
return true;
}
catch (Exception exp)
{
con.Close();
MessageBox.Show(exp.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}