C# 在SQL查询中包含文本框值

C# 在SQL查询中包含文本框值,c#,sql,textbox,visual-studio-2013,C#,Sql,Textbox,Visual Studio 2013,我正在尝试使用文本框和搜索按钮搜索GridView。我想我需要一个类似这样的问题 SELECT employeeID, name, position, hourlyPayRate FROM dbo.employee WHERE name LIKE 'textBox1.text+' 我使用visual studio 2013中的查询设计器进行了查询 然后我有一个这样的事件处理程序 private void btnSearch_Click(object sender, EventArgs e)

我正在尝试使用文本框和搜索按钮搜索GridView。我想我需要一个类似这样的问题

SELECT employeeID, name, position, hourlyPayRate 
FROM dbo.employee 
WHERE name LIKE 'textBox1.text+'
我使用visual studio 2013中的查询设计器进行了查询

然后我有一个这样的事件处理程序

private void btnSearch_Click(object sender, EventArgs e)
{            
    this.employeeTableAdapter.FillBy(this.personnelDataSet.employee); 
}

我确信问题出在查询中,但我不知道如何将文本框的值包含到查询中。

如果我理解正确,您可以手动构建sql查询。使用纯字符串连接是一种不好的做法,因为它使SQL注入成为可能,相反,您应该使用SqlCommand语法并在其中添加SqlParameters

   using (var sqlConnection = new SqlConnection("connection"))
   {
       var commandText = "SELECT employeeID, name, position, hourlyPayRate FROM dbo.employee WHERE name LIKE @name";

       using (var sqlCommand = new SqlCommand(commandText, sqlConnection))
       {
             sqlCommand.Parameters.Add("@salary", SqlDbType.Money).Value = textBox1.text;
       }
   }

它并没有经过实际测试,也可能无法正常工作,但它描述了一个新的想法。您只需添加所需sql类型的参数,请参见此处的更多示例:

where子句应该是where名称,如textBox1.text+“其余查询”


但是正如makambi提到的,您构建查询的方式为您打开了sql注入的大门,您只需更改查询,它应该如下所示:

string textboxValue = textbox1.Text;
string query = "SELECT employeeID, name, position, hourlyPayRate " +
               "FROM dbo.employee " +
               "WHERE name LIKE '" + textboxValue + "'";
但这很容易受到SQL注入的影响,您应该使用带有:

更新:

要在单击按钮时执行此代码,请将代码放在此处(确保您有一个名为
YourButton
)的按钮:

更新2:

您应该在SqlConnection中使用一个连接字符串,该字符串可能类似于:

string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
在这里,您必须将以
my
开头的值替换为您自己的值。有关SQL Server连接字符串的更多信息/示例:


  • 正如马坎比已经说过的那样。你不应该这样做,因为你正在为SQL注入打开大门。而是使用如下参数化查询:

    SqlCommand cmd = new SqlCommand("SELECT employeeID, name, position, hourlyPayRate FROM dbo.employee WHERE name LIKE @name", connection);
    
    cmd.Parameters.Add("@name", textBox1.Text);
    connection.Open();
    cmd.ExecuteNonQuery();
    

    或者使用存储的过程。

    作为次要旁白;下面是一种替代方法,它展示了如何通过简单但安全有效的参数化和物化来避免
    数据表
    等的开销:

    public class Employee {
        public int EmployeeID {get;set;}
        public string Name {get;set;}
        public string Position {get;set;}
        public decimal HourlyPayRate {get;set;}
    }
    ...
    // uses the "dapper" tool to provide the Query<T> extension method;
    // freely available from NuGet: PM> Install-Package Dapper
    var employees = connection.Query<Employee>(@"
    SELECT employeeID, name, position, hourlyPayRate 
    FROM dbo.employee 
    WHERE name LIKE @pattern",
        new { pattern = "%" + textBox1.Text + "%" }).ToList();
    
    grid.DataSource = employees;
    
    公共类员工{
    public int EmployeeID{get;set;}
    公共字符串名称{get;set;}
    公共字符串位置{get;set;}
    公共十进制小时支付率{get;set;}
    }
    ...
    //使用“dapper”工具提供查询扩展方法;
    //从NuGet:PM>安装程序包Dapper免费提供
    var employees=connection.Query(@)
    选择员工ID、姓名、职位、小时工资率
    来自dbo.employee
    其中名称如@pattern“,
    新的{pattern=“%”+textBox1.Text+“%”}).ToList();
    grid.DataSource=员工;
    
    我认为这应该是一个更好的评论,为什么人们坚持使用
    DataTable
    等?想想那些小猫!另外:永远不要连接输入…是的,非常糟糕的joo-joo。你的大学讲师应该被嘲笑。不过,不要管犹太人;法哈,我只是试探一下!你所说的“查询的其余部分”是什么意思?不是吗?别写了,我只是完成了答覆。我想在这里学习好习惯,所以我将尝试使用带有参数的SqlCommand。如何在按钮事件处理程序中执行查询?我已经更改了代码,请将其放置在您喜欢的按钮事件中。@Abbas如果我错了,请更正我。查询中的Position应该是
    [Position]
    。您可能应该就此打开一个新问题,包括代码和抛出的exception+消息。这个例外与您的原始问题无关。我很乐意在这方面进一步帮助你。
    SqlCommand cmd = new SqlCommand("SELECT employeeID, name, position, hourlyPayRate FROM dbo.employee WHERE name LIKE @name", connection);
    
    cmd.Parameters.Add("@name", textBox1.Text);
    connection.Open();
    cmd.ExecuteNonQuery();
    
    public class Employee {
        public int EmployeeID {get;set;}
        public string Name {get;set;}
        public string Position {get;set;}
        public decimal HourlyPayRate {get;set;}
    }
    ...
    // uses the "dapper" tool to provide the Query<T> extension method;
    // freely available from NuGet: PM> Install-Package Dapper
    var employees = connection.Query<Employee>(@"
    SELECT employeeID, name, position, hourlyPayRate 
    FROM dbo.employee 
    WHERE name LIKE @pattern",
        new { pattern = "%" + textBox1.Text + "%" }).ToList();
    
    grid.DataSource = employees;