利用动态代理实现通用存储过程的调用

本文介绍了一种使用Castle DynamicProxy简化ADO.NET存储过程调用的方法,通过定义接口及其实现来减少冗余代码,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   通常情况下我们利用ADO.NET调用存储过程往往要写上好多代码,特别存储过程是参数很多的话很容易出错,而且很繁琐,看看下面这段调用存储过程的代码:
ContractedBlock.gif ExpandedBlockStart.gif
None.gif  public static void au_UpdateU_jbxxi(
None.gif                    
int xs_id, 
None.gif                    
object xj_bhao, 
None.gif                    
string xs_xming, 
None.gif                    
string xbie, 
None.gif                    System.DateTime sri, 
None.gif                    
int bj_ID, 
None.gif                    
string mzhu, 
None.gif                    
object rxxxi, 
None.gif                    
object rxcji, 
None.gif                    
object tcahao, 
None.gif                    
object tpian, 
None.gif                    
string jtdhua, 
None.gif                    
object paytel, 
None.gif                    
object ybian, 
None.gif                    
string dzhi, 
None.gif                    
object qtlxi, 
None.gif                    
object xsjkuang, 
None.gif                    
object fCard, 
None.gif                    
object stuID, 
None.gif                    
object entrYear, 
None.gif                    
object schoolingLength, 
None.gif                    
object fSyncState)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
// 创建一个连接dot.gif
InBlock.gif
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(EnterpriseObjects.EnterpriseApplication.Application.ConnectionString);
InBlock.gif            connection.Open();
InBlock.gif            
// 创建一个命令dot.gif
InBlock.gif
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("au_UpdateU_jbxxi", connection);
InBlock.gif            command.CommandType 
= System.Data.CommandType.StoredProcedure;
InBlock.gif            
// 参数dot.gif
InBlock.gif
            System.Data.SqlClient.SqlParameter xs_idParam = command.Parameters.Add("@xs_id", System.Data.SqlDbType.Int);
