最近被朋友問到一個SqlDataSource調用帶參數存儲過程爲何不成功,代碼以下:javascript
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() java
是啊,爲何不成功呢,提示的消息是太多的參數,這段代碼看起來沒什麼問題啊。幾輪搜索之後,以爲網上說的種種緣由,最可能的是參數名要和字段名一致和參數要加@,因而改爲:
ide
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()this
存儲過程裏也進行相應的修改,把定義的傳入參數和調用的地方都改成@empNo,@empName,仍然失敗!錯誤消息變成了spa
Procedure or Function 'asdfg' expects parameter '@empNo', which was not supplied.就是沒找到@empNo,此時,我有點迷茫。code
因而把@去掉,再試,成功。代碼以下:blog
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執行了兩次,因此會有兩條數據。ip
很奇怪,我一直以爲我遇到的問題別人也必定會遇到過,可是這個例外,嘗試了各類搜索條件,沒有這個問題。全部的文章都說調用存儲過程傳參要用@佔位符。ci
------------------------------------------字符串
以上是網上的一篇文章, 確實解決了個人一個問題:
使用參數時用@會提示你給參數賦值, 去掉@,直接使用參數名稱就能夠了.
同時我還遇到了另一個問題:
執行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賦值, 雖然這樣能夠偷點小懶,可是查詢不到結果,找問題時很不容易的;
通常能夠採起以下作法:
給存儲過程參數一個默認值, 這樣該參數就是可選參數了, 程序使用時,如不查詢該字段,則不給其賦值.