1

我将以下 C# 标记转换为使用 SqlParameter(我是 SqlParameter 新手)以增强安全性:

public static void EmptyTable(string TableToEmpty)
{
    try
    {
        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("TRUNCATE TABLE @prmTableToEmpty", conn))
            //using (SqlCommand cmd = new SqlCommand("TRUNCATE TABLE " + TableToEmpty, conn))
            {
                cmd.Parameters.Add(new SqlParameter("@prmTableToEmpty", TableToEmpty));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
    catch
    {
        throw;
    }
}

没有参数使用它工作顺利。但是,使用这种语法,我得到了这个异常:

    [SqlException (0x80131904): Incorrect syntax near '@prmTableToEmpty'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2073550
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5064508
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +215
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +178
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
   pjsql.EmptyTable(String TableToEmpty) in c:\website\App_Code\mySqlClass.cs:67
   _Default.btnEmptyTheTable_Click(Object sender, EventArgs e) in c:\website\Default.aspx.cs:83
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

提到的两个行号都包含抛出;命令。

我经历了这个: http: //msdn.microsoft.com/en-us/library/4f844fc7 (v=vs.71).aspx

并尝试了这种替代语法:http ://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

但没有运气。

我错过了什么?

4

1 回答 1

3

不能使用参数代替对象名称。

要在动态替换对象名称时增强安全性,请使用quotename(首先使用参数执行命令select quotename(@prmTableToEmpty),然后使用此命令的结果来构建truncate命令并在不带参数的情况下执行它)。

using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
{
    conn.Open();

    string sanitized_name;

    using (SqlCommand cmd = new SqlCommand("select quotename(@prmTableToEmpty)", conn))
    {
        cmd.Parameters.Add(new SqlParameter("@prmTableToEmpty", TableToEmpty));
        sanitized_name = (string)cmd.ExecuteScalar();
    }

    using (SqlCommand cmd = new SqlCommand("TRUNCATE TABLE " + sanitized_name, conn))
    {
        cmd.ExecuteNonQuery();
    }
}
于 2012-04-21T14:10:01.717 回答