Exemplo n.º 1
0
        public static SqlParameter[] GetParameterValues <T>(string procedureName, T objInfo)
        {
            DataTable dtProcedureInfo = GetProcedureInfo(procedureName);

            if (dtProcedureInfo.Rows.Count == 0)
            {
                throw new ArgumentException("Không tìm thấy procedure");
            }
            SqlParameter[] sqlParameter = new SqlParameter[dtProcedureInfo.Rows.Count];

            string strFieldName = "";

            Dictionary <string, object> dicProperty = new Dictionary <string, object>();
            Type pType = objInfo.GetType();

            foreach (System.Reflection.PropertyInfo info in pType.GetProperties())
            {
                dicProperty.Add(info.Name, info.GetValue(objInfo, null));
            }

            object objO = new object();

            for (int i = 0; i < dtProcedureInfo.Rows.Count; i++)
            {
                try
                {
                    strFieldName    = dtProcedureInfo.Rows[i]["PARAMETER_NAME"].ToString().Trim();
                    strFieldName    = strFieldName.Replace("@", "");
                    sqlParameter[i] = new SqlParameter(dtProcedureInfo.Rows[i]["PARAMETER_NAME"].ToString(), Null.GetNull(dicProperty[strFieldName], DBNull.Value));
                    if (dtProcedureInfo.Rows[i]["DATA_TYPE"].ToString().Equals("image") == true)
                    {
                        sqlParameter[i].SqlDbType = SqlDbType.Image;
                    }
                }
                catch (Exception ex)
                {
                    throw new System.ArgumentException("Trường gây lỗi: " + strFieldName + " :" + ex.Message);
                }
            }
            return(sqlParameter);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Phương thức tạo đối tượng từ giá trị lấy từ IDataReader
        /// </summary>
        /// <param name="objType">Kiểu đối tượng</param>
        /// <param name="dr">IDataReader</param>
        /// <param name="objProperties">Mảng chứa các thuộc tính của đối tượng</param>
        /// <param name="arrOrdinals">Mảng lưu vị trí các cột</param>
        /// <returns>Trả lại một đối tượng có kiểu Object</returns>
        /// <remarks></remarks>
        private static object CreateObject(Type objType, IDataReader dr, ArrayList objProperties, int[] arrOrdinals)
        {
            object objObject   = Activator.CreateInstance(objType);
            int    intProperty = 0;

            // fill object with values from datareader
            for (intProperty = 0; intProperty <= objProperties.Count - 1; intProperty++)
            {
                if (((PropertyInfo)objProperties[intProperty]).CanWrite)
                {
                    if (arrOrdinals[intProperty] != -1)
                    {
                        if (Convert.IsDBNull(dr.GetValue(arrOrdinals[intProperty])))
                        {
                            // translate Null value
                            ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[intProperty]), null);
                        }
                        else
                        {
                            try
                            {
                                // try implicit conversion first
                                ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, dr.GetValue(arrOrdinals[intProperty]), null);
                                // data types do not match
                            }
                            catch
                            {
                                try
                                {
                                    Type pType = ((PropertyInfo)objProperties[intProperty]).PropertyType;
                                    //need to handle enumeration conversions differently than other base types
                                    if (pType.BaseType.Equals(typeof(System.Enum)))
                                    {
                                        ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, System.Enum.ToObject(pType, dr.GetValue(arrOrdinals[intProperty])), null);
                                    }
                                    else
                                    {
                                        // try explicit conversion
                                        ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Convert.ChangeType(dr.GetValue(arrOrdinals[intProperty]), pType), null);
                                    }
                                }
                                catch
                                {
                                    // error assigning a datareader value to a property
                                    ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[intProperty]), null);
                                }
                            }
                        }
                        // property does not exist in datareader
                    }
                    else
                    {
                        ((PropertyInfo)objProperties[intProperty]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[intProperty]), null);
                    }
                }
            }
            return(objObject);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Hàm này dùng để map các tham số
 /// </summary>
 /// <param name="procedureName"></param>
 /// <param name="sqlParameter"></param>
 /// <param name="parameterValues"></param>
 private static void AssignParameterValues(string procedureName, SqlParameter[] sqlParameter, params object[] parameterValues)
 {
     if (String.IsNullOrEmpty(procedureName) == true)
     {
         throw new ArgumentException("Bạn chưa điền tên procedure");
     }
     if (parameterValues != null && parameterValues.Length > 0)
     {
         DataTable dtProcedureInfo = GetProcedureInfo(procedureName);
         if (dtProcedureInfo.Rows.Count == 0)
         {
             throw new ArgumentException("Không tìm thấy procedure");
         }
         if (dtProcedureInfo.Rows.Count != parameterValues.Length)
         {
             throw new ArgumentException("Không thể map các đối tượng");
         }
         for (int i = 0; i <= dtProcedureInfo.Rows.Count - 1; i++)
         {
             sqlParameter[i] = new SqlParameter(dtProcedureInfo.Rows[i]["PARAMETER_NAME"].ToString(), Null.GetNull(parameterValues[i], DBNull.Value));
             if (dtProcedureInfo.Rows[i]["DATA_TYPE"].ToString().Equals("image") == true)
             {
                 sqlParameter[i].SqlDbType = SqlDbType.Image;
             }
         }
     }
 }