我想通过ID存储过程进行搜索 [英] I want to search by ID- stored procedure
本文介绍了我想通过ID存储过程进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在存储过程中按ID搜索。
我有书:
I want to search by ID in a stored proc.
I have tbl book:
BookID | Title | BookAuthor | BookEdition
1 |Sql | Robert | 2nd
|Server | |
查询存储过程:
Query for stored proc:
select * from tblBook
where Id like '@#Id'
C#代码:
C# Code:
private void btnsearch_Click(object sender, EventArgs e)
{
string c = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection(c);
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("searchdata", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", textBox1.Text);
cmd.Parameters.AddWithValue("@ID", txtID.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
con.Open();
sda.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
它不起作用。
It's not working.
推荐答案
如果id为numeric \int;
If id is numeric\int;
select * from tblBook where Id = @Id
你没有做好解释问题的工作,所以我的一些反应是基于猜测。看起来您想要使用名为Id的参数。如果是这样,请删除#符号并删除单引号。所以,你应该有
You have not done a good job explaining the problem so some of my response is based on guessing. It looks like you want to use a parameter named Id. If so, remove the # sign and remove the single quotes. So, you should have
SELECT * FROM tblBook
WHERE id = @Id
这样你可以使用@Id作为参数。
This way you can use @Id as a parameter.
这篇关于我想通过ID存储过程进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文