如何将参数传递给Sql Data Adapter

编程入门 行业动态 更新时间:2024-10-25 05:28:44
本文介绍了如何将参数传递给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 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

本文发布于:2023-06-04 19:08:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   参数   Sql   Data   Adapter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!