public static void PrepareParametersUpdate(DbCommand cm, object currentObj, object expectedObj, string[] primaryKeyNames)
        {
            //set parameter for current primary key name
            // if the filed is primary key name, dont add param
            foreach (string primaryKeyName in primaryKeyNames)
            {
                FieldInfo field = currentObj.GetType().GetField(primaryKeyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                AddParamUpdate(cm, field.FieldType, "Current" + field.Name, currentObj, field.GetValue(currentObj));
            }

            // set parameter for new values
            FieldInfo[] myFieldInfo;

            //get Type of object
            Type myType = expectedObj.GetType();

            // Get the type and fields of FieldInfoClass.
            myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
                                           | BindingFlags.Public);

            for (int i = 0; i < myFieldInfo.Length; i++)
            {
                //get type of field
                Type type = myFieldInfo[i].FieldType;

                if (myFieldInfo[i].Name.IndexOf("Desc") < 0)
                {
                    // convert value to DB value
                    object value = DBConvert.ParseToDBValue(myFieldInfo[i].GetValue(expectedObj));

                    //set paramname
                    AddParamUpdate(cm, type, myFieldInfo[i].Name, currentObj, value);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Set value from database for fields of the object
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="dre"></param>
        /// <param name="type"></param>
        public static void SetValueForObject(object obj, IDataReader dre, FieldInfo[] myFieldInfo)
        {
            for (int i = 0; i < myFieldInfo.Length; i++)
            {
                //get type of field
                Type   type      = myFieldInfo[i].FieldType;
                string paramName = myFieldInfo[i].Name;

                // convert value to DB value
                object value = DBConvert.ParseToDBValue(myFieldInfo[i].GetValue(obj));

                // if type is char
                if (type == typeof(System.Char))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToChar(dre, paramName));
                }
                // if type is string
                else if (type == typeof(System.String))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToString(dre, paramName));
                }
                // if type is smallint
                else if (type == typeof(System.Int16))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToSmallInt(dre, paramName));
                }
                // if type is int
                else if (type == typeof(System.Int32))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToInt(dre, paramName));
                }
                // if type is long
                else if (type == typeof(System.Int64))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToLong(dre, paramName));
                }
                // if type is decimal
                else if (type == typeof(System.Decimal))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToDecimal(dre, paramName));
                }
                // if type is double
                else if (type == typeof(System.Double))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToDouble(dre, paramName));
                }
                // if type is datetime
                else if (type == typeof(System.DateTime))
                {
                    myFieldInfo[i].SetValue(obj, DBConvert.ParseDBToDateTime(dre, paramName));
                }
            }
        }
        // make parameters
        public static void PrepareParametersDelete(IDbCommand cm, object obj, string[] primaryKeyNames)
        {
            FieldInfo[] myFieldInfo;

            //get Type of object
            Type myType = obj.GetType();

            // Get the type and fields of FieldInfoClass.
            myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
                                           | BindingFlags.Public);

            // if exist any parameters
            if (primaryKeyNames != null)
            {
                foreach (string primaryKeyName in primaryKeyNames)
                {
                    FieldInfo field = myType.GetField(primaryKeyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);

                    //get type of field
                    Type type = field.FieldType;
                    // convert value to DB value
                    object value = DBConvert.ParseToDBValue(field.GetValue(obj));

                    //get paramname
                    string paramName = PARAM_PREFIX + field.Name;

                    // if type is char
                    if (type == typeof(System.Char))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.AnsiStringFixedLength, value);
                    }
                    // if type is string
                    else if (type == typeof(System.String))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.String, value);
                    }
                    // if type is smallint
                    else if (type == typeof(System.Int16))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.Int16, value);
                    }
                    // if type is byte
                    else if (type == typeof(System.Byte))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.Byte, value);
                    }
                    // if type is int
                    else if (type == typeof(System.Int32))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.Int32, value);
                    }
                    // if type is long
                    else if (type == typeof(System.Int64))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.Int64, value);
                    }
                    // if type is decimal
                    else if (type == typeof(System.Decimal))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.Decimal, value);
                    }
                    // if type is double
                    else if (type == typeof(System.Double))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.Double, value);
                    }
                    // if type is datetime
                    else if (type == typeof(System.DateTime))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.DateTime, value);
                    }
                }
            }
        }
        // make parameters
        public static void PrepareParametersLoad(IDbCommand cm, object obj, string[] primaryKeyNames)
        {
            FieldInfo[] myFieldInfo;

            //get Type of object
            Type myType = obj.GetType();

            // Get the type and fields of FieldInfoClass.
            myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
                                           | BindingFlags.Public);

            if (primaryKeyNames != null)
            {
                foreach (string primaryKey in primaryKeyNames)
                {
                    for (int i = 0; i < myFieldInfo.Length; i++)
                    {
                        //get paramname
                        string paramName = PARAM_PREFIX + myFieldInfo[i].Name;

                        // if( primaryKey is equal fieldname)
                        if (primaryKey.Equals(myFieldInfo[i].Name, StringComparison.OrdinalIgnoreCase))
                        {
                            //get type of field
                            Type type = myFieldInfo[i].FieldType;

                            // convert value to DB value
                            object value = DBConvert.ParseToDBValue(myFieldInfo[i].GetValue(obj));

                            // if type is char
                            if (type == typeof(System.Char))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.AnsiStringFixedLength, value);
                            }
                            // if type is string
                            else if (type == typeof(System.String))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.String, value);
                            }
                            // if type is byte
                            else if (type == typeof(System.Byte))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.Byte, value);
                            }
                            // if type is smallint
                            else if (type == typeof(System.Int16))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.Int16, value);
                            }
                            // if type is int
                            else if (type == typeof(System.Int32))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.Int32, value);
                            }
                            // if type is long
                            else if (type == typeof(System.Int64))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.Int64, value);
                            }
                            // if type is decimal
                            else if (type == typeof(System.Decimal))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.Decimal, value);
                            }
                            // if type is double
                            else if (type == typeof(System.Double))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.Double, value);
                            }
                            // if type is datetime
                            else if (type == typeof(System.DateTime))
                            {
                                DGCDataParameter.AddParameter(cm, paramName, DbType.DateTime, value);
                            }
                            break;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// For Max val filed By Identity
        /// </summary>
        /// <param name="cm"></param>
        /// <param name="obj"></param>
        public static void PrepareParametersInsertWithMaxField(DbCommand cm, object obj, string fieldMax)
        {
            // get length of string columns from database
            IList stringColumns = GetLengthOfStringColumns(obj.GetType().Name);

            FieldInfo[] myFieldInfo;

            //get Type of object
            Type myType = obj.GetType();

            // Get the type and fields of FieldInfoClass.
            myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
                                           | BindingFlags.Public);

            for (int i = 0; i < myFieldInfo.Length; i++)
            {
                //get type of field
                Type type = myFieldInfo[i].FieldType;

                // if this field not have minvalue of long or int, add parameter
                if (!((myFieldInfo[i].FieldType == typeof(int) && (int)myFieldInfo[i].GetValue(obj) == int.MinValue) ||
                      (myFieldInfo[i].FieldType == typeof(byte) && (byte)myFieldInfo[i].GetValue(obj) == byte.MinValue) ||
                      (myFieldInfo[i].FieldType == typeof(long) && (long)myFieldInfo[i].GetValue(obj) == long.MinValue) ||
                      (myFieldInfo[i].Name.EndsWith("Desc"))))
                {
                    // convert value to DB value
                    object value = DBConvert.ParseToDBValue(myFieldInfo[i].GetValue(obj));

                    //get paramname
                    string paramName = PARAM_PREFIX + myFieldInfo[i].Name;

                    // if type is char
                    if (type == typeof(System.Char))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.AnsiStringFixedLength, LengthOfStringColumns(stringColumns, myFieldInfo[i].Name), value);
                    }
                    // if type is string
                    else if (type == typeof(System.String))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.String, LengthOfStringColumns(stringColumns, myFieldInfo[i].Name), value);
                    }
                    // if type is byte
                    else if (type == typeof(System.Byte))
                    {
                        DGCDataParameter.AddParameter(cm, paramName, DbType.Byte, value);
                    }
                    // if type is smallint
                    else if (type == typeof(System.Int16))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.Int16, value);
                    }
                    // if type is int
                    else if (type == typeof(System.Int32))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.Int32, value);
                    }
                    // if type is long
                    else if (type == typeof(System.Int64))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.Int64, value);
                    }
                    // if type is decimal
                    else if (type == typeof(System.Decimal))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.Decimal, value);
                    }
                    // if type is double
                    else if (type == typeof(System.Double))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.Double, value);
                    }
                    // if type is datetime
                    else if (type == typeof(System.DateTime))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.DateTime, value);
                    }
                    // if type is datetime
                    else if (type == typeof(System.Boolean))
                    {
                        DBHelper.AddParameter(cm, paramName, DbType.Boolean, value);
                    }
                }
            }
        }