zoukankan      html  css  js  c++  java
  • .net自动生成数据库表的类

    // 获取到所有的用户表.
    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( " public class " + className ); 
                    writer.WriteLine( " {" ); 



    //写入所有私有变量
                    for( int i = 0 ; i < table.Rows.Count ; i++ ) 
                    { 
                        object [ ] row = table.Rows [ i ].ItemArray; 
                        writer.Write( " " ); 
                        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( " " ); 
                        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( " {" ); 
                        writer.WriteLine( " get" ); 
                        writer.WriteLine( " {" ); 
                        if( row [ 1 ].Equals( "static" ) ) 
                        { 
                            writer.WriteLine( " return " + attribName + ";" ); 
                        } 
                        else 
                        { 
                            writer.WriteLine( " return this." + attribName + ";" ); 
                        } 
                        writer.WriteLine( " }" ); 
                        writer.WriteLine( " set" ); 
                        writer.WriteLine( " {" ); 
                        if( row [ 1 ].Equals( "static" ) ) 
                        { 
                            writer.WriteLine( " " + attribName + " = value;" ); 
                        } 
                        else 
                        { 
                            writer.WriteLine( " this." + attribName + "= value;" ); 
                        } 
                        writer.WriteLine( " }" ); 
                        writer.WriteLine( " }" ); 
                    } 
                    writer.WriteLine( " }" ); 
                    writer.WriteLine( "}" );

  • 相关阅读:
    linux系统调优工具
    搭建ceph分布式文件系统
    ansible管理windows主机
    jenkins结合ansible发布
    Linux系统安全配置
    tomcat 的安全配置预防后台被攻击
    【9】添加主页日志列表展示
    【8】添加新建/编辑博客逻辑
    【7】使用css/js/html模板来实现一个注册、登录和管理的功能
    Ubuntu下给Sublime Text 3添加用python3运行文件
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/4249898.html
Copyright © 2011-2022 走看看