C#程序自动生成 实体类

C#程序自动生成 实体类

 

 

 1.xml文件部分

 

 <?xml version="1.0" standalone="yes"?>


  <DATA>
     <Conn>workstation id=localhost;packet size=8192;user id=sa;data source=192.168.168.126;persist security info=True;initial catalog=OridIntegrationServer;password=infact
</Conn>
  </DATA>

 

2.程序类文件部分

 

 using System;
using System.Xml;

namespace WindowsApplication1
{
 
 
 public class QuickCoder
 {
  public static string MSSQLServerDataTypeCSharpTypeMap (System.Type _type)
  {

   string _Result = string.Empty;


   switch( _type.ToString() )
   {
    case "char":
     _Result = "string";
     break;
    case "varchar" :
     _Result = "string";
     break;
    case "nvarchar" :
     _Result = "string";
     break;

    case "text":
     _Result = "string";
     break;
    case "datetime":
     _Result =  "System.DateTime";
     break;
    case "int":
     _Result =  "int";
     break;

    case "int identity":
     _Result =  "float";
     break;
    case "float":
     _Result =  "float";
     break;
    case "bit":
     _Result =  "string";
     break;
    case "numeric":
     _Result =  "double";
     break;
    case "money":
     _Result =  "double";
     break;
 
 


   }

  
   return _Result;
  }
  

  public static void table2CSharp(
   System.Data.DataSet  rsmd,
   String _namespace, String className) 
  {
   System.Text.StringBuilder cs = new  System.Text.StringBuilder (),
    csFiled = new System.Text.StringBuilder(),
    csProperty = new System.Text.StringBuilder();
   string line = "/r/n";
 

   //加入命名空间

   cs.Append("using System;").Append(line).Append(line);
   cs.Append("/*").Append(line);
   cs.Append(" *本代码部分为程序自动生成").Append(line);
   cs.Append(" * 使用前请检查").Append(line);
   cs.Append(" */").Append(line).Append(line);
   cs.Append("namespace ").Append(_namespace).Append(" {");
   cs.Append(line).Append(line);

   //加入类名
   cs.Append("/// <summary>").Append(line);
   cs.Append("//实体类").Append(line);
   cs.Append("/// </summary>").Append(line);
   cs.Append(" public class ").Append(className).Append(" {");
   cs.Append(line).Append(line);

   int c = rsmd.Tables[0].Columns.Count;
   System.Data.DataColumn[] pKeycolum = rsmd.Tables[0].PrimaryKey;


   string  type, name,pKey =string.Empty;
   System.Object t;

   for (int i = 0; i <= c-1; i++)
   {
    name = rsmd.Tables[0].Columns[i].ColumnName.Replace(' ', '_');
    System.Type dbType = rsmd.Tables[0].Columns[i].DataType ;
    string allowDBNull ="字段允许为空:"+ rsmd.Tables[0].Columns[i].AllowDBNull.ToString();
    string maxLong  ="字段长度:"+ rsmd.Tables[0].Columns[i].MaxLength.ToString();
    
    foreach( System.Data.DataColumn colum in pKeycolum)
    {
     if (name == colum.ColumnName)
     {
      pKey = "主键";
     }
    }
   
    t = dbType.ToString();

    string summary = string.Empty;

    summary = "///"+ name + line
       +"///"+  allowDBNull + line
       +"///"+ maxLong  + line;

    type = MSSQLServerDataTypeCSharpTypeMap(dbType);

    if (t != null)
    {
     type = t.ToString();
    }
    else
    {
     type = "___" + dbType;
     //prt(namespace + "" + className + " " + dbType);
    }

    //加入私有字段
    csFiled.Append(line).Append(line);
    csFiled.Append("/// <summary>").Append(line);
    csFiled.Append("///私有字段").Append(line);
    csFiled.Append(summary);
    csFiled.Append("/// </summary>").Append(line);
    csFiled.Append(" private ").Append(type).Append(" _").Append(name).Append(";").Append(line);
    

    //加入共有方法
    csProperty.Append(line).Append(line);
    csProperty.Append("/// <summary>").Append(line);
    csProperty.Append("///公有属性").Append(line);
    csProperty.Append(summary);
    csProperty.Append("/// </summary>").Append(line);
    csProperty.Append(" public ").Append(type).Append(" ").Append(name).Append(line);
    csProperty.Append("{").Append(line);
    csProperty.Append(" get {").Append(line);
    csProperty.Append(" return ").Append(" _").Append(name).Append(";").Append(line);
    csProperty.Append("}").Append(line);
    csProperty.Append(" set {").Append(line);
    csProperty.Append("  _").Append(name).Append(" = value ;").Append(line);
    csProperty.Append("}").Append(line);
    csProperty.Append("}").Append(line);
     

 

   
   }
   cs.Append(csFiled).Append(csProperty);
   cs.Append(" }").Append(line);
   cs.Append("}").Append(line);

   System.IO.File.Delete( className + ".cs");
   System.IO.StreamWriter sw = System.IO.File.CreateText( className + ".cs");

   sw.Write(cs.ToString());


   sw.Close();

  
  }

 }


