/// <summary> /// 创建一个SPOut对象 /// </summary> /// <param name="o">对象值</param> /// <returns>转换后的SPOut对象</returns> public static SPOut Create(object o) { SPOut parameter = new SPOut(); parameter.Value = o; return(parameter); }
//private static IEnumerable<SqlParameter> GetSpParameters(string spName, object parameterObject) //{ // if( parameterObject == null ) // return null; // string connectionString = ConnectionScope.GetDefaultConnectionString(); // SqlParameter[] parameters = ParameterCache.GetSpParameters(connectionString, spName); // PropertyInfo[] properties = parameterObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); // foreach( PropertyInfo property in properties ) { // string name = "@" + property.Name; // SqlParameter para = parameters.Where(p => p.ParameterName == name).FirstOrDefault(); // if (para != null) // { // object value = property.FastGetValue(parameterObject); // if (value == null) // { // para.Value = DBNull.Value; // } // else // { // para.Value = value; // } // } // } // return parameters; //} internal static SqlParameter[] GetSpParameters(object parameterObject) { if (parameterObject == null) { return(null); } PropertyInfo[] properties = parameterObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); SqlParameter[] parameters = new SqlParameter[properties.Length]; int index = 0; foreach (PropertyInfo property in properties) { string name = "@" + property.Name; object value = property.FastGetValue(parameterObject); SqlParameter para = null; if (value == null || value == DBNull.Value) { //throw new ArgumentException("输入参数的属性值不能为空。"); para = new SqlParameter(name, DBNull.Value); //para.SqlDbType = SqlDbType.Variant; } else { SPOut spOutParam = value as SPOut; if (spOutParam != null) { value = spOutParam.Value; if (value == null || value == DBNull.Value) { throw new ArgumentException("输出参数的属性值不允许为空。"); } else { //对于字符串类型的输出参数,长度需要默认设置为-1.这样避免了ADO.NET做长度推算. if (spOutParam.Value is string) { para = new SqlParameter(name, spOutParam.Value); para.Size = -1; } else { para = new SqlParameter(name, spOutParam.Value); } if (spOutParam.Size != -1) { para.Size = spOutParam.Size; } } para.Direction = ParameterDirection.Output; } else { //支持匿名对象中直接使用SqlParameter SqlParameter parameter = value as SqlParameter; if (parameter != null) { para = parameter; } else { para = new SqlParameter(name, value); } } } parameters[index] = para; index++; } return(parameters); }