如何使用sql数据库在c#中编写用于搜索的源代码 [英] how to write source code for search in c# with sql database
问题描述
如何在C#中搜索代码?
我计划搜索按钮的名称和名为txtsearch和txtsearch的文本框.我一直在搜索有关我正在使用的Datagrid.
在文本框中输入的用户信息以及当我在数据库中获取信息数据时,显示我在Datagrid孜然中编写它的代码,以查看应写的内容
How do I search code in C #?
I plan to search for the name of a button and a textbox called txtsearch and txtsearch. I have been searching for about a Datagrid I am using.
User information entered in the textbox and when I got the information data in the database show the code that I wrote it in Datagrid cumin to see me what I should write
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=PARIS\SQLEXPRESS;Integrated Security=True";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd = new SqlCommand ("select * from samira where name like '%"+txtsearch.Text+"%'");
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Visible = true;
conn.Close();
这将停止运行adapter.fill(dt);
错误代码在哪里?
This will stop running adapter.fill (dt);
Where does the fault code?
推荐答案
在此行:
In this line:
cmd = new SqlCommand ("select * from samira where namelike N'"+txtsearch.Text+"%'");
您需要在名称"和喜欢"之间放置一个空格.
You need to put a space between "name" and "like".
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=PARIS\SQLEXPRESS;Integrated Security=True";
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from samira where name like ''%"+txtsearch.Text+"%''",conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Visible = true;
conn.Close();
我在这里写错了代码
这将停止运行
I wrote the wrong code here
This will stop running
adapter.fill (dt);
故障代码在哪里?
Where does the fault code?
这篇关于如何使用sql数据库在c#中编写用于搜索的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!