Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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
asp.net/sql当Server.GetLastError()中的文本在字符串中有单引号时,如何将其保存到DB?_Asp.net_Sql Server - Fatal编程技术网

asp.net/sql当Server.GetLastError()中的文本在字符串中有单引号时,如何将其保存到DB?

asp.net/sql当Server.GetLastError()中的文本在字符串中有单引号时,如何将其保存到DB?,asp.net,sql-server,Asp.net,Sql Server,我有下面的代码,发现在Server.GetLastError 包含单引号并打断我的SQL插入代码 Exception ex = Server.GetLastError(); StringBuilder theBody = new StringBuilder(); theBody.Append("Error Message: " + ex.ToString() + "\n"); Server.ClearError(); try { string sSQ

我有下面的代码,发现在
Server.GetLastError
包含单引号并打断我的SQL插入代码

Exception ex = Server.GetLastError();

StringBuilder theBody = new StringBuilder();        
theBody.Append("Error Message: " + ex.ToString() + "\n");

Server.ClearError();

try
    {
        string sSQL = "INSERT INTO PMISwebErr VALUES ('" + theBody.ToString() + "', GETDATE())";

        using (System.Data.SqlClient.SqlConnection con = STAR.Global.GetConnection())
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sSQL, con);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.ExecuteScalar();
        }
    }
    catch (Exception exe) {
        Response.Redirect("~/default.aspx?Err="+ exe.Message.ToString() );
    }

欢迎来到SQL注入攻击的精彩世界

选项1:搜索并替换body.ToString(),将“.”替换为““””

选择2:


而且,正因为如此,您也需要这样做。

欢迎来到SQL注入攻击的精彩世界

选项1:搜索并替换body.ToString(),将“.”替换为““””

选择2:

只是因为你也需要这样做

您应该始终在SQL语句中使用参数。它不仅处理单引号字符串之类的情况,而且有助于防止SQL注入攻击


您应该始终在SQL语句中使用参数。它不仅处理单引号字符串等情况,而且有助于防止SQL注入攻击。

使用您的应用程序,输入并保存此值:
1',GETDATE();删除表PMISwebErr--
并查看在连接包含用户输入的字符串以生成sql命令时发生的情况。请参阅“小Bobby表”-使用应用程序输入并保存此值:
1',GETDATE();删除表PMISwebErr--
并查看在连接包含用户输入的字符串以生成sql命令时会发生什么。请参阅“小Bobby表”-+1,我想我会突出显示您所做的一个非常重要的声明。+1,我想突出显示您所做的一个非常重要的声明。
    string sSQL = "INSERT INTO PMISwebErr VALUES (@errVal, GETDATE())";

    using (System.Data.SqlClient.SqlConnection con = STAR.Global.GetConnection())
    {
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sSQL, con);
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue( "@errVal", theBody.ToString() );
        cmd.ExecuteScalar();
    }