Sql Server 中存儲過程的output return的區別

看http://zxianf.blog.163.com/blog/static/301207012009114104124969/中片關于Sql Server中存儲過程output和return值的區別

在里面有講解,我在自己本機中測試的結果如下,

1:ReturnValue只能返回0,1,-1這樣的數據,局限性很大?,而在存儲過程中用OutPut參數,可以返回各種類型的數據,比較靈活方便。

ReturnValue ? 是用來返回錯誤碼的,output是指存儲過程傳出參數 ? ? ? 例如????:

@Flag   varchar(20)   output   
View Code
 1 sql存儲過程:
2 create proc Test
3 @B varchar(50) output,
4 @C varchar(50)
5 as
6 begin
7 declare @A int
8 set @B=@C+'Return'
9 set @A=1000
10 return @A
11 end


c#程序代碼:???????
View Code
 1  System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=(local);uid=sa;pwd=sa;database=ServerUForVhost1");
2 System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("Test", conn);
3 comm.CommandType = System.Data.CommandType.StoredProcedure;
4 //調用sqlhelper時這樣寫,單獨不行:comm.Parameters.Add(不能addsqlparameter[])
5 //SqlParameter[] parameter ={
6 // new System.Data.SqlClient.SqlParameter("@A",System.Data.SqlDbType.Int,4),
7 // new System.Data.SqlClient.SqlParameter("@B",System.Data.SqlDbType.VarChar,50),
8 // new System.Data.SqlClient.SqlParameter("@C",System.Data.SqlDbType.VarChar,50)
9 // };
10 //parameter[2].Direction = ParameterDirection.Input;
11 // parameter[0].Direction = ParameterDirection.ReturnValue;
12 // parameter[1].Direction = ParameterDirection.Output;
13 comm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@A", System.Data.SqlDbType.Int, 4));
14 comm.Parameters["@A"].Direction = ParameterDirection.ReturnValue;
15 comm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@B", System.Data.SqlDbType.VarChar, 50));
16 comm.Parameters["@B"].Direction = ParameterDirection.Output;
17 comm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@C", System.Data.SqlDbType.VarChar, 50));
18 comm.Parameters["@C"].Value = "insertmsg";
19 conn.Open();
20 int i = comm.ExecuteNonQuery();
21 string result1 = comm.Parameters["@A"].Value.ToString();
22 string result2 = comm.Parameters["@B"].Value.ToString();
23 conn.Close();
結果為: result1=1000;result2=insertmsgResult
另外還要主要output中如果返回字符串時候,一定需要指定字符串的長度,否則返回的時候就只返回首字符,寫成下面的形式
或者指定其長度
new SqlParameter("@TableName",SqlDbType.VarChar,500,ParameterDirection.Output,false,0,0,"TableName",DataRowVersion.Default,pTable),
其中測試的語句如下
View Code
 1 public void TestOutput(out  string pTable, out int pPageIndex, out int pTotalPage)
