使用SqlDataSource调用带参数存储过程(获取不到数据?)

最近被朋友问到一个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(
"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>
<div onclick=     "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 />--><span style=     "color: #000000;"     ><p><span style=     "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><span style=     "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><span style=     "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><span style=     "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>

但是会产生两条数据,这段代码是写在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赋值, 虽然这样可以偷点小懒,但是查询不到结果,找问题时很不容易的;

一般可以采取如下做法:

给存储过程参数一个默认值, 这样该参数就是可选参数了, 程序使用时,如不查询该字段,则不给其赋值.

 

转载于:https://my.oschina.net/leet123/blog/648556

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值