注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 linux服务器被黑了
 帮助

通过 数据库表 自动生成 实体类.


2008-02-29 09:58:36
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://maddish.blog.51cto.com/282288/63635
// 获取到所有的用户表.
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[i]["type"].ToString());

                DataTableNew.Rows.Add(new object[] {"private", "", typeName, myTable.Rows[i]["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( "}" );
 
 
完整项目代码可下载....
共享下....
还是可以提高开发效率的.
 
 

本文出自 “maddish” 博客,请务必保留此出处http://maddish.blog.51cto.com/282288/63635


附件下载:
  完整项目代码.




    文章评论
 
2008-02-29 10:11:04
有什么问题请留言告之....

2008-02-29 10:39:50
自动生成总是很有用的 学习

2008-02-29 10:57:24
有不明白的或者问题可以一起探讨.~

2008-02-29 10:57:45
共同进步..

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: