存储过程动态配直3

45 篇文章 0 订阅
12 篇文章 0 订阅

using System;
using System.Xml ;
using System.Xml.Serialization ;
using System.Xml.XPath ;
using System.Data ;
using System.Data.SqlClient ;
using System.Reflection;

namespace DhzShop.Config
{
 /// <summary>
 /// Procedure 的摘要说明。
 /// </summary>
 public class Procedure
 {
  public Procedure()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  private Command[] _Commands;
  [XmlArray("Commands")]
  public  Command[] Commands
  {
   get
   {
    return this._Commands;}
   set
   {
    this._Commands=value;
   }
  }
  public static  object   Instance(System.Web.HttpContext context)
  {
   System.Xml.XmlDocument  doc=new System.Xml.XmlDocument();
   doc.Load(context.Server.MapPath("Config/Procedure.xml"));
   System.Xml.XmlNodeList nodeList=doc.GetElementsByTagName("Procedure");
   if(nodeList.Count!=1)
    throw new Exception("配置参数出错了!!");
   System.Xml.XmlNode section=nodeList.Item(0);
   XPathNavigator nav = section.CreateNavigator();
   string typename = (string) nav.Evaluate("string(@type)");
   Type t = Type.GetType(typename);
   XmlSerializer ser = new XmlSerializer(t);
   XmlNodeReader xnr=new XmlNodeReader(section);
   
   object obj= ser.Deserialize(xnr);
   return obj;
  }
  public static void   ExecCmdNonQuery(System.Web.HttpContext context,string cmdname,object arg)
  {
   SqlCommand sqlcmd=null;
   Config.Procedure procfg=Config.Procedure.Instance(context) as Config.Procedure ;
   int ks=procfg.Commands.Length ;
   int i=0;
   for(i=0;i<ks;i++)
   {
    if(procfg.Commands[i].CommandText.ToUpper ().Equals(cmdname.ToUpper ()))
    {
     sqlcmd=procfg.Commands[i].toSqlCommand ();
     break;
    }
   }

   using(SqlConnection cnn=Config.SqlConfig.GetSqlConn())
   {
    
    sqlcmd.Connection=cnn;
    Command cmd=procfg.Commands[i];
    bool pout=false;
    if(System.Type.GetType(cmd.ArgType)==arg.GetType())
    {
     ks=cmd.Parameters.Length ;
     for(i=0;i<ks;i++)
     {
      if(cmd.Parameters[i].Direction!=System.Data.ParameterDirection.Input)
      {
       pout=true;
      }
      if(cmd.Parameters[i].Direction==System.Data.ParameterDirection.ReturnValue||cmd.Parameters[i].Direction==System.Data.ParameterDirection.Output)
      sqlcmd.Parameters[cmd.Parameters[i].ParameterName].Value=Config.Procedure.GetObjectPropertyValue(arg,cmd.Parameters[i].MapFld);
     }
     cnn.Open ();
     sqlcmd.ExecuteNonQuery();
     cnn.Close ();
     if(pout)
     {
      for(i=0;i<ks;i++)
      {
       switch(cmd.Parameters[i].Direction)
       {
        case ParameterDirection.Output:
        case ParameterDirection.InputOutput:
        case ParameterDirection.ReturnValue:
         Config.Procedure.SetObjectPropertyVale(arg,cmd.Parameters[i].MapFld,new object[]{sqlcmd.Parameters[cmd.Parameters[i].ParameterName].Value});
         break;
        default:
         break;
       }
      }
     }
    }
   }
  }
  public static object GetObjectPropertyValue(object instance, string propertyName)
  {
   Type t = instance.GetType();
   return t.InvokeMember(propertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,null, instance, null);
   
  }
  public static void SetObjectPropertyVale(object instance,string propertyName,object[]   arg )
  {
           Type t = instance.GetType();
   t.InvokeMember(propertyName,BindingFlags.SetProperty|BindingFlags.Public|BindingFlags.Instance,null,instance,arg);
    
  }
   
 }
 
}

///

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER  PROCEDURE [addProIndex]
 (@SortID  [int]    output ,
  @SortName  [varchar](100),
  @SortCls  [tinyint],
  @SortParent [int])

AS
Begin
 INSERT INTO [ProIndex]
  (
  [SortName],
  [SortDate],
  [SortCls],
  [SortParent])
 
VALUES
 (
  @SortName,
  getdate(),
  @SortCls,
  @SortParent)
Select @SortID=IDENT_CURRENT('proIndex')
End

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

///测试代码

DhzShop.Entity.ProIndex pro=new DhzShop.Entity.ProIndex();
   pro.Name="DHZ7ST";
   pro.Parent=0;
   pro.Date=DateTime.Now ;
   pro.Cls=0;
   pro.ID=0;
   Config.Procedure.ExecCmdNonQuery(this.Context,"addProIndex",pro);
   Response.Write(pro.ID);
        

//

<?xml version="1.0" encoding="utf-8" ?>
<Procedure type="DhzShop.Config.Procedure,DhzShop">
     <Commands>
        <Command CommandText="addProIndex" ArgType="DhzShop.Entity.ProIndex,DhzShop">
    <Parameters>
     <Pm MapFld="ID" ParameterName="@SortID" Direction="Output" SqlDbType="Int" Size="0"></Pm>
     <Pm MapFld="Name" ParameterName="@SortName" Direction="Input" SqlDbType="VarChar" Size="100"></Pm>
     <Pm MapFld="Cls" ParameterName="@SortCls" Direction="Input" SqlDbType="TinyInt" Size="0"></Pm>
     <Pm MapFld="Parent" ParameterName="@SortParent" Direction="Input" SqlDbType="Int" Size="0"></Pm>
    </Parameters>
        </Command>
     </Commands>
</Procedure>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值