使用SqlDataSource调用带参数存储过程(获取不到数据?)
2016-03-28 15:30
453 查看
最近被朋友问到一个SqlDataSource调用带参数存储过程为什么不成功,代码如下:
是啊,为什么不成功呢,提示的消息是太多的参数,这段代码看起来没什么问题啊。几轮搜索以后,觉得网上说的种种原因,最可能的是参数名要和字段名一致和参数要加@,于是改成:
string user_name = ((TextBox)this.DetailsView1.Rows[1].Cells[1].Controls[0]).Text.ToString().Trim();
string pass_word = ((TextBox)this.DetailsView1.Rows[2].Cells[1].Controls[0]).Text.ToString().Trim();
SqlDataSource1.InsertCommand = "pro_newUser";
SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
SqlDataSource1.InsertParameters.Add("@empNo", TypeCode.String, user_name);
SqlDataSource1.InsertParameters.Add("@empName", TypeCode.String, pass_word);
SqlDataSource1.Insert()
存储过程里也进行相应的修改,把定义的传入参数和调用的地方都改为@empNo,@empName,仍然失败!错误消息变成了
Procedure or Function 'asdfg' expects parameter '@empNo', which was not supplied.就是没找到@empNo,此时,我有点迷茫。
于是把@去掉,再试,成功。代码如下:
但是会产生两条数据,这段代码是写在DetailsView1_ItemInserting里的,根据以前的经验,这个方法在点DetailsView1的插入按钮时触发,只要InsertCommand中有代码,就会去执行,那么最后的一句SqlDataSource1.Insert()具有相同的作用,所以等于把insertCommand执行了两次,所以会有两条数据。
很奇怪,我一直觉得我遇到的问题别人也一定会遇到过,但是这个例外,尝试了各种搜索条件,没有这个问题。所有的文章都说调用存储过程传参要用@占位符。
------------------------------------------
以上是网上的一篇文章, 确实解决了我的一个问题:
使用参数时用@会提示你给参数赋值, 去掉@,直接使用参数名称就可以了.
同时我还遇到了另外一个问题:
执行sr.Select(new DataSourceSelectArguments());时始终未null
最终解决:
sr.ConnectionString ="连接字符串";
sr.SelectCommand = "UP_Report_Storage_ReportPutInCompare1";
sr.DataSourceMode = SqlDataSourceMode.DataSet;
sr.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
sr.SelectParameters.Add("InWarehouseName", DbType.AnsiString, "惠州_实体仓"); //参数名不带@
sr.SelectParameters.Add("dateMonth", DbType.AnsiString, "201512");
sr.SelectParameters.Add("MatCodeName", DbType.AnsiString, "");//不能给存储过程参数赋空值""和null
DataView ds1 = (DataView)sr.Select(new DataSourceSelectArguments());
总结:
对于查询时用到的可选参数, 不能使用""和null赋值, 虽然这样可以偷点小懒,但是查询不到结果,找问题时很不容易的;
一般可以采取如下做法:
给存储过程参数一个默认值, 这样该参数就是可选参数了, 程序使用时,如不查询该字段,则不给其赋值.
string user_name = ((TextBox)this.DetailsView1.Rows[1].Cells[1].Controls[0]).Text.ToString().Trim(); string pass_word = ((TextBox)this.DetailsView1.Rows[2].Cells[1].Controls[0]).Text.ToString().Trim(); SqlDataSource1.InsertCommand = "pro_newUser"; SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure; SqlDataSource1.InsertParameters.Add("x", TypeCode.String, user_name); SqlDataSource1.InsertParameters.Add("y", TypeCode.String, pass_word); SqlDataSource1.Insert()
是啊,为什么不成功呢,提示的消息是太多的参数,这段代码看起来没什么问题啊。几轮搜索以后,觉得网上说的种种原因,最可能的是参数名要和字段名一致和参数要加@,于是改成:
string user_name = ((TextBox)this.DetailsView1.Rows[1].Cells[1].Controls[0]).Text.ToString().Trim();
string pass_word = ((TextBox)this.DetailsView1.Rows[2].Cells[1].Controls[0]).Text.ToString().Trim();
SqlDataSource1.InsertCommand = "pro_newUser";
SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
SqlDataSource1.InsertParameters.Add("@empNo", TypeCode.String, user_name);
SqlDataSource1.InsertParameters.Add("@empName", TypeCode.String, pass_word);
SqlDataSource1.Insert()
存储过程里也进行相应的修改,把定义的传入参数和调用的地方都改为@empNo,@empName,仍然失败!错误消息变成了
Procedure or Function 'asdfg' expects parameter '@empNo', which was not supplied.就是没找到@empNo,此时,我有点迷茫。
于是把@去掉,再试,成功。代码如下:
1 2 3 4 5 | <p> </p> <divonclick= "cnblogs_code_show('dc866ee1-3108-4db4-9dae-a8349ca9953b')" class = "cnblogs_code" style= "width: 1063px; height: 327px;" ><div class = "cnblogs_code_toolbar" ><span class = "cnblogs_code_copy" ><a href= "javascript:void(0);" onclick= "copyCnblogsCode(this)" title= "复制代码" ><img src= "http://common.cnblogs.com/images/copycode.gif" alt= "复制代码" ></a></span></div><img src= "http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" class = "code_img_closed" id= "code_img_closed_dc866ee1-3108-4db4-9dae-a8349ca9953b" ><img src= "http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" onclick= "cnblogs_code_hide('dc866ee1-3108-4db4-9dae-a8349ca9953b',event)" class = "code_img_opened" id= "code_img_opened_dc866ee1-3108-4db4-9dae-a8349ca9953b" style= "display: none;" ><span class = "cnblogs_code_collapse" >代码</span><div class = "cnblogs_code_hide" id= "cnblogs_code_open_dc866ee1-3108-4db4-9dae-a8349ca9953b" ><pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><spanstyle= "color: #000000;" ><p><spanstyle= "background-color: #f5f5f5; font-family: Courier New;" > string user_name = ((TextBox) this .DetailsView1.Rows[1].Cells[1].Controls[0]).Text.ToString().Trim();</span></p><p><spanstyle= "background-color: #f5f5f5; font-family: Courier New;" > string pass_word = ((TextBox) this .DetailsView1.Rows[2].Cells[1].Controls[0]).Text.ToString().Trim();<br>SqlDataSource1.InsertCommand = "pro_newUser" ;<br>SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;</span></p><p><spanstyle= "background-color: #f5f5f5; font-family: Courier New;" >SqlDataSource1.InsertParameters.Add( "empNo" , TypeCode.String, user_name);<br>SqlDataSource1.InsertParameters.Add( "empName" , TypeCode.String, pass_word);</span></p><p><spanstyle= "background-color: #f5f5f5; font-family: Courier New;" >SqlDataSource1.Insert()</span></p><br><br></span></div></pre> </div> <div class = "cnblogs_code_toolbar" ><span class = "cnblogs_code_copy" ><a href= "javascript:void(0);" onclick= "copyCnblogsCode(this)" title= "复制代码" ><img src= "http://common.cnblogs.com/images/copycode.gif" alt= "复制代码" ></a></span></div></div> <p> </p> |
很奇怪,我一直觉得我遇到的问题别人也一定会遇到过,但是这个例外,尝试了各种搜索条件,没有这个问题。所有的文章都说调用存储过程传参要用@占位符。
------------------------------------------
以上是网上的一篇文章, 确实解决了我的一个问题:
使用参数时用@会提示你给参数赋值, 去掉@,直接使用参数名称就可以了.
同时我还遇到了另外一个问题:
执行sr.Select(new DataSourceSelectArguments());时始终未null
最终解决:
sr.ConnectionString ="连接字符串";
sr.SelectCommand = "UP_Report_Storage_ReportPutInCompare1";
sr.DataSourceMode = SqlDataSourceMode.DataSet;
sr.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
sr.SelectParameters.Add("InWarehouseName", DbType.AnsiString, "惠州_实体仓"); //参数名不带@
sr.SelectParameters.Add("dateMonth", DbType.AnsiString, "201512");
sr.SelectParameters.Add("MatCodeName", DbType.AnsiString, "");//不能给存储过程参数赋空值""和null
DataView ds1 = (DataView)sr.Select(new DataSourceSelectArguments());
总结:
对于查询时用到的可选参数, 不能使用""和null赋值, 虽然这样可以偷点小懒,但是查询不到结果,找问题时很不容易的;
一般可以采取如下做法:
给存储过程参数一个默认值, 这样该参数就是可选参数了, 程序使用时,如不查询该字段,则不给其赋值.
相关文章推荐
- MySQL中SELECT+UPDATE处理并发更新问题解决方案分享
- mongodb语法
- oracle in 加select查询语句提示无效的字符
- ibatis - sqlMapConfig.xml配置文件详解
- SQL中Group By的使用
- oracle数据库小结
- Oracle ORA-01033: 错误解决办法
- Oracle 存储过程异常处理
- ubuntu 安装mysql
- 再看数据库——(3)触发器
- 关于mysql的内连接,外连接,交叉连接
- Oracle CDC配置案例
- OCCI数据库ORACLE编程步骤
- Redis-3.0.7主从配置
- Oracle学习笔记(二)——Oracle的基本使用
- MySQL中去除字段中的回车符和换行符
- 通过SQL Server代理启动Windows服务
- 我们怎么获取数据库中的值或者在数据库中添加值那???
- 深入浅出MySQL 数据库开发、优化与管理维护(第2版) -- 读书笔记 -- 基础篇
- Oracle学习笔记(一)——Oracle介绍及安装