Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何放下桌子_C#_Sql Server 2008 - Fatal编程技术网

C# 如何放下桌子

C# 如何放下桌子,c#,sql-server-2008,C#,Sql Server 2008,我使用以下C#方法执行查询: 当我通过这项声明时: ExecuteQuery("DROP TABLE MyTable"); 然后该方法返回true,这意味着它工作正常,但当我检查时,myTable没有被删除。如果在中运行相同的语句,MyTable将被删除 我错在哪里?在回答您的问题之前,请给出一些评论: 避免使用查询文本对此类操作进行编码,这很可能会导致安全问题。最好创建以下存储过程: 然后将存储过程的名称作为参数传递给函数。现在回到你的错误 表drop不是一个事务模式,但您尝试在事务模

我使用以下C#方法执行查询:

当我通过这项声明时:

ExecuteQuery("DROP TABLE MyTable");
然后该方法返回true,这意味着它工作正常,但当我检查时,
myTable
没有被删除。如果在中运行相同的语句,
MyTable
将被删除


我错在哪里?

在回答您的问题之前,请给出一些评论:

  • 避免使用查询文本对此类操作进行编码,这很可能会导致安全问题。最好创建以下存储过程:

然后将存储过程的名称作为参数传递给函数。现在回到你的错误

表drop不是一个事务模式,但您尝试在事务模式中执行它。这使得它失败了。尝试:

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;
}

在回答您的问题之前,请发表以下评论:

  • 避免使用查询文本对此类操作进行编码,这很可能会导致安全问题。最好创建以下存储过程:

然后将存储过程的名称作为参数传递给函数。现在回到你的错误

表drop不是一个事务模式,但您尝试在事务模式中执行它。这使得它失败了。尝试:

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#Side note中调用它的最佳方法-您应该在使用完资源后立即处理它们,以避免内存泄漏(实现IDisposable的任何东西-我在这里看到的一些示例是SqlConnection、SqlCommand,我相信还有SqlTransaction)创建存储过程并在c#Side note中调用它的最佳方法-您应该在使用完资源后立即处理它们,以避免内存泄漏(任何实现IDisposable的东西-我在这里看到的一些示例是SqlConnection、SqlCommand和我相信SqlTransaction),sp也会抛出错误
increct语法靠近@tblename
1+用于回答中的更新谢谢你的评论,这真的是我的错:/Side注意:你不应该在你的存储过程中使用
sp\
前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用
sp.
并使用其他东西作为前缀,或者根本不使用前缀!sp将抛出错误
increct语法靠近@tblename
1+用于回答中的更新谢谢你的评论,这真的是我的错:/Side注意:你不应该在你的存储过程中使用
sp\
前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用
sp.
并使用其他东西作为前缀,或者根本不使用前缀!
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;
}