Atlas, Ajax,ASP.NET, .NET, Javascript, PHP, C#
Johson点滴 Know What ! Know How ! Know Why ! Know Working !!! If you think you can ,You can !
一个生成数据库实体类的代码
在项目中可以很容易生成数据库实体类,提高开发的效率

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.Collections.Specialized;
  5using System.ComponentModel;
  6using System.Windows.Forms;
  7using System.Data;
  8using System.IO;
  9using System.Text;
 10using System.Data.SqlClient;
 11using System.Data.SqlTypes;
 12
 13namespace WindowsApplication1
 14{
 15    /// <summary>
 16    /// Form1 的摘要说明。
 17    /// </summary>

 18    public class Form1 : System.Windows.Forms.Form
 19    {
 20        private System.Windows.Forms.ListBox listBox1;
 21        private System.Windows.Forms.Button button1;
 22        private System.Windows.Forms.ListView listView1;
 23        private System.Windows.Forms.Button button2;
 24        private System.Windows.Forms.ComboBox comboBox1;
 25        private System.Windows.Forms.TextBox textBox1;
 26        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
 27        private System.Windows.Forms.Label label1;
 28        private System.Windows.Forms.ComboBox comboBox2;
 29        private System.Windows.Forms.Label label2;
 30        private System.Windows.Forms.Label label3;
 31        
 32        /// <summary>
 33        /// 必需的设计器变量。
 34        /// </summary>

 35        private System.ComponentModel.Container components = null;
 36
 37        public Form1()
 38        {
 39            //
 40            // Windows 窗体设计器支持所必需的
 41            //
 42            InitializeComponent();
 43            this.MaximizeBox = false;
 44            this.Text = "Create Tabel Class Tools";
 45            this.button1.Text = "Import";
 46            this.button2.Text = "Create";
 47            listView1.View = View.Details;
 48            listView1.GridLines = true;
 49            listView1.Columns.Add("NO"-2, HorizontalAlignment.Left);
 50            listView1.Columns.Add("ColumnName"-2, HorizontalAlignment.Left);
 51            listView1.Columns.Add("Type"-2, HorizontalAlignment.Left);
 52            listView1.Columns.Add("Length"-2, HorizontalAlignment.Left);
 53            this.comboBox2.Items.Add("local");
 54            this.comboBox2.Text = "local";
 55            SQLDMO.NameList strServerNames;
 56            SQLDMO.ApplicationClass sqldmoApp = new SQLDMO.ApplicationClass();
 57            try
 58            {
 59                strServerNames = sqldmoApp.ListAvailableSQLServers();
 60                for(int i= 0 ;i<strServerNames.Count;i++)
 61                    this.comboBox2.Items.Add(strServerNames.Item(i));
 62            }

 63            catch
 64            {
 65                MessageBox.Show("Can not connect to other services");
 66            }

 67            finally
 68            {
 69            sqldmoApp.Quit();
 70            }

 71            
 72            ComBoxListDataLoad(this.comboBox2.Text);
 73            this.textBox1.Text = string.Empty;
 74            this.label1.Text = string.Empty;
 75            this.label2.Text = "Servers:";
 76            this.label3.Text = "DataBase:";
 77            this.label1.AutoSize = true;
 78            this.label2.AutoSize = true;
 79            this.label3.AutoSize = true;
 80
 81            //
 82            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 83            //
 84        }

 85
 86        /// <summary>
 87        /// 清理所有正在使用的资源。
 88        /// </summary>

 89        protected override void Dispose( bool disposing )
 90        {
 91            if( disposing )
 92            {
 93                if (components != null
 94                {
 95                    components.Dispose();
 96                }

 97            }

 98            base.Dispose( disposing );
 99        }

100
101        Windows 窗体设计器生成的代码
227
228        /// <summary>
229        /// 应用程序的主入口点。
230        /// </summary>

231        [STAThread]
232        static void Main() 
233        {
234            Application.Run(new Form1());
235        }

236
237        public void SqlConn()
238        {
239            SqlConnection v_SqlConnection = new SqlConnection(ConnectString());
240           
241            try
242            {
243                v_SqlConnection.Open();
244                string strSql = "SELECT name AS tabelName FROM dbo.sysobjects WHERE (type = 'U') AND (name <> N'dtproperties') Order By tabelName"
245                SqlCommand v_SqlCommand = new SqlCommand(strSql,v_SqlConnection);
246                SqlDataReader v_SqlDataReader =v_SqlCommand.ExecuteReader();
247                while(v_SqlDataReader.Read())
248                {
249                    this.listBox1.Items.Add(v_SqlDataReader["tabelName"].ToString());
250                }

251            }

252            catch(SqlException se)
253            {
254                MessageBox.Show(se.ToString());
255            }

256            finally
257            {
258                v_SqlConnection.Close(); 
259            }

260        }

261
262        public string ConnectString()
263        {
264            return  string.Format("Integrated Security=SSPI;Initial Catalog={0};Data Source=({1})",this.comboBox1.SelectedItem,this.comboBox2.Text);
265        }

266        private void Form1_Load(object sender, System.EventArgs e)
267        {
268        }

269
270        private void button1_Click(object sender, System.EventArgs e)
271        {
272            this.listBox1.Items.Clear();  
273            SqlConn();
274        }

275
276        private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
277        {
278            SqlConnection v_SqlConnection = new SqlConnection(ConnectString());
279            try
280            {
281                v_SqlConnection.Open();
282                string strSql = string.Format("SELECT dbo.syscolumns.name AS ColumnName, dbo.systypes.name AS TypeName, dbo.syscolumns.length FROM dbo.syscolumns INNER JOIN dbo.sysobjects ON dbo.syscolumns.id = dbo.sysobjects.id INNER JOIN dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype WHERE (dbo.sysobjects.name = '{0}') and (dbo.systypes.name <> N'sysname') Order By ColumnName",this.listBox1.SelectedItem); 
283                SqlCommand v_SqlCommand = new SqlCommand(strSql,v_SqlConnection);
284                SqlDataReader v_SqlDataReader =v_SqlCommand.ExecuteReader();
285                int i= 0;
286                this.listView1.Items.Clear();
287                while(v_SqlDataReader.Read())
288                {
289                    ListViewItem v_ListViewItem = new ListViewItem();
290                    v_ListViewItem.SubItems[0].Text = i.ToString();
291                    v_ListViewItem.SubItems.Add(v_SqlDataReader[0].ToString());
292                    v_ListViewItem.SubItems.Add(v_SqlDataReader[1].ToString());
293                    v_ListViewItem.SubItems.Add(v_SqlDataReader[2].ToString());
294                    this.listView1.Items.Add(v_ListViewItem);
295                    i++;
296                }

297            }

298            catch(SqlException se)
299            {
300                MessageBox.Show(se.ToString());
301            }

302            finally
303            {
304                v_SqlConnection.Close(); 
305            }

306        }

307
308        private void button2_Click(object sender, System.EventArgs e)
309        {
310            
311            if(this.listBox1.SelectedItem == null)
312            {
313                MessageBox.Show("Please select one table,then try again","Opreat wrong");
314                return ;
315            }

316            this.folderBrowserDialog1.ShowDialog();
317            this.label1.Text = folderBrowserDialog1.SelectedPath +"\\" +  this.listBox1.SelectedItem.ToString() + ".cs";
318           
319            StreamWriter v_StreamWriter = new StreamWriter(folderBrowserDialog1.SelectedPath +"\\" +  this.listBox1.SelectedItem.ToString() + ".cs",false,Encoding.Unicode);
320            
321            string strText = "using System;\r\n using System.Data;\r\n namespace BE \r\n{ public class " + this.listBox1.SelectedItem + "\r\n {" ;       
322           
323            strText += "public " + this.listBox1.SelectedItem + "(){} \r\n";
324            foreach(ListViewItem v_ListViewItem in this.listView1.Items)
325            {
326                strText += "private " + StrSqlType(v_ListViewItem.SubItems[2].Text) + " _" + v_ListViewItem.SubItems[1].Text + ";\r\n";
327            }

328            strText += "\r\n\r\n";
329
330            foreach(ListViewItem v_ListViewItem in this.listView1.Items)
331            {
332                strText += "/// <summary> \r\n" + "///"+ v_ListViewItem.SubItems[1].Text  + " " +  v_ListViewItem.SubItems[2].Text + "\r\n/// </summary>\r\n";
333                strText += "public " + StrSqlType(v_ListViewItem.SubItems[2].Text)  + " " + v_ListViewItem.SubItems[1].Text + "\r\n{ get{return _" + v_ListViewItem.SubItems[1].Text +";}\r\n" + "set{ _" + v_ListViewItem.SubItems[1].Text +  "=value; }}\r\n" ;
334                strText += "\r\n";
335            }

336
337            strText += "}\r\n}";
338            v_StreamWriter.Write(strText);
339            v_StreamWriter.Close();
340            this.textBox1.Text = strText;
341        }

342        public string StrSqlType(string strType)
343        {
344            switch (strType)
345            {
346                case "bigint":
347                    return "long";
348                case "int ":
349                    return "int";
350                case "smallint":
351                    return "short";
352                case "tinyint":
353                    return "short";
354                case "bit":
355                    return "boolean";
356                case "decimal":
357                    return "decimal";
358                case "numeric":
359                    return "decimal";
360                case "money":
361                    return "string";
362                case "smallmoney":
363                    return "string";
364                case "float":
365                    return "float";
366                case "real":
367                    return "float";
368                case "datetime":
369                    return "DateTime";
370                case "smalldatetime":
371                    return "DateTime";
372                case "char":
373                    return "char";
374                case "varchar":
375                    return "string";
376                case "text":
377                    return "string";
378                case "nchar":
379                    return "string";
380                case "nvarchar":
381                    return "string";
382                case "ntext":
383                    return "string";
384                case "binary":
385                    return "byte";
386                case "varbinary":
387                    return "byte";
388                case "image":
389                    return "string";
390                case "uniqueidentifier":
391                    return "Guid";
392                default :
393                    return "string";
394            }

395        }

396
397        public void ComBoxListDataLoad(string strServerName)
398        {
399            SqlConnection v_SqlConnection = new SqlConnection(string.Format("Integrated Security=SSPI;Initial Catalog=Master;Data Source=({0})",strServerName));
400            try
401            {
402                v_SqlConnection.Open();
403                string strSql = "SELECT TOP 100 PERCENT name AS DBName FROM dbo.sysdatabases ORDER BY name DESC"
404                SqlCommand v_SqlCommand = new SqlCommand(strSql,v_SqlConnection);
405                SqlDataReader v_SqlDataReader =v_SqlCommand.ExecuteReader();
406                this.comboBox1.Text = "";
407                while(v_SqlDataReader.Read())
408                {
409                    this.comboBox1.Items.Add(v_SqlDataReader["DBName"]);
410                }

411            }

412            catch(SqlException se)
413            {
414                MessageBox.Show(se.ToString());
415            }

416            finally
417            {
418                v_SqlConnection.Close(); 
419            }

420        
421        }

422
423        private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
424        {
425            this.listView1.Sort();
426        }

427    }

428}

429
posted on 2007-08-28 15:19  Johson  阅读(2541)  评论(1编辑  收藏  举报