您的位置:首页 > 数据库

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

2008-02-29 09:58 507 查看
// 获取到所有的用户表.


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( "}" );

完整项目代码可下载....
共享下....
还是可以提高开发效率的.

附件:http://down.51cto.com/data/2349465
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息