2 {
3 pTable = string.Empty;
4 pPageIndex = 0;
5 pTotalPage = 0;
6 string procedureName = "up_PageOutput";
7 System.Collections.Hashtable result = new System.Collections.Hashtable();
8 //
9 try
10 {
11 using (SqlConnection connection = new SqlConnection(SqlHelper.SqlHelper.ConnectionStringLocalTransaction))
12 {
13 connection.Open();
14 if (connection.State != ConnectionState.Open)
15 {
16 connection.Open();
17 }
18 using (SqlCommand cmd = new SqlCommand(procedureName, connection))
19 {
20 // 注意這里要把CommandType設為StoredProcedure解析為存儲過程
21 // 也可默認為Text 以SQL語句模式解析,這樣調用存儲過程就要用SQL語句 EXEC <存儲過程名> <參數...> 寫 SQL 語句調用
22 cmd.CommandType = CommandType.StoredProcedure;
23 cmd.CommandTimeout = 60;
24 cmd.Parameters.AddRange(new SqlParameter[]
25 {
26 new SqlParameter("@TableName",SqlDbType.VarChar,500,ParameterDirection.Output,false,0,0,"TableName",DataRowVersion.Default,pTable),
27 //new SqlParameter("@pageIndex", SqlDbType.Int,pPageIndex),
28 new SqlParameter("@pageIndex",pPageIndex),
29 //new SqlParameter("@TotalPage", SqlDbType.Int,pTotalRecord)
30 new SqlParameter("@TotalPage",pTotalPage)
31 });
32 cmd.Parameters["@TableName"].Direction = ParameterDirection.Output;
33 cmd.Parameters["@pageIndex"].Direction = ParameterDirection.Output;
34 cmd.Parameters["@TotalPage"].Direction = ParameterDirection.Output;
35 cmd.Parameters.Add(new SqlParameter("@retrunValue", SqlDbType.VarChar, 500));
36 cmd.Parameters["@retrunValue"].Direction = ParameterDirection.ReturnValue;
37 object hang = cmd.ExecuteNonQuery();
38 foreach (SqlParameter param in cmd.Parameters)
39 {
40 // 這里把輸出參數放到一個 HashTable 里面,方便取出
41 if (param.Direction == ParameterDirection.Output || param.Direction == ParameterDirection.InputOutput || param.Direction == ParameterDirection.ReturnValue)
42 {
43 result.Add(param.ParameterName, param.Value);
44 }
45 }
46 //pTotalRecord = SqlHelper.SqlHelper.ExecuteNonQuery(SqlHelper.SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, procedureName, param);
47 object retValue1 = cmd.Parameters["@TotalPage"].Value;
48 //pTotalPage = Convert.ToInt32(retValue1);
49 object retValue2 = cmd.Parameters["@pageIndex"].Value;
50 object retValue3 = cmd.Parameters["@TableName"].Value;
51 object retValue4 = cmd.Parameters["@retrunValue"].Value;
52
53 }
54
55 connection.Close();
56 }
57 }
58 catch (Exception)
59 {
60
61 }
62 }
其中幾個關鍵主要設置參數的方式和和取得返回值的方式。

轉載于:https://www.cnblogs.com/huangyuanfengxue/archive/2012/02/29/2373339.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274981.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274981.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274981.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

1個月增長15000 star,zx 庫寫shell腳本真不錯~

大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。今天來討論一個牛逼…

灰色邊框陰影_50種暗模式灰色陰影

灰色邊框陰影If you’re an avid dark mode user like me, you’ll know that dark mode isn’t just about white text on black backgrounds. In a single app, a handful of shades of gray give the app some depth. And across various apps, the spectrum of gray become…

Android源代碼下載

為什么80%的碼農都做不了架構師&#xff1f;>>> Android代碼使用git管理, 所以關于Android源碼下載一般來說要安裝git. 本文是講述只使用Eclipse完成Android源碼下載和關聯. 下載Eclipse,目前最新版本是Juno,自帶了EGit插件-->Eclipse Git插件 那么可以使用EGit…

關于nginx調轉404錯誤頁面

在server{}模塊添加如下&#xff1a; error_page 404 /404.html; location /404.html { root /var/www/html/sina/; #“404目錄地址” }轉載于:https://www.cnblogs.com/alang85/archive/2012/03/01/404_error.html

尤雨溪:Vue 3 將成為新的默認版本

過完年&#xff0c;大年初七&#xff0c;Vue3 將成為默認版本&#xff0c;這時感覺我之前寫的幾篇文章都可以抽空更新一版了。尤雨溪推薦神器 ni &#xff0c;能替代 npm/yarn/pnpm &#xff1f;簡單好用&#xff01;源碼揭秘&#xff01;Vue 3.2 發布了&#xff0c;那尤雨溪是…

shell編程系列20--文本處理三劍客之awk常用選項

shell編程系列20--文本處理三劍客之awk常用選項awk選項總結選項 解釋 -v 參數傳遞 -f 指定腳本文件 -F 指定分隔符 -V 查看awk的版本號[rootlocalhost shell]# awk -v num2"$num1" -v var1"$var" BEGIN{print num2,var…

v-charts加載動畫_加載動畫-用戶體驗寫作練習

v-charts加載動畫Many new UX writers often struggle to find the balance between creativity and clarity. You can’t make everything fun/exciting/interesting as it can have an adverse effect on usability. But there are times when you can add a bit of flair.許…

linux 常用命令收集

