/// <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)); } } }
/// <summary> /// Set value from database for fields of the object /// </summary> /// <param name="obj"></param> /// <param name="dre"></param> /// <param name="type"></param> private static void SetValue(object obj, PropertyInfo field, IDataReader dre) { // if type is char if (field.PropertyType == typeof(System.Char)) { field.SetValue(obj, DBConvert.ParseDBToChar(dre, field.Name), null); } // if type is string else if (field.PropertyType == typeof(System.String)) { field.SetValue(obj, DBConvert.ParseDBToString(dre, field.Name), null); } // if type is smallint else if (field.PropertyType == typeof(System.Int16)) { field.SetValue(obj, DBConvert.ParseDBToSmallInt(dre, field.Name), null); } // if type is int else if (field.PropertyType == typeof(System.Int32)) { field.SetValue(obj, DBConvert.ParseDBToInt(dre, field.Name), null); } // if type is long else if (field.PropertyType == typeof(System.Int64)) { field.SetValue(obj, DBConvert.ParseDBToLong(dre, field.Name), null); } // if type is decimal else if (field.PropertyType == typeof(System.Decimal)) { field.SetValue(obj, DBConvert.ParseDBToDecimal(dre, field.Name), null); } // if type is double else if (field.PropertyType == typeof(System.Double)) { field.SetValue(obj, DBConvert.ParseDBToDouble(dre, field.Name), null); } // if type is datetime else if (field.PropertyType == typeof(System.DateTime)) { field.SetValue(obj, DBConvert.ParseDBToDateTime(dre, field.Name), null); } }
// get List Of char, varchar, nvarchar columns /* * private static IList GetLengthOfStringColumns(string _tableName) * { * SqlFactory factory = new SqlFactory(); * // Make command * DbCommand cm = factory.MakeCommand(string.Format(@" * SELECT column_name, data_type, character_octet_length * FROM INFORMATION_SCHEMA.COLUMNS * WHERE table_name='{0}' AND data_type IN ('char', 'varchar', 'nvarchar')", _tableName)); * * // execute query * IDataReader dre = factory.ExecuteReader(cm); * * IList _params = new ArrayList(); * * while (dre.Read()) * { * _params.Add(new DGCParameter(DBConvert.ParseDBToString(dre, "column_name"), DbType.String, DBConvert.ParseDBToInt(dre, "character_octet_length"))); * } * dre.Close(); * * return _params; * * } */ private static IList GetLengthOfStringColumns(string _tableName) { IFactory factory = DBHelper.CreateFactory(); // Make command string query = string.Empty; switch (ConfigurationManager.AppSettings["Database"]) { case "SQLServer": query = string.Format(@" SELECT column_name, data_type, character_octet_length FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name='{0}' AND data_type IN ('char', 'varchar', 'nvarchar')", _tableName); break; case "MySQL": query = string.Format(@" SELECT * FROM information_schema.columns WHERE table_name = '{0}' AND data_type IN ('char','varchar')", _tableName.ToLower()); break; } DbCommand cm = factory.MakeCommand(query); // execute query IDataReader dre = factory.ExecuteReader(cm); IList _params = new ArrayList(); while (dre.Read()) { _params.Add(new DGCParameter(DBConvert.ParseDBToString(dre, "column_name"), DbType.String, DBConvert.ParseDBToInt(dre, "character_octet_length"))); } dre.Close(); return(_params); }