如何将参数传递给Sql Data Adapter [英] How to pass parameters to Sql Data Adapter

查看:60
本文介绍了如何将参数传递给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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