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;
            }
        }
  • 相关阅读:
    git如何从远端获取某个文件
    git显示不出来图标标志
    sublime text3设置
    怎么解决sublime的插件自动被禁用
    外甥语录
    sublime Text3支持vue高亮,sublime Text3格式化Vue
    sass安装方法,绝对好用的方式
    win10 安装nodejs,报错there is a problem in the windows installer package
    npm下载模块提速方法
    npm如何删除node_modules文件夹
  • 原文地址:https://www.cnblogs.com/guolihao/p/2863993.html
Copyright © 2011-2022 走看看