關機&#xff1a;poweroff&#xff0c;shutdown -h now&#xff0c;init 0重啟&#xff1a;reboot&#xff0c;shutdown -r now&#xff0c;init 6 關閉x-window&#xff1a;init 3啟動x-window&#xff1a;init 5&#xff0c;start x1.終止命令&#xff1a;ctrlc 2.結束輸入…

34歲回顧人生,也怕中年危機!

大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。最近發生一件令人感…

蛋花花APP,APP開發這幾點你要注意了

蛋花花君又來了&#xff0c;這次蛋花花君為大家分享的是APP開發過程中需要注意的幾點。 1、用戶體驗 程序猿蛋花花覺得&#xff0c;APP開發是為客戶來開發的&#xff0c;而不單單是為了老板&#xff0c;畢竟真正使用的是廣大用戶。APP的設計應該從用戶的角度出發&#xff0c;提…

svg動畫制作_制作第一個SVG動畫

svg動畫制作Story of a designer trying to code animations instead of asking a dev to figure it out.一位設計師嘗試編寫動畫代碼而不是要求開發人員弄清楚動畫的故事。 編碼動畫是Webdesign的未來 (Coded animations are the future of Webdesign) Because we have acces…

網站前端設計,從960框架開始

一個網站進入到前端設計階段&#xff0c;第一步肯定是為全站搭建一個統一的&#xff0c;基礎的HTML模型&#xff0c;在這里推薦一下我剛學習的960框架。960是一個CSS框架&#xff0c;你肯定在想&#xff0c;這個世界肯定是瘋了&#xff0c;連CSS都有框架了嗎&#xff0c;沒錯&a…

60+ 實用 React 工具庫,助力你高效開發!

大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。最近看到一些實用的…

2012年12月第二個周末

2019獨角獸企業重金招聘Python工程師標準>>> 這周&#xff0c;裝上了windows版的 Linux版的oracle 熟悉了下SQL*PLUS的編程規則&#xff0c;還有常用的linux命令 看了一本《簡愛》 正在看oracle 轉載于:https://my.oschina.net/u/204616/blog/545513

『C#基礎』調用CMD的一個小工具

由于經常要使用CMD的一些命令&#xff0c;比如查看IP&#xff0c;Ping一個網址之類的。于是就寫了一個調用CMD.exe的小工具。 主要就是實現這樣一個事情&#xff1a;調用CMD.exe然后傳給它我想要執行的命令&#xff0c;最后獲取結果。 界面&#xff1a; 代碼&#xff1a; 主要執…

小姐姐:如何參與大型開源項目-Taro 共建

大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。“本文來自前端程序…

JavaWeb學習總結(十七)——JSP中的九個內置對象

2019獨角獸企業重金招聘Python工程師標準>>> 一、JSP運行原理 每個JSP 頁面在第一次被訪問時&#xff0c;WEB容器都會把請求交給JSP引擎&#xff08;即一個Java程序&#xff09;去處理。JSP引擎先將JSP翻譯成一個_jspServlet(實質上也是一個servlet) &#xff0c;然…

C#網絡編程(異步傳輸字符串) - Part.3[轉自JimmyZhang博客]

源碼下載&#xff1a;http://www.tracefact.net/SourceCode/Network-Part3.rar C#網絡編程(異步傳輸字符串) - Part.3 這篇文章我們將前進一大步&#xff0c;使用異步的方式來對服務端編程&#xff0c;以使它成為一個真正意義上的服務器&#xff1a;可以為多個客戶端的多次請求…

chrome黑暗模式_黑暗模式:如何克服黑暗面

chrome黑暗模式This article has been written by Redmadrobot Design Lab. Translated and reposted with permission by Alconost Inc., professional translation and localization company.本文由 Redmadrobot設計實驗室 撰寫 。 經過 專業翻譯和本地化公司 Alconost Inc.的…

Deco 智能代碼體驗版正式上線啦,快來體驗設計稿一鍵生成代碼~

Deco 是什么&#xff1f;—Deco 智能代碼項目是我們團隊在「前端智能化」方向上的探索&#xff0c;其聚焦設計稿一鍵生成多端代碼這一切入點&#xff0c;實現將 Sketch/Photoshop 等設計稿進行解析并直接生成多端代碼&#xff08;Taro/React/Vue&#xff09;的能力。Deco 可以使…