 public class DataAccess
 {

  public string GetConnString ()
  {
   string strPath = @"XMLDataSet.xml";
   string _SqlConn = string.Empty;
   
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.Load( strPath );

   XmlNodeList nodeList=xmlDoc.SelectSingleNode("DATA").ChildNodes;

   foreach(XmlNode xn in nodeList)//遍历所有子节点
   {
    XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
    //TODO:

    _SqlConn = xe.InnerText;
    
   }

   return _SqlConn;
  
  }

  public System.Data.DataSet GetTableList()
  {
   
   string _SqlConn = string.Empty;

  
   _SqlConn =  GetConnString();
   
   System.Data.SqlClient.SqlConnection myConnection;

   myConnection = new System.Data.SqlClient.SqlConnection(_SqlConn);

   string myQuery = "select name from dbo.sysobjects where type ='U'";


   //System.Data.SqlClient.SqlCommand myCommand =new  System.Data.SqlClient.SqlCommand(myQuery,myConnection);

   myConnection.Open();
   System.Data.DataSet myDataSet = new System.Data.DataSet();
   

   try
   {
    System.Data.SqlClient.SqlDataAdapter myAdapter = new System.Data.SqlClient.SqlDataAdapter(myQuery,myConnection);
    myAdapter.Fill(myDataSet);
   }
   finally
   {
    // always call Close when done reading.
    //myReader.Close();
    // always call Close when done reading.
    myConnection.Close();
   }


   return myDataSet;


  }

  public System.Data.DataSet GetDataSet(string strTableName)
  {

   string _SqlConn = string.Empty;

   _SqlConn =  GetConnString();

   System.Data.SqlClient.SqlConnection myConnection;

   myConnection = new System.Data.SqlClient.SqlConnection(_SqlConn);

   string myQuery = "select * from "+strTableName+" where 1>2 ";

   myConnection.Open();
   System.Data.DataSet myDataSet = new System.Data.DataSet();
   

   try
   {
    System.Data.SqlClient.SqlDataAdapter myAdapter = new System.Data.SqlClient.SqlDataAdapter(myQuery,myConnection);
    myAdapter.Fill(myDataSet);
   }
   finally
   {
    myConnection.Close();
   }


   return myDataSet;

 


  }
 }

 }

 

3.窗口部分

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

 

namespace WindowsApplication1
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.ListBox lst_Tables;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   FillList();
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.lst_Tables = new System.Windows.Forms.ListBox();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(16, 232);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "生成实体类";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // lst_Tables
   //
   this.lst_Tables.HorizontalScrollbar = true;
   this.lst_Tables.ItemHeight = 12;
   this.lst_Tables.Location = new System.Drawing.Point(0, 0);
   this.lst_Tables.Name = "lst_Tables";
   this.lst_Tables.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
   this.lst_Tables.Size = new System.Drawing.Size(288, 184);
   this.lst_Tables.TabIndex = 1;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.lst_Tables);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {

//   DataSystemType _DataSystemType = new DataSystemType();
//
//   System.Data.DataSet _DS;
//   _DS = _DataSystemType.SystemTypeQuery();
//
//   QuickCoder.table2CSharp(_DS,"tempNameSpace","tempclass");

  System.Windows.Forms.ListBox.SelectedObjectCollection _SelObj  =this.lst_Tables.SelectedItems;

   int n=_SelObj.Count;
   for(int i=0;i<n;i++)
   {
    string tableName = ((System.Windows.Forms.ListBox.SelectedObjectCollection)_SelObj)[i].ToString();
    MakeFile( tableName );
   }

  
  }

  private void MakeFile(string TableName)
  {

   //根据表名生成dataSet

   System.Data.DataSet _DS ;

   DataAccess _DataAccess = new DataAccess();
   _DS = _DataAccess.GetDataSet(TableName);

   //生成文件
   QuickCoder.table2CSharp(_DS,"tempNameSpace", TableName);


   
  }

  private void FillList()
  {
   DataAccess _DataAccess = new DataAccess();

   
   System.Data.DataSet _DS = _DataAccess.GetTableList();

   foreach (System.Data.DataRow dr in _DS.Tables[0].Rows)
   {
    this.lst_Tables.Items.Add(dr[0].ToString());
   }

  }
 }
}

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值