zoukankan      html  css  js  c++  java
  • SubSonic3.0使用存储过程查询时,不能使用output参数返回值的问题修改

      有个群友问SubSonic3.0执行存储过程时能不能使用output参数返回值,说测试过后获取不到返回值,早上有些时间所以就尝试修改了一下

      首先在数据库中创建一个存储过程

    1 CREATE PROCEDURE [OutValue]
    2     @a int,
    3     @b int, 
    4     @c int output
    5 AS
    6     Set @c = @a + @b
    7 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.cnblogs.com/EmptyFS/
    10         * 修改时间:2014-06-27
    11         * 修改说明:添加存储过程说明参数,用于判断该参数是否是返回值
    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    群:327360708
    23             * 博客地址:http://www.cnblogs.com/EmptyFS/
    24             * 修改时间:2014-06-27
    25             * 修改说明:添加存储过程说明参数,用于判断该参数是否是返回值
    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.cnblogs.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             else
    30             {
    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.cnblogs.com/EmptyFS/

  • 相关阅读:
    jdk环境一键配置
    IIS 浏览aspx页面出现 无法显示 XML 页
    ASP.NET MVC(三)
    ASP.NET MVC(二)
    ASP.NET MVC (一)
    Fatal error: Call to a member function bind_param() on a non-object in
    Eclipse字符集设置方式
    mysql 管理工具
    win7 IIS发布项目遇到的问题
    php 环境的搭建
  • 原文地址:https://www.cnblogs.com/EmptyFS/p/3811604.html
Copyright © 2011-2022 走看看