Pho*_*ian 0 c# c#-3.0 c#-2.0 c#-4.0
可能重复:
如何在c#中搜索数据库中的字符串
我想详细搜索一个名称..这就是搜索按钮的代码
private void button1_Click(object sender, EventArgs e)
{
string connectionString = Tyres.Properties.Settings.Default.Database1ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM table1 where Details like" + textBox1.Text, conn);
SDA.Fill(dt);
dataGridView1.DataSource = dt;
}
Run Code Online (Sandbox Code Playgroud)
以及我的应用程序示例 单击此处查看图像http://i45.tinypic.com/35aqa6t.jpg
我收到错误:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Run Code Online (Sandbox Code Playgroud)
附加信息:在"likeelie"附近的预期条件的上下文中指定的非布尔类型的表达式.
我试过这个代码来搜索一个整数(比如Quantity),它对我很有用:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = Tyres.Properties.Settings.Default.Database1ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM table1 where Quantity like" + int.parse(textBox1.Text), conn);
SDA.Fill(dt);
dataGridView1.DataSource = dt;
}
Run Code Online (Sandbox Code Playgroud)
所以我需要第一个代码的帮助
看起来你需要在SQL语句中添加一个空格,你只是附加一个textBox1.Text
没有空格的值,所以它的值textBox.Text
变成一个单词like
.
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM table1 where Details like " + textBox1.Text, conn);
Run Code Online (Sandbox Code Playgroud)