转一个生成实体类的类
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
namespace BuilderEntity
{
public partial class Build : Form
{
public Build( )
{
InitializeComponent();
}
private SqlConnection _Conn;
private void BunBuild_Click( object sender , EventArgs e )
{
DataTable userTableName = GetTable("select name as tablename from sysobjects where xtype = 'U'");
//GetTable( "select name as tablename from sysobjects where xtype = 'U'" );
for( int i = 0 ; i < userTableName.Rows.Count ; i++ )
{
BuilRefer( userTableName.Rows [ i ] [ "tablename" ].ToString() );
}
}
protected void BuilRefer( string tableName )
{
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') ");
DataTable DataTableNew = new DataTable();
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"]});
}
string mapPath = Application.StartupPath + ("/" + txtNamespace.Text + "/");
if (!Directory.Exists(mapPath))
{
Directory.CreateDirectory(Application.StartupPath + ("/" + txtNamespace.Text + "/"));
}
BuilderSource(DataTableNew, mapPath + tableName + ".cs", tableName, txtNamespace.Text);
MessageBox.Show("生成成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private DataTable GetTable( string query )
{
_Conn = new SqlConnection(txtDBstr.Text.Trim());
using (_Conn)
{
_Conn.Open();
//DataTable objDataTable = new DataTable();
DataSet ds = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(query, _Conn);
// try
// {
//objAdapter.Fill(objDataTable);
objAdapter.Fill(ds);
// }
// finally
// {
// _Conn.Close();
// }
return ds.Tables[0];
}
}
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;
}
public static void BuilderSource( DataTable table , string fileDesc , string className , string nameSpace )
{
using( 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{" );
if( table.Rows.Count == 0 )
{
writer.WriteLine( "\t\t//没有输入字段" );
}
else
{
writer.WriteLine( "\t\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();
}
writer.WriteLine();
if( table.Rows.Count == 0 )
{
writer.WriteLine( "\t\t//没有属性" );
}
else
{
writer.WriteLine( "\t\t//以下是生成的属性" );
}
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( "}" );
}
}
}
}
posted on 2010-06-30 11:55 jianshaohui 阅读(187) 评论(0) 编辑 收藏 举报