如何将参数传递给Sql Data Adapter [英] How to pass parameters to Sql Data Adapter
问题描述
Hiii,
我想在sql数据适配器中传递参数我试试这个
string cmdstr =select * from Report where InstituteId = @InstituteId;
SqlDataAdapter adp = new SqlDataAdapter(cmdstr,con);
adp.SelectCommand.Parameters.Add(@ InstituteId,Session [InstituteId]);
但是我得到错误参数化查询'(@InstituteId nvarchar(4000))需要参数'@InstituteId',这是未提供的。
Hiii,
I want to pass parameter in sql data adapter i try this
string cmdstr = "select * from Report where InstituteId=@InstituteId";
SqlDataAdapter adp = new SqlDataAdapter(cmdstr, con);
adp.SelectCommand.Parameters.Add("@InstituteId", Session["InstituteId"]);
But im getting error The parameterized query '(@InstituteId nvarchar(4000)) expects the parameter '@InstituteId', which was not supplied.
推荐答案
string cmdstr = "select * from Report where InstituteId=@InstituteId";
Sqlcommand cmd=new SqlCommand(cmdstr,con);
cmd.Parameter.add(new SqlParameter("@InstituteId", Session["InstituteId"].toString());
SqlDataAdapter adp = new SqlDataAdapter(cmd);
Datatable dt=new Datatable();
adap.fill(dt);
//adp.SelectCommand.Parameters.Add("@InstituteId", Session["InstituteId"]);
1.问题是添加
方法需要一个未正确提供的字符串类型的参数。也许会话缓存中的值为null,这个特殊情况应该是管理的。
2.你应该像这样改进你的代码:
1.The problem is that theAdd
method expect a parameter of type string that was not provided correctly. Maybe the value from session cache is null and this special case should be manage.
2.You should improve your code like this:
string paramValue = (string)Session["InstituteId"]; //Unbox the value from the session cache!
adp.SelectCommand.Parameters.Add("@InstituteId",SqlDbType.NVarChar, 40, (paramValue == null ? DBNull.Value : paramValue)); //Specify the type and length of the parameter!
这篇关于如何将参数传递给Sql Data Adapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!