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>