我收到这个错误:
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/