gpt4 book ai didi

c# - "An expression of non-boolean type specified"从 .Net 执行 SQL 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:35 25 4
gpt4 key购买 nike

我收到这个错误:

An expression of non-boolean type specified in a context where a condition is expected, near 'likeram'.

我在 txt_name 中输入了“ram”:

SqlConnection con = new SqlConnection(
@"Data Source=DELL_LAPTOP\sqlexpress;Integrated Security=True");
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter(
"SELECT * FROM newproj where name like" + txt_name.Text, con);
SDA.Fill(dt);
dataGridView1.DataSource = dt;

最佳答案

like 和字符串连接以及参数两边的引号之间缺少空格:

SqlDataAdapter SDA = new SqlDataAdapter(
string.Format("SELECT *
FROM newproj
WHERE name like '{0}'" txt_name.Text), con);

虽然我建议您不要使用该方法,因为它容易出现 SQL 注入(inject)。改用 SQL 参数:

SqlCommand command = new SqlCommand("SELECT * FROM newproj where name like @text");
command.Parameters.AddWithValue("text", txtName.Text);
var sqlAdapter = new SqlDataAdapter(command);

关于c# - "An expression of non-boolean type specified"从 .Net 执行 SQL 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31830311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com