文本版|topic 高级搜索
   名人堂 帮助 论坛制度 意见反馈 | 首页 博客 周新贴 专题 求职 读书
RSS 底部
 
社区导航: 专家门诊   网络技术   操作系统   数据库   程序设计   系统应用   考试认证   CIO及信息化   站长交流   综合交流   下载基地  51CTO产品服务 设为首页 | 收藏本站
51CTO技术论坛» .Net » 通过 数据库表 自动生成 实体类       [ 打印]  [ 订阅]  [ 收藏]  [ 推荐给朋友]   [ 本帖文本页]

论坛跳转:
     
标题: [原创] 通过 数据库表 自动生成 实体类  ( 查看:517  回复:7 )   
 
maddish
新新人类  点击可查看详细



十二生肖之猪   天秤座   行业勋章   技术勋章   诚信兄弟  
帖子 20
精华 0
无忧币 838
积分 36
阅读权限 20
注册日期 2007-11-30
最后登录 2008-7-26 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-2-29 12:09   标题:通过 数据库表 自动生成 实体类
上一帖 |
// 获取到所有的用户表.
DataTable userTableName = GetTable( "select name as tablename from sysobjects where xtype = 'U'" );

//根据表名获取所有字段和字段类型
            DataTable myTable =
                GetTable(
                    "select syscolumns.name,systypes.name as type from syscolumns  " +
                    " INNER   JOIN  sysobjects  ON   syscolumns.id  =  sysobjects.id " +
                    "INNER   JOIN   systypes   ON   syscolumns.xtype   =   systypes.xtype " +
                    " WHERE   (sysobjects.name   =   '" + tableName + "')   AND   (systypes.name   <>   'sysname') ");

//创建一个新的表.需要生成的数据
//字段1:修饰符 - 一般为 private
//字段2:修饰符2 - 一般我不写.可以写 statice
//字段3:SQL字段的类型 - 在生成.cs文件前会转换成c#类型
//字段4:生成属性名称 - 与表内字段名一样
            DataTableNew.Columns.Add("xiushifu1", typeof (string));
            DataTableNew.Columns.Add("xiushifu2", typeof (string));
            DataTableNew.Columns.Add("type", typeof (string));
            DataTableNew.Columns.Add("name", typeof (string));

// 进行类型转换
//为生成实体需要的数据添加记录保存
            for (int i = 0; i < myTable.Rows.Count; i++)
            {
                string typeName = ChangeToCSharpType(myTable.Rows["type"].ToString());

                DataTableNew.Rows.Add(new object[] {"private", "", typeName, myTable.Rows["name"]});
            }

//进行类型转换函数
        protected static string ChangeToCSharpType( string type )
        {
            string reval;

            switch( type.ToLower() )
            {
                case "int":
                    reval = "Int32";
                    break;
                case "text":
                    reval = "String";
                    break;
                case "bigint":
                    reval = "Int64";
                    break;
                case "binary":
                    reval = "System.Byte[]";
                    break;
                case "bit":
                    reval = "Boolean";
                    break;
                case "char":
                    reval = "String";
                    break;
                case "datetime":
                    reval = "System.DateTime";
                    break;
                case "decimal":
                    reval = "System.Decimal";
                    break;
                case "float":
                    reval = "System.Double";
                    break;
                case "image":
                    reval = "System.Byte[]";
                    break;
                case "money":
                    reval = "System.Decimal";
                    break;
                case "nchar":
                    reval = "String";
                    break;
                case "ntext":
                    reval = "String";
                    break;
                case "numeric":
                    reval = "System.Decimal";
                    break;
                case "nvarchar":
                    reval = "String";
                    break;
                case "real":
                    reval = "System.Single";
                    break;
                case "smalldatetime":
                    reval = "System.DateTime";
                    break;
                case "smallint":
                    reval = "Int16";
                    break;
                case "smallmoney":
                    reval = "System.Decimal";
                    break;
                case "timestamp":
                    reval = "System.DateTime";
                    break;
                case "tinyint":
                    reval = "System.Byte";
                    break;
                case "uniqueidentifier":
                    reval = "System.Guid";
                    break;
                case "varbinary":
                    reval = "System.Byte[]";
                    break;
                case "varchar":
                    reval = "String";
                    break;
                case "Variant":
                    reval = "Object";
                    break;
                default:
                    reval = "String";
                    break;
            }
            return reval;
        }

//创建文本流并设置编码
TextWriter writer = new StreamWriter(
                      new BufferedStream(
                      new FileStream( fileDesc , FileMode.Create , FileAccess.Write ) ) , System.Text.Encoding.GetEncoding( "gb2312" ) )

// 写入命名空间和类名
                writer.WriteLine( "using System;" );
                writer.WriteLine();

                writer.WriteLine( "namespace " + nameSpace );
                writer.WriteLine( "{" );
                writer.WriteLine( "\tpublic class " + className );
                writer.WriteLine( "\t{" );



