如何在SQL查询中使用多个参数

SearchForKnowledge

我最近遇到了有关如何防止SQL注入的声明,因此我将代码更改为此(注释为旧代码):

nameE = txtName.Text;

//sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + nameE + "'";
sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = @name";

using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
      //command.CommandType = CommandType.Text;
      command.Parameters.AddWithValue("name", nameE);

      using (reader = command.ExecuteReader())
      {
        // some action goes here...
      }
 }

如何使用多个参数执行相同的操作?

我的代码是在此处使用此函数填充两个参数作为另一个函数的变量的函数:

public void writeData(string k, string c)
{
    Conn = new SqlConnection(cString);
    Conn.Open();

    //MessageBox.Show(k);
    //MessageBox.Show(c);

    var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/fw9.pdf"));

    // Get the form fields for this PDF and fill them in!
    var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);

    //if more than multiple entries, verify by name and the last four ssn
    //sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + k + "' AND [ssn3] = " + c + "";
    sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = @name2 AND [ssn3] = @ssnnum";
    //MessageBox.Show("" + sqlCode.ToString());

    using (SqlCommand command = new SqlCommand(sqlCode, Conn))
    {
        //command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("name2", k);
        command.Parameters.AddWithValue("ssnnum", c);

        using (reader = command.ExecuteReader())
        {
            if (reader.HasRows)
            {
                if (reader.Read())
                {
                    MessageBox.Show(reader.GetValue(0).ToString());
                    /*formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = reader.GetValue(0).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
                    formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();*/
                }
            }
        }
    }

    // Requester's name and address (hard-coded)
    //formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n27 West Ave\nPurchase, NY 10577";

    //var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);

    //PDFHelper.ReturnPDF(pdfContents, "Completed-W9.pdf");
}

在此处输入图片说明

在此处输入图片说明

霍拉修

您可以像以前一样添加参数。这是您的代码的样子,如下所示:

sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = @name AND [ssn3] =@ssn3";

using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
      //command.CommandType = CommandType.Text;
      command.Parameters.AddWithValue("@name", nameE);
      command.Parameters.AddWithValue("@ssn3", c);

      using (reader = command.ExecuteReader())
      {
        // some action goes here...
      }
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在PreparedStatement中使用多个可选参数填充查询sql?

如何在Golang应用程序中使用多个JSON参数创建SQL查询?

如何在TpFIBDataSet SQL查询中使用参数?

如何在带参数的SQL查询中使用通配符

如何在 SQL 查询中使用 AND 搜索多个术语?

如何在属性中使用查询参数?

如何在查找中使用多个参数?

如何在 $_POST 中使用多个参数

如何在单个查询中使用不同的参数执行多个连接

如何在FeignClient中使用多个查询字符串参数调用url?

如何在Swift中使用包含相同键的多个值的查询参数构建URL?

如何在 Django 中使用相同的键过滤多个查询参数?

如何在Oracle SQL中使用声明的参数过滤SQL查询

如何在 GCP BigQuery 联合查询中使用查询参数

如何在SQL中使用子查询?

如何在SQL查询中使用VIEWS

如何在关系SQL查询中使用NOT

如何在 Django 中使用 NOT IN sql 查询

如何在 SQL 查询中使用 if

如何在SQL查询中使用Arraylist?

如何在Postgres查询窗口中使用参数测试我的即席SQL

如何在python的嵌入式sql查询中使用参数值?

我们如何在SQL查询列中使用iReport参数

如何在sql查询命令中使用方法输入中退出的参数?

如何在SQL查询中使用IF-THEN-ELSE逻辑包括可选参数?

如何在ado.net中使用输出参数并选择SQL Server存储过程的查询结果?

如何在C#中使用字符串参数值进行SQL查询

如何在SQL查询中使用多个HTML复选框中的值?

如何在SQL Server中使用单个查询在多个表中插入数据?