Sql Server 中存储过程的output return的区别
生活随笔
收集整理的這篇文章主要介紹了
Sql Server 中存储过程的output return的区别
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
看http://zxianf.blog.163.com/blog/static/301207012009114104124969/中片關(guān)于Sql Server中存儲(chǔ)過程output和return值的區(qū)別
在里面有講解,我在自己本機(jī)中測(cè)試的結(jié)果如下,
1:ReturnValue只能返回0,1,-1這樣的數(shù)據(jù),局限性很大?,而在存儲(chǔ)過程中用OutPut參數(shù),可以返回各種類型的數(shù)據(jù),比較靈活方便。
ReturnValue ? 是用來返回錯(cuò)誤碼的,output是指存儲(chǔ)過程傳出參數(shù) ? ? ? 例如????:
@Flag varchar(20) output View Code 1 sql存儲(chǔ)過程: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 //調(diào)用sqlhelper時(shí)這樣寫,單獨(dú)不行: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(); 結(jié)果為: result1=1000;result2=insertmsgResult 另外還要主要output中如果返回字符串時(shí)候,一定需要指定字符串的長(zhǎng)度,否則返回的時(shí)候就只返回首字符,寫成下面的形式 或者指定其長(zhǎng)度 new SqlParameter("@TableName",SqlDbType.VarChar,500,ParameterDirection.Output,false,0,0,"TableName",DataRowVersion.Default,pTable), 其中測(cè)試的語(yǔ)句如下 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設(shè)為StoredProcedure解析為存儲(chǔ)過程
21 // 也可默認(rèn)為Text 以SQL語(yǔ)句模式解析,這樣調(diào)用存儲(chǔ)過程就要用SQL語(yǔ)句 EXEC <存儲(chǔ)過程名> <參數(shù)...> 寫 SQL 語(yǔ)句調(diào)用
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 // 這里把輸出參數(shù)放到一個(gè) 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 } 其中幾個(gè)關(guān)鍵主要設(shè)置參數(shù)的方式和和取得返回值的方式。
轉(zhuǎn)載于:https://www.cnblogs.com/huangyuanfengxue/archive/2012/02/29/2373339.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Sql Server 中存储过程的output return的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器压力测试工具
- 下一篇: SharePoint中的权限体系