/// <summary> /// 创建数据库命令参数 /// </summary> /// <param name="name">参数名</param> /// <param name="value">参数值</param> /// <param name="dbParameterAttribute">参数特性</param> /// <returns>SqlParameter</returns> internal static SqlParameter CreateSqlParameter(string name, object value, DbParameterAttribute dbParameterAttribute) { // 设置参数方向 var sqlParameter = new SqlParameter(name, value) { Direction = dbParameterAttribute.Direction }; // 设置参数数据库类型 if (dbParameterAttribute.DbType != null) { var type = dbParameterAttribute.DbType.GetType(); if (type.IsEnum && typeof(SqlDbType).IsAssignableFrom(type)) { sqlParameter.SqlDbType = (SqlDbType)dbParameterAttribute.DbType; } } // 设置大小,解决NVarchar,Varchar 问题 if (dbParameterAttribute.Size > 0) { sqlParameter.Size = dbParameterAttribute.Size; } return(sqlParameter); }
/// <summary> /// 配置数据库命令参数 /// </summary> /// <param name="name">参数名</param> /// <param name="value">参数值</param> /// <param name="dbParameterAttribute">参数特性</param> /// <returns>DbParameter</returns> internal static DbParameter ConfigureDbParameter(string name, object value, DbParameterAttribute dbParameterAttribute, DbParameter dbParameter) { // 设置参数方向 dbParameter.ParameterName = name; dbParameter.Value = value; dbParameter.Direction = dbParameterAttribute.Direction; // 设置参数数据库类型 if (dbParameterAttribute.DbType != null) { var type = dbParameterAttribute.DbType.GetType(); if (type.IsEnum && typeof(DbType).IsAssignableFrom(type)) { dbParameter.DbType = (DbType)dbParameterAttribute.DbType; } } // 设置大小,解决NVarchar,Varchar 问题 if (dbParameterAttribute.Size > 0) { dbParameter.Size = dbParameterAttribute.Size; } return(dbParameter); }