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 the Add 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
发布评论