首页 > 代码库 > 代码生成器

代码生成器

一.界面设计

二.具体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 代码生成器1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            string sql = "select * from INFORMATION_SCHEMA.TABLES";
            DataTable dt = ExecuteDatatable(sql);
            foreach (DataRow row in dt.Rows)
            {
                string tableName = (string)row["TABLE_NAME"];
                clbDatabase.Items.Add(tableName);
 
            }
          
        }
        private static string constr = ConfigurationManager.ConnectionStrings["dbconstr"].ConnectionString;
        #region 数据库链接
        private DataTable ExecuteDatatable(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
        #endregion
        #region 代码生成
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            string sql = "select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@tabel";
            foreach (string tablename in clbDatabase.CheckedItems)
            {
                DataTable dt = ExecuteDatatable(sql,
                     new SqlParameter("@tabel", tablename));
                //MessageBox.Show(dt.TableName.ToString());
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Class " + tablename);
                sb.AppendLine("{");

                foreach (DataRow row in dt.Rows)
                {
                    string dataType = ToVSType((string)row["DATA_TYPE"]);
                    string dataName = (string)row["COLUMN_NAME"];
                    sb.AppendLine("public " + dataType + " " + dataName + " {get;set;}");
                }
                sb.AppendLine("}");
                MessageBox.Show(sb.ToString());


            }

        }
        #endregion
        //数据库数据类型向VS转换
        private string ToVSType(string dataType)
        {
            switch (dataType)
            {
                case "int":
                    return "int";
                    //break;
                case "nchar":
                case "text":
                    return "string";
                    //break;
                case "bit":
                    return "bool";
                    //break;
                case "DateTime":
                    return "datetime";
                    //break;
                default :
                    return "object";

                   
            }
        }

    }
}