检索特定ID时有些疑问 [英] Some doubt in retrive the particular id
问题描述
I DESIGN如下所示的小应用程序;
项目ID Combobox1
项目名称Textbox1
评价Textbox2
单位文本框3
Amt Textbox4
所有数据都存储在数据库中.
我在文本框中有一个文本框和一个按钮(按钮名称Project Wise Id),我键入字母R仅以R开头的项目ID想要显示输出,如下所示:
项目ID项目名称费率单位Amt
R001嵌入式30 16480
R002嵌入式40 16 640
1120
项目ID项目名称费率单位Amt
P001 JAVA 15 5 75
P002 .Net 20 10 200
R001嵌入式30 16480
R002嵌入式40 16 640
总计1395
I DESIGN A small application as follows;
Project ID Combobox1
Project Name Textbox1
Rate Textbox2
Unit Textbox3
Amt Textbox4
all the datas are stored in the database.
I have one textbox and one button(button name Project Wise Id)in textbox i type the letter R Only the project id start with R want to display the output as follows;
Project ID Project Name Rate Unit Amt
R001 Embedded 30 16 480
R002 Embedded 40 16 640
1120
Project ID Project Name Rate Unit Amt
P001 JAva 15 5 75
P002 .Net 20 10 200
R001 Embedded 30 16 480
R002 Embedded 40 16 640
Total 1395
private void button3_Click(object sender, EventArgs e)
{
con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
con.Open();
string str = "select * from Project where PojectID like = '" + cb_id.SelectedItem + "'";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter sqlda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sqlda.Fill(ds, "Project");
con.Close();
ReportDocument r1 = new ReportDocument();
//r1.Load("Full Path of Your Report\Employee.rpt");
//string path = Server.MapPath("Reports/Service Bill.rpt");
r1.Load("Reports/ProjectReport.rpt");
r1.SetDataSource(ds);
crystalReportViewer1.ReportSource = r1;
crystalReportViewer1.RefreshReport();
}
这里的查询如下:从Project中选择*,其中PojectID像=""+ cb_id.SelectedItem +"'';
从上面请更正查询,我对此表示怀疑.
添加了代码块[/编辑]
Here this query as follows: "select * from Project where PojectID like = ''" + cb_id.SelectedItem + "''";
From the above please correct the query i have some doubt.
Code block added[/Edit]
推荐答案
Sql LIKE需要通配符才能有效.在常规搜索中,%"在SQL中等效于"*",表示匹配任意数量的任何字符"
试试:
Sql LIKE requires wildcard characters to be effective. ''%'' is the Sql equivalent of ''*'' in a normal search, meaning "match any number of any character"
Try:
string str = "select * from Project where PojectID like = '" + cb_id.SelectedItem + "%'";
匹配ComboBox中的以任何开头"的值.
to match "anything starting with" the value in the ComboBox.
private void button3_Click(object sender, EventArgs e)
{
con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
con.Open();
string str = "select * from Project where PojectID like = '" + cb_id.SelectedItem + "%'";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter sqlda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sqlda.Fill(ds, "Project");
con.Close();
ReportDocument r1 = new ReportDocument();
//r1.Load("Full Path of Your Report\Employee.rpt");
//string path = Server.MapPath("Reports/Service Bill.rpt");
r1.Load("Reports/ProjectReport.rpt");
r1.SetDataSource(ds);
crystalReportViewer1.ReportSource = r1;
crystalReportViewer1.RefreshReport();
}
请在您的SQL语句中使用LIKE或=运算符.
尝试:
Please use either LIKE or = operator in your SQL statement.
Try with:
string str = "select * from Project where PojectID like '" + cb_id.SelectedItem + "%'";
祝您编码愉快!
Happy coding!
这篇关于检索特定ID时有些疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!