国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

SubSonic3.0使用存儲過程查詢時,不能使用output參數(shù)返回值的問題修改

2019-11-17 01:36:40
字體:
供稿:網(wǎng)友

SubSonic3.0使用存儲過程查詢時,不能使用output參數(shù)返回值的問題修改

  有個群友問SubSonic3.0執(zhí)行存儲過程時能不能使用output參數(shù)返回值,說測試過后獲取不到返回值,早上有些時間所以就嘗試修改了一下

  首先在數(shù)據(jù)庫中創(chuàng)建一個存儲過程

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://m.survivalescaperooms.com/EmptyFS/10         * 修改時間:2014-06-2711         * 修改說明:添加存儲過程說明參數(shù),用于判斷該參數(shù)是否是返回值12         *********************************************/13         public string Direction;14     }

  打開SQLServer.ttinclude模板,找到GetSPParams函數(shù),修改為下面代碼

 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://m.survivalescaperooms.com/EmptyFS/24             * 修改時間:2014-06-2725             * 修改說明:添加存儲過程說明參數(shù),用于判斷該參數(shù)是否是返回值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://m.survivalescaperooms.com/EmptyFS/ 5         /// 修改時間:2014-06-27 6         /// 功能說明:執(zhí)行存儲過程,返回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             //檢查是否是輸出參數(shù)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模板,生成存儲過程函數(shù)

 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 }

  搞定后我們運行執(zhí)行一下這段存儲過程看看有沒有返回我們想要的結(jié)果(1+2=?)

  

  返回結(jié)果是3,正確

版權(quán)聲明:  本文由AllEmpty原創(chuàng)并發(fā)布于博客園,歡迎轉(zhuǎn)載,未經(jīng)本人同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,否則保留追究法律責任的權(quán)利。如有問題,可以通過1654937@qq.com 聯(lián)系我,非常感謝。

  發(fā)表本編內(nèi)容,只要主為了和大家共同學習共同進步,有興趣的朋友可以加加Q群:327360708 ,大家一起探討。

  更多內(nèi)容,敬請觀注博客:http://m.survivalescaperooms.com/EmptyFS/


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 达州市| 乐业县| 黄龙县| 孝昌县| 康乐县| 离岛区| 安丘市| 咸丰县| 平江县| 辽宁省| 阜康市| 建宁县| 大宁县| 渝中区| 茂名市| 大兴区| 资源县| 志丹县| 凭祥市| 永昌县| 奇台县| 湟源县| 彩票| 合肥市| 应城市| 深圳市| 株洲市| 杭锦后旗| 新乐市| 昌宁县| 泌阳县| 曲阳县| 洪江市| 区。| 锦屏县| 枣阳市| 宁阳县| 来凤县| 故城县| 弥勒县| 晋中市|