第二步:資料表的預先處理
/// /// 根據變數類型擷取從資料庫欄位轉換到該類型的轉換語句 /// /// private static void InitConvertMethod(FieldModel Field) { switch (Field.VarType) { case "string": Field.ToVarConvertMethod = "Convert.ToString"; break; case "int": Field.ToVarConvertMethod = "Convert.ToInt32"; break; case "DateTime": Field.ToVarConvertMethod = "Convert.ToDateTime"; break; case "bool": Field.ToVarConvertMethod = "Convert.ToBoolean"; break; case "float": Field.ToVarConvertMethod = "Convert.ToSingle"; break; case "decimal": Field.ToVarConvertMethod = "Convert.ToDecimal"; break; case "byte[]": Field.ToVarConvertMethod = "(byte[])"; break; case "money": Field.ToVarConvertMethod = "Convert.ToDecimal"; break; default: throw new Exception(string.Format("變數類型({0})的轉換方法未定義!", Field.VarType)); } } /// /// 初始化資料庫實體類屬性預設值 /// /// 資料庫實體類屬性 private static void InitDefaultVarValue(FieldModel Field) { switch (Field.DbType) { case "char": Field.DefaultValueVar = "String.Empty"; break; case "nchar": Field.DefaultValueVar = "String.Empty"; break; case "varchar": Field.DefaultValueVar = "String.Empty"; break; case "nvarchar": Field.DefaultValueVar = "String.Empty"; break; case "text": Field.DefaultValueVar = "String.Empty"; break; case "ntext": Field.DefaultValueVar = "String.Empty"; break; case "int": Field.DefaultValueVar = "0"; break; case "bit": Field.DefaultValueVar = "false"; break; case "datetime": Field.DefaultValueVar = "DateTime.MinValue"; break; case "float": Field.DefaultValueVar = "0"; break; case "decimal": Field.DefaultValueVar = "0"; break; case "image": Field.DefaultValueVar = "null"; break; case "money": Field.DefaultValueVar = "0"; break; default: throw new Exception(string.Format("資料庫欄位類型({0})未定義!", Field.DbType)); } } /// /// 獲得資料訪問參數的類型 /// /// /// private static string GetSqlParamTypeFromSqlDbType(string DbType) { switch (DbType.ToLower()) { case "char": return "SqlDbType.Char"; case "nchar": return "SqlDbType.NChar"; case "varchar": return "SqlDbType.VarChar"; case "nvarchar": return "SqlDbType.NVarChar"; case "text": return "SqlDbType.Text"; case "ntext": return "SqlDbType.NText"; case "int": return "SqlDbType.Int"; case "bit": return "SqlDbType.Bit"; case "datetime": return "SqlDbType.DateTime"; case "float": return "SqlDbType.Float"; case "image": return "SqlDbType.Image"; case "decimal": return "SqlDbType.Decimal"; case "money": return "SqlDbType.Money"; default: throw new Exception(string.Format("資料庫欄位類型({0})未定義!", DbType)); } } /// /// 獲得屬性變數類型 /// /// /// private static string GetVarTypeFromSqlDbType(string DbType) { switch (DbType.ToLower()) { case "char": return "string"; case "nchar": return "string"; case "varchar": return "string"; case "nvarchar": return "string"; case "text": return "string"; case "ntext": return "string"; case "int": return "int"; case "bit": return "bool"; case "datetime": return "DateTime"; case "float": return "float"; case "image": return "byte[]"; case "decimal": return "decimal"; case "money": return "decimal"; default: throw new Exception(string.Format("資料庫欄位類型({0})未定義!", DbType)); } }