如何将参数传递给select语句 [英] how can pass parameter to the select statement
本文介绍了如何将参数传递给select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将参数传递给select语句
例如select * from tblcatProducts其中catid = @parameter
DataTable dt2 = new DataTable();
string CS = ConfigurationManager.ConnectionStrings [ DBCS跨度>]的ConnectionString。
使用(SqlConnection con = new SqlConnection(CS))
{
string sqlstr2 = select * from tblcatProducts< /跨度>;
使用(SqlDataAdapter dr2 = new SqlDataAdapter(sqlstr2,CS))
{
dr2.Fill(dt2);
dataGridViewCatProd.DataSource = dt2;
}
}
解决方案
DataAdapter公开它的Command对象,因此您可以将其添加到该对象中。 />
例如:
dr2.SelectCommand.Parameters.AddWithValue( @ catid,catid);
但是你首先需要在你的select语句中添加一个WHERE子句。
string sqlstr2 = select * from tblcatProducts WHERE catid = @catid < /跨度>;
how can pass parameter to the select statement
for example select * from tblcatProducts where catid = @parameter
DataTable dt2 = new DataTable();
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
string sqlstr2 = "select * from tblcatProducts";
using (SqlDataAdapter dr2 = new SqlDataAdapter(sqlstr2, CS))
{
dr2.Fill(dt2);
dataGridViewCatProd.DataSource = dt2;
}
}
解决方案
The DataAdapter exposes it's Command object so you can add it to that.
For example:
dr2.SelectCommand.Parameters.AddWithValue("@catid",catid);
But you'll first need to add to your select statement a WHERE clause.
string sqlstr2 = "select * from tblcatProducts WHERE catid = @catid";
这篇关于如何将参数传递给select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文