有個群友問SubSonic3.0執行存儲過程時能不能使用output參數返回值,說測試過后獲取不到返回值,早上有些時間所以就嘗試修改了一下
首先在數據庫中創建一個存儲過程
1 CREATE PROCEDURE [OutValue]2 @a int,3 @b int, 4 @c int output5 AS6 Set @c = @a + @b7 GO
打開Settings.ttinclude模板,找到SPParam類,修改為下面代碼
1 public class SPParam{ 2 public string Name; 3 public string CleanName; 4 public string SysType; 5 public string DbType; 6 /* 7 * 修 改 人:Empty(AllEmpty) 8 * QQ 群:327360708 9 * 博客地址:http://www.49028c.com/EmptyFS/10 * 修改時間:2014-06-2711 * 修改說明:添加存儲過程說明參數,用于判斷該參數是否是返回值12 *********************************************/13 public string Direction;14 }
打開SQLServer.ttinclude模板,找到GetSPParams函數,修改為下面代碼
1 List<SPParam> GetSPParams(string spName){ 2 var result=new List<SPParam>(); 3 string dbName = null; 4 5 if(!String.IsNullOrEmpty(DatabaseName)) 6 dbName = DatabaseName; 7 8 string[] restrictions = new string[4] { dbName , null, spName, null }; 9 10 using(SqlConnection conn=new SqlConnection(ConnectionString)){11 conn.Open();12 var sprocs=conn.GetSchema("ProcedureParameters", restrictions);13 conn.Close();14 foreach(DataRow row in sprocs.Select("", "ORDINAL_POSITION")){15 SPParam p=new SPParam();16 p.SysType=GetSysType(row["DATA_TYPE"].ToString());17 p.DbType=GetDbType(row["DATA_TYPE"].ToString()).ToString();18 p.Name=row["PARAMETER_NAME"].ToString().Replace("@","");19 p.CleanName=CleanUp(p.Name);20 /*21 * 修 改 人:Empty(AllEmpty)22 * QQ 群:32736070823 * 博客地址:http://www.49028c.com/EmptyFS/24 * 修改時間:2014-06-2725 * 修改說明:添加存儲過程說明參數,用于判斷該參數是否是返回值26 *********************************************/27 p.Direction=row["PARAMETER_MODE"].ToString();28 result.Add(p);29 }30 }31 return result;32 }
打開SubSonic3.0源碼:Schema/StoredProcedure.cs,添加下面代碼
1 /// <summary> 2 /// 修 改 人:Empty(AllEmpty) 3 /// QQ 群:327360708 4 /// 博客地址:http://www.49028c.com/EmptyFS/ 5 /// 修改時間:2014-06-27 6 /// 功能說明:執行存儲過程,返回OutputValues 7 /// </summary> 8 public List<object> ExecuteReturnValue() 9 {10 Provider.ExecuteQuery(Command);11 return Command.OutputValues;12 }
如圖:
打開StoredProcedures.tt模板,修改為下面代碼
1 <#@ template language="C#" debug="True" hostspecific="True" #> 2 <#@ output extension=".cs" #> 3 <#@ include file="SQLServer.ttinclude" #> 4 <# 5 var sps = GetSPs(); 6 if(sps.Count>0){ 7 #> 8 using System; 9 using System.Data;10 using SubSonic.Schema;11 using SubSonic.DataProviders;12 13 namespace <#=Namespace#>{14 public partial class SPs{15 16 <# foreach(var sp in sps){#>17 public static StoredProcedure <#=sp.CleanName#>(<#=sp.ArgList#>){18 StoredProcedure sp=new StoredProcedure("<#=sp.Name#>");19 20 <# 21 foreach(var par in sp.Parameters){22 //檢查是否是輸出參數23 if(par.Direction == "INOUT")24 { 25 #>26 sp.Command.AddOutputParameter("<#=par.Name#>",-1,DbType.<#=par.DbType#>);27 <# 28 }29 else30 {31 #>32 sp.Command.AddParameter("<#=par.Name#>",<#=par.CleanName#>,DbType.<#=par.DbType#>);33 <# 34 }35 }36 #>37 return sp;38 }39 <# }#>40 41 }42 43 }44 <# }#>
運行修改好的StoredProcedures.tt模板,生成存儲過程函數
1 using System.Data; 2 using SubSonic.Schema; 3 4 namespace Solution.Dataaccess.DataModel{ 5 public partial class SPs{ 6 7 public static StoredProcedure OutValue(int a,int b,int c){ 8 StoredProcedure sp=new StoredProcedure("OutValue"); 9 10 sp.Command.AddParameter("a",a,DbType.Int32);11 sp.Command.AddParameter("b",b,DbType.Int32);12 sp.Command.AddOutputParameter("c",-1,DbType.Int32);13 return sp;14 }15 }16 17 }
搞定后我們運行執行一下這段存儲過程看看有沒有返回我們想要的結果(1+2=?)
返回結果是3,正確
版權聲明: 本文由AllEmpty原創并發布于博客園,歡迎轉載,未經本人同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,否則保留追究法律責任的權利。如有問題,可以通過1654937@qq.com 聯系我,非常感謝。
發表本編內容,只要主為了和大家共同學習共同進步,有興趣的朋友可以加加Q群:327360708 ,大家一起探討。
更多內容,敬請觀注博客:http://www.49028c.com/EmptyFS/
新聞熱點
疑難解答