sqlcommandgenerator类的设计思路就是通过反射得到方法的参数,使用被sqlcommandparameterattribute标记的参数来装配一个command实例. 【程序编程相关:如何创建COM组件能够调用的.NET装配】
sqlcommandgenerator类的设计 【推荐阅读:WinForm的Splitter使用心得】
引用的命名空间:
//sqlcommandgenerator.cs using system; using system.reflection; using system.data; using system.data.sqlclient; using debug = system.diagnostics.debug; using stacktrace = system.diagnostics.stacktrace; 类代码: namespace dataaccess { public sealed class sqlcommandgenerator { //私有构造器,不允许使用无参数的构造器构造一个实例 private sqlcommandgenerator() { throw new notsupportedexception(); } //静态只读字段,定义用于返回值的参数名称 public static readonly string returnvalueparametername = "return_value"; //静态只读字段,用于不带参数的存储过程 public static readonly object[] novalues = new object[] {}; public static sqlcommand generatecommand(sqlconnection connection, methodinfo method, object[] values) { //如果没有指定方法名称,从堆栈帧得到方法名称 if (method == null) method = (methodinfo) (new stacktrace().getframe(1).getmethod()); // 获取方法传进来的sqlcommandmethodattribute // 为了使用该方法来生成一个command对象,要求有这个attribute.... 下一页