Exemple #1
0
        public static DbParameter getParameter(string paramName, object paramValue, Type type)
        {
            DbParameter dbParam = getParameter(paramName);

            if (type == typeof(Int32) || type == typeof(Int32?))
            {
                dbParam.DbType = DbType.Int32;
                if (paramValue != null)
                {
                    dbParam.Value = Convert.ToInt32(paramValue);
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }
            else if (type == typeof(string))
            {
                dbParam.DbType = DbType.String;
                if (paramValue != null)
                {
                    dbParam.Value = paramValue.ToString();
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }
            else if (type == typeof(bool) || type == typeof(bool?))
            {
                dbParam.DbType = DbType.Boolean;
                if (paramValue != null)
                {
                    dbParam.Value = Convert.ToBoolean(paramValue);
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }
            else if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                dbParam.DbType = DbType.DateTime;
                if (paramValue != null)
                {
                    dbParam.Value = Convert.ToDateTime(paramValue);
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }
            else if (type == typeof(decimal) || type == typeof(decimal?))
            {
                dbParam.DbType = DbType.Decimal;
                if (paramValue != null)
                {
                    dbParam.Value = Convert.ToDecimal(paramValue);
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }
            else if (type == typeof(double) || type == typeof(double?))
            {
                dbParam.DbType = DbType.Double;
                if (paramValue != null)
                {
                    dbParam.Value = Convert.ToDouble(paramValue);
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }
            else if (type == typeof(byte[]))
            {
                dbParam.DbType = DbType.Binary;
                if (paramValue != null)
                {
                    dbParam.Value = (byte[])paramValue;
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }
            else if (type.IsClass)
            {
                dbParam.DbType = DbType.Int32;

                if (Attributes.getTableAttribute(type) != null && paramValue != null)
                {
                    object obj2 = paramValue;

                    foreach (PropertyInfo prop2 in type.GetProperties())
                    {
                        ColumnAttribute columnAttribute2 = Attributes.getColumnAttribute(prop2);
                        if (columnAttribute2 != null)
                        {
                            if (columnAttribute2.Identity)
                            {
                                dbParam.Value = Convert.ToInt32(prop2.GetValue(obj2, null));
                                break;
                            }
                        }
                    }
                }
                else
                {
                    dbParam.Value = DBNull.Value;
                }
            }

            return(dbParam);
        }