InBlock.gif            xs_idParam.Value 
= xs_id;
InBlock.gif            System.Data.SqlClient.SqlParameter xj_bhaoParam 
= command.Parameters.Add("@xj_bhao", System.Data.SqlDbType.VarChar, 30);
InBlock.gif            
if ((xj_bhao == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                xj_bhaoParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                xj_bhaoParam.Value 
= xj_bhao;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter xs_xmingParam 
= command.Parameters.Add("@xs_xming", System.Data.SqlDbType.VarChar, 30);
InBlock.gif            xs_xmingParam.Value 
= xs_xming;
InBlock.gif            System.Data.SqlClient.SqlParameter xbieParam 
= command.Parameters.Add("@xbie", System.Data.SqlDbType.VarChar, 2);
InBlock.gif            xbieParam.Value 
= xbie;
InBlock.gif            System.Data.SqlClient.SqlParameter sriParam 
= command.Parameters.Add("@sri", System.Data.SqlDbType.DateTime);
InBlock.gif            sriParam.Value 
= sri;
InBlock.gif            System.Data.SqlClient.SqlParameter bj_IDParam 
= command.Parameters.Add("@bj_ID", System.Data.SqlDbType.Int);
InBlock.gif            bj_IDParam.Value 
= bj_ID;
InBlock.gif            System.Data.SqlClient.SqlParameter mzhuParam 
= command.Parameters.Add("@mzhu", System.Data.SqlDbType.VarChar, 20);
InBlock.gif            mzhuParam.Value 
= mzhu;
InBlock.gif            System.Data.SqlClient.SqlParameter rxxxiParam 
= command.Parameters.Add("@rxxxi", System.Data.SqlDbType.VarChar, 1000);
InBlock.gif            
if ((rxxxi == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rxxxiParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rxxxiParam.Value 
= rxxxi;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter rxcjiParam 
= command.Parameters.Add("@rxcji", System.Data.SqlDbType.Int);
InBlock.gif            
if ((rxcji == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rxcjiParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rxcjiParam.Value 
= rxcji;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter tcahaoParam 
= command.Parameters.Add("@tcahao", System.Data.SqlDbType.VarChar, 300);
InBlock.gif            
if ((tcahao == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tcahaoParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tcahaoParam.Value 
= tcahao;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter tpianParam 
= command.Parameters.Add("@tpian", System.Data.SqlDbType.VarChar, 50);
InBlock.gif            
if ((tpian == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tpianParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tpianParam.Value 
= tpian;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter jtdhuaParam 
= command.Parameters.Add("@jtdhua", System.Data.SqlDbType.VarChar, 50);
InBlock.gif            jtdhuaParam.Value 
= jtdhua;
InBlock.gif            System.Data.SqlClient.SqlParameter paytelParam 
= command.Parameters.Add("@paytel", System.Data.SqlDbType.VarChar, 50);
InBlock.gif            
if ((paytel == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                paytelParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                paytelParam.Value 
= paytel;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter ybianParam 
= command.Parameters.Add("@ybian", System.Data.SqlDbType.VarChar, 6);
InBlock.gif            
if ((ybian == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ybianParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ybianParam.Value 
= ybian;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter dzhiParam 
= command.Parameters.Add("@dzhi", System.Data.SqlDbType.VarChar, 100);
InBlock.gif            dzhiParam.Value 
= dzhi;
InBlock.gif            System.Data.SqlClient.SqlParameter qtlxiParam 
= command.Parameters.Add("@qtlxi", System.Data.SqlDbType.VarChar, 100);
InBlock.gif            
if ((qtlxi == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                qtlxiParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                qtlxiParam.Value 
= qtlxi;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter xsjkuangParam 
= command.Parameters.Add("@xsjkuang", System.Data.SqlDbType.VarChar, 2000);
InBlock.gif            
if ((xsjkuang == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                xsjkuangParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                xsjkuangParam.Value 
= xsjkuang;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter fCardParam 
= command.Parameters.Add("@fCard", System.Data.SqlDbType.VarChar, 20);
InBlock.gif            
if ((fCard == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fCardParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fCardParam.Value 
= fCard;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter stuIDParam 
= command.Parameters.Add("@stuID", System.Data.SqlDbType.Char, 6);
InBlock.gif            
if ((stuID == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                stuIDParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                stuIDParam.Value 
= stuID;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter entrYearParam 
= command.Parameters.Add("@entrYear", System.Data.SqlDbType.VarChar, 4);
InBlock.gif            
if ((entrYear == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                entrYearParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                entrYearParam.Value 
= entrYear;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter schoolingLengthParam 
= command.Parameters.Add("@schoolingLength", System.Data.SqlDbType.Int);
InBlock.gif            
if ((schoolingLength == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                schoolingLengthParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                schoolingLengthParam.Value 
= schoolingLength;
ExpandedSubBlockEnd.gif            }

InBlock.gif            System.Data.SqlClient.SqlParameter fSyncStateParam 
= command.Parameters.Add("@fSyncState", System.Data.SqlDbType.VarChar, 50);
InBlock.gif            
if ((fSyncState == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fSyncStateParam.Value 
= System.DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fSyncStateParam.Value 
= fSyncState;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
// 执行dot.gif
InBlock.gif
            command.ExecuteNonQuery();
InBlock.gif            
// 清除dot.gif
InBlock.gif
            command.Dispose();
InBlock.gif            connection.Close();
ExpandedBlockEnd.gif        }

None.gif        

      晕,居然这么多,不知道你觉得烦不烦,反正我是很讨厌,很反感了。

      其实利用动态代理,可以解决很多问题,现在先假设我们调用的模式:
  1.定义调用的接口,方法名对应到要调用的存储过程名,参数也与之对应(注:我用的实例数据库是NorthWind):
None.gif      public   interface  ISproces
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        System.Data.DataSet CustOrderHist(
string CustomerID);
InBlock.gif        DataSet CustOrdersDetail(
int OrderID);
InBlock.gif    
InBlock.gif        
//如果储存过程名字和方法名字不同,应该用SpCustomNameAttribute来进行说明
InBlock.gif
        [SpCustomNameAttribute("Employee Sales By Country")]
InBlock.gif        DataSet EmployeeSalesByCountry(DateTime Beginning_Date,DateTime Ending_Date);
ExpandedBlockEnd.gif    }
2.利用Castle的DynamicProxy拦截对接口ISproces的调用,并写自己的拦截类SprocInterceptor,
如果不清楚DynamicProxy请参见园子里的一些优秀的文章: DynamicProxy(动态代理)技术剖析(1)  DynamicProxy(动态代理)技术剖析(2)
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif    
/// 该类负责拦截接口中方法的执行,并调用对应的存储过程
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  SprocInterceptor:StandardInterceptor
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public SqlConnection connection;
InBlock.gif
InBlock.gif        
public SprocInterceptor()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override object Intercept(IInvocation invocation, params object[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MethodInfo method
=invocation.Method;
InBlock.gif            connection.Open();
InBlock.gif            
string methodName="";
InBlock.gif            
object returnObj=null;
InBlock.gif            
if (invocation.Method.IsDefined(typeof(SpCustomNameAttribute),true))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                methodName 
=SpCustomNameAttribute.GetSPName(method);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                methodName
=method.Name;
ExpandedSubBlockEnd.gif            }

InBlock.gif            SqlCommand command 
= new SqlCommand(methodName, connection);
InBlock.gif            command.CommandType 
= System.Data.CommandType.StoredProcedure;
InBlock.gif            ParameterInfo[] paramInfos
=method.GetParameters();
InBlock.gif            
int paramlength=paramInfos.Length;
InBlock.gif            
for(int i=0;i<paramlength;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Type type
=paramInfos[i].ParameterType;
InBlock.gif                SqlDbType sqlType
=ConvertSqlType(type);
InBlock.gif                SqlParameter PageIndexParam 
= command.Parameters.Add("@"+paramInfos[i].Name, sqlType);
InBlock.gif                PageIndexParam.Value 
= args[i];
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(method.ReturnType==typeof(void))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 执行dot.gif
InBlock.gif
                command.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if(method.ReturnType==typeof(DataSet))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 取出数据集dot.gif
InBlock.gif
                SqlDataAdapter adapter = new SqlDataAdapter(command);
InBlock.gif                DataSet dataset 
= new DataSet();
InBlock.gif                adapter.Fill(dataset);
InBlock.gif                adapter.Dispose();
InBlock.gif                returnObj
=dataset;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlParameter returnValueParam 
= command.Parameters.Add("@returnValueParam",ConvertSqlType(method.ReturnType));
InBlock.gif                returnValueParam.Direction 
= System.Data.ParameterDirection.ReturnValue;
InBlock.gif                
// 执行dot.gif
InBlock.gif
                command.ExecuteNonQuery();
InBlock.gif                returnObj
=Convert.ChangeType(returnValueParam.Value,method.ReturnType);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
// 清除dot.gif
InBlock.gif
            command.Dispose();
InBlock.gif            connection.Close();
InBlock.gif            
return returnObj;
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}


      该类继承自StandardInterceptor,并重写了Intercept方法实现对调用的方法的拦截,invocation得到调用的方法名,返回值,参数名,参数的类型而params object[] args参数对应的数据,得到这些数据后,我们便可以很轻松的构造对存储过程的ADO.NET调用的代码了,同时区分处理返回值和void的情况。
     值得注意的是:SqlParameter param = command.Parameters.Add("@"+paramInfos[i].Name, sqlType);这个sqlType是SQL server对应的数据类型的枚举,所以这里需要一个映射,使.net的Type转换到SqlDbType,很简单:

 1 None.gif
 2 ExpandedBlockStart.gifContractedBlock.gif         /**/ /// <summary>
 3InBlock.gif        /// 转化类型到SQL server对应的数据类型
 4InBlock.gif        /// </summary>
 5InBlock.gif        /// <param name="type"></param>
 6ExpandedBlockEnd.gif        /// <returns></returns>

 7 None.gif          public   static  SqlDbType ConvertSqlType(Type type) 
 8 ExpandedBlockStart.gifContractedBlock.gif         dot.gif
 9InBlock.gif            if (type.FullName.ToLower() == "system.int64"
10ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
11InBlock.gif                return SqlDbType.BigInt; 
12ExpandedSubBlockEnd.gif            }
 
13InBlock.gif            else if (type.FullName.ToLower() == "system.boolean"
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
15InBlock.gif                return SqlDbType.Bit; 
16ExpandedSubBlockEnd.gif            }
 
17InBlock.gif            else if (type.FullName.ToLower() == "system.datetime"
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
19InBlock.gif                return SqlDbType.DateTime; 
20ExpandedSubBlockEnd.gif            }
 
21InBlock.gif            else if (type.FullName.ToLower() == "system.decimal"
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
23InBlock.gif                return SqlDbType.Decimal; 
24ExpandedSubBlockEnd.gif            }
 
25InBlock.gif            else if (type.FullName.ToLower() == "system.double"
26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
27InBlock.gif                return SqlDbType.Float; 
28ExpandedSubBlockEnd.gif            }
 
29InBlock.gif            else if (type.FullName.ToLower() == "system.int32"
30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
31InBlock.gif                return SqlDbType.Int; 
32ExpandedSubBlockEnd.gif            }
 
33InBlock.gif            else if (type.FullName.ToLower() == "system.single"
34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
35InBlock.gif                return SqlDbType.Real; 
36ExpandedSubBlockEnd.gif            }
 
37InBlock.gif            else if (type.FullName.ToLower() == "system.int16"
38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
39InBlock.gif                return SqlDbType.SmallInt; 
40ExpandedSubBlockEnd.gif            }
 
41InBlock.gif            else if (type.FullName.ToLower() == "system.byte"
42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
43InBlock.gif                return SqlDbType.TinyInt; 
44ExpandedSubBlockEnd.gif            }
 
45InBlock.gif            else if (type.FullName.ToLower() == "system.guid"
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
47InBlock.gif                return SqlDbType.UniqueIdentifier; 
48ExpandedSubBlockEnd.gif            }
 
49InBlock.gif            else if (type.FullName.ToLower() == "system.byte()"
50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
51InBlock.gif                return SqlDbType.VarBinary; 
52ExpandedSubBlockEnd.gif            }
 
53InBlock.gif            else if (type.FullName.ToLower() == "system.string"
54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
55InBlock.gif                return SqlDbType.VarChar; 
56ExpandedSubBlockEnd.gif            }
 
57InBlock.gif            else if (type.FullName.ToLower() == "system.object"
58ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
59InBlock.gif                return SqlDbType.Variant; 
60ExpandedSubBlockEnd.gif            }
 
61InBlock.gif            else 
62ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
63InBlock.gif                throw new ArgumentOutOfRangeException(); 
64ExpandedSubBlockEnd.gif            }
 
65ExpandedBlockEnd.gif        }

3.需要给定义了存储过程方法的接口创建代理,使得拦截器去拦截其中的方法:

ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// InvokeSP 的摘要说明。
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  SpProxy
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//public SqlConnection connection;
InBlock.gif
        public SpProxy()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static object CreatSpObject(Type inerfaceType,SqlConnection connection)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ProxyGenerator _generator 
= new ProxyGenerator(); 
InBlock.gif            GeneratorContext context 
= new GeneratorContext(); 
InBlock.gif            SprocInterceptor interceptor 
= new SprocInterceptor();
InBlock.gif            interceptor.connection
=connection;
InBlock.gif            
object proxy = _generator.CreateCustomProxy(inerfaceType, interceptor,new noUse(), context);
InBlock.gif            
return proxy;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private class noUse
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

4.如何调用呢??肯定有很多朋友都会问的。

None.gif          private   void  button2_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            System.Data.SqlClient.SqlConnection connection
=new System.Data.SqlClient.SqlConnection("Initial Catalog=Northwind;Data Source=(local);Packet Size=4096;user id=sa;password=sa");
InBlock.gif            
object proxy=SpProxy.CreatSpObject(typeof(ISproces),connection);
InBlock.gif            ISproces sp
=proxy as ISproces;
InBlock.gif            DataSet ds
=sp.CustOrdersDetail(10249);
InBlock.gif            dataGrid1.DataSource
=ds.Tables[0];
ExpandedBlockEnd.gif        }

这样就可以对ISporces中定义的方法映射到对应名称的存储过程上去,实现调用。

示例代码下载

转载于:https://www.cnblogs.com/hzuIT/articles/732685.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值