66 lines
3.0 KiB
HTML
66 lines
3.0 KiB
HTML
|
||
|
||
<div id="sina_keyword_ad_area2" class="articalContent ">当存储过程中多个结果需要返回时经常需要用到output类型的参数,如果存储过程没有返回结果集只是输出output类型参数时使用如下代码:
|
||
<div><br>
|
||
<div>db.AddOutParameter(dbCmd, "@<span style="line-height:21px">ParamName</span>", DbType.Int32, 0);</div>
|
||
<div>db.ExecuteNonQuery(dbCmd);<br>
|
||
<div>int i = dbCmd.Parameters["@ParamName"].Value.ToString();</div>
|
||
</div>
|
||
</div>
|
||
<div><br>
|
||
</div>
|
||
<div>但是存储过程返回结果集时使用上面的代码程序将会出现异常,所以存储过程有结果集返回时应使用如下代码:</div>
|
||
<div><br>
|
||
</div>
|
||
<div>
|
||
<div style="line-height:21px">db.AddOutParameter(dbCmd,"@<span style="line-height:21px">ParamName</span>", DbType.Int32,0);</div>
|
||
<div>
|
||
<div> <wbr>using (IDataReader idr =db.ExecuteReader(dbCmd))</div>
|
||
<div>{</div>
|
||
<div style="line-height:21px"><span style="line-height:21px">}</span></div>
|
||
</div>
|
||
</div>
|
||
<div style="line-height:21px"><span style="line-height:21px">int i =dbCmd.Parameters["@ParamName"].Value.ToString();</span></div>
|
||
<div style="line-height:21px"><span style="line-height:21px"><br>
|
||
</span></div>
|
||
<div style="line-height:21px">或者</div>
|
||
<div style="line-height:21px"><br>
|
||
</div>
|
||
<div style="line-height:21px">
|
||
<div style="line-height:21px">
|
||
<div style="line-height:21px">db.AddOutParameter(dbCmd,"@<span style="line-height:21px">ParamName</span>", DbType.Int32,0);</div>
|
||
<div>
|
||
<div> <wbr>using (IDataReader idr =db.ExecuteReader(dbCmd))</div>
|
||
<div>{</div>
|
||
<div>while(idr.Read())</div>
|
||
<div>{</div>
|
||
<div>}idr.Close();</div>
|
||
<div><span style="line-height:21px">int i =dbCmd.Parameters["@ParamName"].Value.ToString();</span></div>
|
||
<div style="line-height:21px"><span style="line-height:21px">}</span></div>
|
||
</div>
|
||
</div>
|
||
<div style="line-height:21px"><br>
|
||
</div>
|
||
<div style="line-height:21px">再或者</div>
|
||
<div style="line-height:21px"><br>
|
||
</div>
|
||
<div style="line-height:21px">
|
||
<div style="line-height:21px">db.AddOutParameter(dbCmd,"@<span style="line-height:21px">ParamName</span>", DbType.Int32,0);</div>
|
||
<div style="line-height:21px">
|
||
<div> <wbr>using (IDataReader idr =db.ExecuteReader(dbCmd))</div>
|
||
<div>{</div>
|
||
<div>while(idr.Read())</div>
|
||
<div>{</div>
|
||
<div>}</div>
|
||
<div>idr.NextResult();</div>
|
||
<div><span style="line-height:21px">int i =dbCmd.Parameters["@ParamName"].Value.ToString();</span></div>
|
||
<div><span style="line-height:21px">idr.Close();</span></div>
|
||
<div style="line-height:21px"><span style="line-height:21px">}</span></div>
|
||
</div>
|
||
</div>
|
||
<br>
|
||
</div>
|
||
<div style="line-height:21px">总之,想要取得<span style="line-height:21px">output参数输出的结果值只能在DataReader结果集关闭或者指向一下个结果集时才能取到值。</span></div>
|
||
<div style="line-height:21px">以上代码使用的是.net企业库Microsoft.Practices.EnterpriseLibrary</div>
|
||
</div>
|
||
|