如何编写此代码?我收到一个错误:
在需要条件的上下文中指定的非布尔类型的表达式,靠近"and"。
法典:
SqlCommand command =new SqlCommand("select * from inventory WHERE barcode1 and barcode2= '" + textBox10.Text + "'", con);
SqlDataReader read = command.ExecuteReader();
con.open();
while (read.Read())
{
textBox4.Text = (read["parts"].ToString());
}
read.close();
WHERE barcode1 and barcode2=
barcode1
什么? 在开始下一个表达式之前,必须先完成表达式。 也许是这样的?
WHERE barcode1 = @barcode1 AND barcode2 = @barcode2
(另请注意使用查询参数而不是直接连接用户输入。 这将有助于防止 SQL 注入攻击,而您当前的代码极易受到这种攻击。
<小时 />当然,您可以对不同的参数使用相同的值。 所以像这样:
var command = new SqlCommand("SELECT * FROM inventory WHERE barcode1 = @barcode1 AND barcode2 = @barcode2", con);
command.Parameters.AddWithValue("@barcode1", textBox10.Text);
command.Parameters.AddWithValue("@barcode2", textBox10.Text);
// now you can execute the query
男人不工作...仅当我选择一个条形码时才有效
var command = new SqlCommand("SELECT * FROM inventory WHERE barcode1 = @barcode1", con);
command.Parameters.AddWithValue("@barcode1", textBox10.Text);
command.Parameters.AddWithValue("@barcode2", textBox10.Text);