zoukankan      html  css  js  c++  java
  • 记录一下动态生sqlParameter[]

    生成SqlParameter[]

     public class DbParameterGenerator
        {
            /// <summary>
            /// 根据方法的参数获取sqlparameter
            /// </summary>
            /// <param name="method">方法对像</param>
            /// <param name="values">参数对应的值</param>
            /// <returns></returns>
            public static System.Data.SqlClient.SqlParameter[] GetParameterGenerator(System.Reflection.MethodInfo method, object[] values)
            {
    
                if (method == null)
                {
                    method = (MethodInfo)new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();
                }
                ParameterInfo[] param = method.GetParameters();
                SqlParameter[] sqlParam = new SqlParameter[values.Length];
                int count = 0;
                for (int i = 0; i < param.Length; i++)
                {
                    if (Attribute.IsDefined(param[i], typeof(NoDataFiledAttribute)))
                    {
                        continue;
                    }
                    DataFiledAttribute d = Attribute.GetCustomAttribute(param[i], typeof(DataFiledAttribute)) as DataFiledAttribute;
                    SqlParameter sparam = null;
                    if (d.FieldName.StartsWith("@"))
                    {
                        sparam = new SqlParameter(d.FieldName, values[count]);
                    }
                    else
                    {
                        sparam = new SqlParameter("@" + d.FieldName, values[count]);
    
                    }
                    sqlParam[count] = sparam;
                    count++;
                }
                return sqlParam;
            }
    
        }

    test类:

     public class test
        {
            public static SqlParameter[] TestParam([DataFiled("id", SqlDbType.Int)] int id, [NoDataFiled] int userID)
            {
                SqlParameter[] param = DbParameterGenerator.GetParameterGenerator(MethodInfo.GetCurrentMethod() as MethodInfo, new object[] { id });
                return param;
            }
        }
  • 相关阅读:
    monkeyrunner 进行多设备UI测试
    python Pool并行执行
    python 字符串函数
    python Map()和reduce()函数
    python re模块使用
    3.6 C++继承机制下的构造函数
    3.5 C++间接继承
    3.4 C++名字隐藏
    3.3 C++改变基类成员在派生类中的访问属性
    3.2 C++继承方式
  • 原文地址:https://www.cnblogs.com/guolihao/p/2863993.html
Copyright © 2011-2022 走看看