码迷,mamicode.com
首页 > 其他好文 > 详细

代码生成器

时间:2014-05-01 15:26:48      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   tar   ext   width   color   get   int   string   art   

一.界面设计

mamicode.com,码迷

二.具体代码

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";

                   
            }
        }

    }
}

代码生成器,码迷,mamicode.com

代码生成器

标签:style   blog   class   tar   ext   width   color   get   int   string   art   

原文地址:http://www.cnblogs.com/shangguanjinwen/p/3700834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!