//写入所有私有变量
                for( int i = 0 ; i < table.Rows.Count ; i++ )
                {
                    object [ ] row = table.Rows [ i ].ItemArray;
                    writer.Write( "\t\t" );
                    for( int j = 0 ; j < row.Length ; j++ )
                    {
                        writer.Write( row [ j ].Equals( "无" ) ? "" : row [ j ] + ( j == row.Length - 1 ? "" : " " ) );
                    }
                    writer.Write( ";" );
                    writer.WriteLine();
                }

//写入所有属性
                for( int i = 0 ; i < table.Rows.Count ; i++ )
                {
                    writer.Write( "\t\t" );
                    writer.Write( "public " );
                    object [ ] row = table.Rows [ i ].ItemArray;
                    for( int j = 1 ; j < row.Length - 1 ; j++ )
                    {
                        if( row [ j ].Equals( "static" ) )
                        {
                            writer.Write( "static " );
                            continue;
                        }
                        writer.Write( row [ j ].Equals( "无" ) ? "" : row [ j ] + " " );
                    }
                    string attribName = row [ row.Length - 1 ].ToString();
                    writer.Write( attribName.Substring( 0 , 1 ).ToUpper() + attribName.Substring( 1 , attribName.Length - 1 ) );
                    writer.WriteLine();
                    writer.WriteLine( "\t\t{" );
                    writer.WriteLine( "\t\t\tget" );
                    writer.WriteLine( "\t\t\t{" );
                    if( row [ 1 ].Equals( "static" ) )
                    {
                        writer.WriteLine( "\t\t\t\treturn " + attribName + ";" );
                    }
                    else
                    {
                        writer.WriteLine( "\t\t\t\treturn this." + attribName + ";" );
                    }
                    writer.WriteLine( "\t\t\t}" );
                    writer.WriteLine( "\t\t\tset" );
                    writer.WriteLine( "\t\t\t{" );
                    if( row [ 1 ].Equals( "static" ) )
                    {
                        writer.WriteLine( "\t\t\t\t" + attribName + " = value;" );
                    }
                    else
                    {
                        writer.WriteLine( "\t\t\t\tthis." + attribName + "= value;" );
                    }
                    writer.WriteLine( "\t\t\t}" );
                    writer.WriteLine( "\t\t}" );
                }
                writer.WriteLine( "\t}" );
                writer.WriteLine( "}" );


完整项目代码可下载....
共享下....
还是可以提高开发效率的.
// 获取到所有的用户表.

DataTable userTableName = GetTable( "select name as tablename from sysobjects where xtype = 'U'" );

//根据表名获取所有字段和字段类型

            DataTable myTable =
                GetTable(
                    "select syscolumns.name,systypes.name as type from syscolumns  " +
                    " INNER   JOIN  sysobjects  ON   syscolumns.id  =  sysobjects.id " +
                    "INNER   JOIN   systypes   ON   syscolumns.xtype   =   systypes.xtype " +
                    " WHERE   (sysobjects.name   =   '" + tableName + "')   AND   (systypes.name      'sysname') ");

//创建一个新的表.需要生成的数据
//字段1:修饰符 - 一般为 private
//字段2:修饰符2 - 一般我不写.可以写 statice
//字段3:SQL字段的类型 - 在生成.cs文件前会转换成c#类型
//字段4:生成属性名称 - 与表内字段名一样

            DataTableNew.Columns.Add("xiushifu1", typeof (string));
            DataTableNew.Columns.Add("xiushifu2", typeof (string));
            DataTableNew.Columns.Add("type", typeof (string));
            DataTableNew.Columns.Add("name", typeof (string));

// 进行类型转换
//为生成实体需要的数据添加记录保存

            for (int i = 0; i

//进行类型转换函数

        protected static string ChangeToCSharpType( string type )
        {
            string reval;

            switch( type.ToLower() )
            {
                case "int":
                    reval = "Int32";
                    break;
                case "text":
                    reval = "String";
                    break;
                case "bigint":
                    reval = "Int64";
                    break;
                case "binary":
                    reval = "System.Byte[]";
                    break;
                case "bit":
                    reval = "Boolean";
                    break;
                case "char":
                    reval = "String";
                    break;
                case "datetime":
                    reval = "System.DateTime";
                    break;
                case "decimal":
                    reval = "System.Decimal";
                    break;
                case "float":
                    reval = "System.Double";
                    break;
                case "image":
                    reval = "System.Byte[]";
                    break;
                case "money":
                    reval = "System.Decimal";
                    break;
                case "nchar":
                    reval = "String";
                    break;
                case "ntext":
                    reval = "String";
                    break;
                case "numeric":
                    reval = "System.Decimal";
                    break;
                case "nvarchar":
                    reval = "String";
                    break;
                case "real":
                    reval = "System.Single";
                    break;
                case "smalldatetime":
                    reval = "System.DateTime";
                    break;
                case "smallint":
                    reval = "Int16";
                    break;
                case "smallmoney":
                    reval = "System.Decimal";
                    break;
                case "timestamp":
                    reval = "System.DateTime";
                    break;
                case "tinyint":
                    reval = "System.Byte";
                    break;
                case "uniqueidentifier":
                    reval = "System.Guid";
                    break;
                case "varbinary":
                    reval = "System.Byte[]";
                    break;
                case "varchar":
                    reval = "String";
                    break;
                case "Variant":
                    reval = "Object";
                    break;
                default: