大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > C#一步一步开发自己的自动代码生成工具之二:SQLServer字段与C#变量的对应转化

C#一步一步开发自己的自动代码生成工具之二:SQLServer字段与C#变量的对应转化

关键词:自动代码生成工具C#  阅读(741) 赞(48)

[摘要]本文主要介绍如何使用C#一步一步开发自己的自动代码生成工具之二:SQLServer字段与C#变量的对应转化,与大家分享学习。

第二步:数据表的预处理  

<strong>        /// <summary>
        /// 根据变量类型获取从数据库字段转换到该类型的转换语句
        /// </summary>
        /// <param name="Field">
        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));
            }
        }
        

        /// <summary>
        /// 初始化数据库实体类属性默认值
        /// </summary>
        /// <param name="field">数据库实体类属性
        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));
            }
        }

        /// <summary>
        /// 获得数据访问参数的类型
        /// </summary>
        /// <param name="DbType">
        /// <returns></returns>
        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));
            }
        }

        /// <summary>
        /// 获得属性变量类型
        /// </summary>
        /// <param name="DbType">
        /// <returns></returns>
        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));
            }
        }</strong>













相关评论