[C#]一步一步開發自己的自動代碼產生工具之二:SQLServer欄位與C#變數的對應轉化

來源:互聯網
上載者:User
 第二步:資料表的預先處理 
        ///         /// 根據變數類型擷取從資料庫欄位轉換到該類型的轉換語句        ///         ///         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));            }        }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.