zoukankan      html  css  js  c++  java
  • 代码生成器

    一.界面设计

    image

    二.具体代码

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

                       
                }
            }

        }
    }

  • 相关阅读:
    Windows平台分布式架构实践
    压力测试的轻量级具体做法
    vs.net git版本仓库使用 之解决冲突方法 原创
    VS2015 Git 插件使用教程
    HTML页面禁止选择、页面禁止复制、页面禁止右键
    python标准库介绍——3 stat 模块详解
    python标准库介绍——2 os.path模块详解
    python标准库介绍——1 os详解
    Python 多进程 一分钟下载二百张图片 是什么样子的体验
    封ip对爬虫的影响
  • 原文地址:https://www.cnblogs.com/shangguanjinwen/p/3700834.html
Copyright © 2011-2022 走看看