private void 生成构造函数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StringBuilder strb1 = new StringBuilder(Environment.NewLine);
            StringBuilder strb2 = new StringBuilder(Environment.NewLine);
            StringBuilder strb3 = new StringBuilder(Environment.NewLine);
            string        t     = "public {0}(){{}};" + Environment.NewLine;
            string        q     = "public {0}({1}){{}};";
            string        c     = string.Empty;

            foreach (TreeNode node in Home.form_Database.GetTreeView1Selected())
            {
                strb1.Append(string.Format(t, node.Text.ToString()) + Environment.NewLine);
                c = "";
                foreach (var item in node.Nodes)
                {
                    Column column = new Model.Column();
                    string data   = item.ToString();
                    data = data.Contains("TreeNode:") ? data.Replace("TreeNode:", "") : data;
                    var str = data.Split('(');
                    column.Name = str[0].ToString().Trim();
                    column.Type = StaticTool.GetColumnsType(str[1].ToString().Trim().Split(',')[0].ToString().Trim());
                    c          += column.Type + " " + column.Name + ",";
                }
                strb2.Append(string.Format(q, node.Text.ToString(), c.Length > 0? c.Substring(0, c.Length - 1):c) + Environment.NewLine);
                strb3.Append(strb1.ToString() + Environment.NewLine);
                strb3.Append(strb2.ToString() + Environment.NewLine);
                strb1.Length = 0;
                strb2.Length = 0;
            }
            Form_Code_Area fca_business = new Form_Code_Area(strb3.ToString(), "构造函数生成");

            fca_business.Show(Home.Instance.dockPanel1);
        }
        private void BuliderModel()
        {
            StringBuilder str_Model    = new StringBuilder(Environment.NewLine);
            ClassCreator  ClassCreator = new ClassCreator();

            // CSBaseClass CSBaseClass = new CSBaseClass();

            foreach (TreeNode node in Home.form_Database.GetTreeView1Selected())
            {
                foreach (var item in node.Nodes)
                {
                    Column column = new Model.Column();
                    string data   = item.ToString();//TreeNode: AdminID(uniqueidentifier,not null)
                    data = data.Contains("TreeNode:") ? data.Replace("TreeNode:", "") : data;
                    var str = data.Split('(');
                    column.Name = str[0].ToString().Trim();
                    column.Type = StaticTool.GetColumnsType(str[1].ToString().Trim().Split(',')[0].ToString().Trim());

                    str_Model.Append("\t\t" + string.Format(CSBaseClass.baseAttributeTemplate, column.Type, "_" + column.Name));
                    str_Model.Append(string.Format(CSBaseClass.basePropertyTemplate, column.Name, column.Type, "_" + column.Name));
                }
            }
            Form_Code_Area fca_business = new Form_Code_Area(str_Model.ToString(), "Model生成");

            fca_business.Show(Home.Instance.dockPanel1);
        }
Exemple #3
0
        private void CreateCode()
        {
            var param = GetCodeCreateSources();

            Business.CreateCode CreateCode = new Business.CreateCode(param.dataBaseType);
            Form_Code_Area      fca_model  = new Form_Code_Area(CreateCode.GetModelClass(param, 2), string.Format("实体类({0})", param.TableName));

            fca_model.Show(Home.Instance.dockPanel1);
            Form_Code_Area fca_data = new Form_Code_Area(CreateCode.GetDataClass(param), string.Format("数据类({0})", param.TableName));

            fca_data.Show(Home.Instance.dockPanel1);
            Form_Code_Area fca_business = new Form_Code_Area(CreateCode.GetBusinessClass(param), string.Format("业务类({0})", param.TableName));

            fca_business.Show(Home.Instance.dockPanel1);
            if (param.BuilderType == BuilderType.Factory)
            {
                Form_Code_Area fca_interface = new Form_Code_Area(CreateCode.GetInterfaceClass(param), string.Format("接口类({0})", param.TableName));
                fca_interface.Show(Home.Instance.dockPanel1);
                Form_Code_Area fca_factory = new Form_Code_Area(CreateCode.GetFactoryClass(param), string.Format("工厂类({0})", param.TableName));
                fca_factory.Show(Home.Instance.dockPanel1);
            }
            this.Close();
            button1.Text = "确定生成";
        }
Exemple #4
0
        public void AutoCode()
        {
            try
            {
                #region 代码自动生成
                string        t          = "\t";
                string        tt         = t + t;
                string        ttt        = t + t + t;
                StringBuilder sw         = new StringBuilder();
                StringBuilder db         = new StringBuilder();
                TreeNode      dbNode     = node.Parent.Parent;
                TreeNode      serverNode = Home.form_Database.GetRoot(node);
                if (dbNode == null || serverNode == null)
                {
                    return;
                }
                Servers    server = (Servers)((TreeNodeTag)serverNode.Tag).Tag;
                CodeCreate param  = new CodeCreate();
                param.ClassName = ((TreeNodeTag)node.Tag).Tag.ToString();
                param.DbName    = ((TreeNodeTag)dbNode.Tag).Tag.ToString();
                param.ServerID  = server.ID;
                param.TableName = ((TreeNodeTag)node.Tag).Tag.ToString();
                string sql = " use " + param.DbName + "; select name,type_name(user_type_id)as typeid from sys.columns where  "
                             + " object_id=(select object_id from sys.tables where name='" + param.TableName + "') ";
                DataTable dt1 = Database.GetDataTable(sql, param.ServerID, param.DbName);
                #region 实体类
                #region 实体类头部
                sw.Append("using System;" + Environment.NewLine);
                sw.Append("using System.Collections.Generic;" + Environment.NewLine);
                sw.Append("using System.Linq;" + Environment.NewLine);
                sw.Append("using System.Text;" + Environment.NewLine);
                sw.Append("namespace " + getdaxie(param.DbName + Environment.NewLine));
                sw.Append(t + "{" + Environment.NewLine);
                sw.Append(tt + "public class " + param.TableName + Environment.NewLine);
                sw.Append(ttt + "{" + Environment.NewLine);
                #endregion
                #region 实体类主体生成
                foreach (DataRow line1 in dt1.Rows)
                {
                    sw.Append(ttt + "private " + Database.GetFieldSqlType(line1["typeid"].ToString()) + "  _" + getdaxie(line1["name"].ToString()) + ";" + Environment.NewLine);
                    sw.Append(ttt + "public " + Database.GetFieldSqlType(line1["typeid"].ToString()) + " " + getdaxie(line1["name"].ToString()) + Environment.NewLine);
                    sw.Append(ttt + "{" + Environment.NewLine);
                    sw.Append(ttt + t + "get { return _" + getdaxie(line1["name"].ToString()) + "; }" + Environment.NewLine);
                    sw.Append(ttt + t + " set { _" + getdaxie(line1["name"].ToString()) + " = value; }" + Environment.NewLine);
                    sw.Append(ttt + "}" + Environment.NewLine);
                }
                sw.Append(tt + "}" + Environment.NewLine);
                sw.Append(t + "}" + Environment.NewLine);
                #endregion
                #endregion
                #region DBHelp
                db.Append("using System;" + Environment.NewLine);
                db.Append("using System.Collections.Generic;" + Environment.NewLine);
                db.Append("using System.Linq;" + Environment.NewLine);
                db.Append("using System.Text;" + Environment.NewLine);
                db.Append("using System.Data;" + Environment.NewLine);
                db.Append("using System.Data.SqlClient;" + Environment.NewLine);
                db.Append("using System.Configuration;" + Environment.NewLine);
                db.Append("namespace " + getdaxie(param.TableName) + Environment.NewLine);
                db.Append(t + "{" + Environment.NewLine);
                db.Append(tt + "public class " + getdaxie("DBHelpe") + Environment.NewLine);
                db.Append(tt + "{" + Environment.NewLine);
                db.Append(ttt + "private static SqlConnection con;" + Environment.NewLine);
                db.Append(ttt + "/// <summary>" + Environment.NewLine);
                db.Append(ttt + " ///获得connection " + Environment.NewLine);
                db.Append(ttt + "/// </summary>" + Environment.NewLine);
                db.Append(ttt + "public static SqlConnection GetCon()" + Environment.NewLine);
                db.Append(ttt + "{" + Environment.NewLine);
                db.Append(ttt + t + "string connectionString = ConfigurationManager.ConnectionStrings[\"url\"].ConnectionString;" + Environment.NewLine);
                db.Append(ttt + t + "if (con == null)" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + t + "con = new SqlConnection(connectionString);" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "return con;" + Environment.NewLine);
                db.Append(ttt + "}" + Environment.NewLine);
                db.Append(ttt + "/// <summary>" + Environment.NewLine);
                db.Append(ttt + "/// 执行增、删、改操作" + Environment.NewLine);
                db.Append(ttt + "/// </summary>" + Environment.NewLine);
                db.Append(ttt + "/// <param name=sql>SQL语句</param>" + Environment.NewLine);
                db.Append(ttt + "/// <returns>受影响行数</returns>" + Environment.NewLine);
                db.Append(ttt + "public static int ExecuteSql(string sql)" + Environment.NewLine);
                db.Append(ttt + "{" + Environment.NewLine);
                db.Append(ttt + t + "int i = -1;" + Environment.NewLine);
                db.Append(ttt + t + "SqlConnection con = DBHelpe.GetCon();" + Environment.NewLine);
                db.Append(ttt + t + "SqlCommand cmd = new SqlCommand(sql, con);" + Environment.NewLine);
                db.Append(ttt + t + "try" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "con.Open();" + Environment.NewLine);
                db.Append(ttt + tt + "i = cmd.ExecuteNonQuery();" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "catch (Exception err)" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "throw err;" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "finally" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "if (con.State == ConnectionState.Open)" + Environment.NewLine);
                db.Append(ttt + tt + "{" + Environment.NewLine);
                db.Append(ttt + tt + "con.Close();" + Environment.NewLine);
                db.Append(ttt + tt + "}" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "return i;" + Environment.NewLine);
                db.Append(ttt + "}" + Environment.NewLine);
                db.Append(ttt + "/// <summary>" + Environment.NewLine);
                db.Append(ttt + "/// 根据SQL返回DataTable" + Environment.NewLine);
                db.Append(ttt + "/// </summary>" + Environment.NewLine);
                db.Append(ttt + "/// <param name=sql>执行的SQL</param>" + Environment.NewLine);
                db.Append(ttt + "/// <returns>DataTable</returns>" + Environment.NewLine);
                db.Append(ttt + "public static DataTable GetTable(string sql)" + Environment.NewLine);
                db.Append(ttt + "{" + Environment.NewLine);
                db.Append(ttt + t + "DataTable dt =new DataTable();" + Environment.NewLine);
                db.Append(ttt + t + "SqlConnection con = DBHelpe.GetCon();" + Environment.NewLine);
                db.Append(ttt + t + "try" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "con.Open();" + Environment.NewLine);
                db.Append(ttt + tt + "SqlCommand cmd = new SqlCommand(sql, con);" + Environment.NewLine);
                db.Append(ttt + tt + "SqlDataAdapter da = new SqlDataAdapter(cmd);" + Environment.NewLine);
                db.Append(ttt + tt + "da.Fill(dt);" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "catch (Exception err)" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "throw err;" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "finally" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "if (con.State == ConnectionState.Open)" + Environment.NewLine);
                db.Append(ttt + tt + "{" + Environment.NewLine);
                db.Append(ttt + ttt + "con.Close();" + Environment.NewLine);
                db.Append(ttt + tt + "}" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "return dt;" + Environment.NewLine);
                db.Append(ttt + "}" + Environment.NewLine);
                db.Append(ttt + "/// <summary>" + Environment.NewLine);
                db.Append(ttt + "/// 执行SQL返回SqlDataReader" + Environment.NewLine);
                db.Append(ttt + "/// </summary>" + Environment.NewLine);
                db.Append(ttt + "/// <param name=sql>执行的SQL</param>" + Environment.NewLine);
                db.Append(ttt + "/// <returns>SqlDataReader</returns>" + Environment.NewLine);
                db.Append(ttt + "public static SqlDataReader ExReader(string sql)" + Environment.NewLine);
                db.Append(ttt + "{" + Environment.NewLine);
                db.Append(ttt + t + "SqlConnection con = DBHelpe.GetCon();" + Environment.NewLine);
                db.Append(ttt + t + "try" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "con.Open();" + Environment.NewLine);
                db.Append(ttt + tt + "SqlCommand cmd = new SqlCommand(sql, con);" + Environment.NewLine);
                db.Append(ttt + tt + "SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);" + Environment.NewLine);
                db.Append(ttt + tt + "return dr;" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + t + "catch(Exception err)" + Environment.NewLine);
                db.Append(ttt + t + "{" + Environment.NewLine);
                db.Append(ttt + tt + "throw err;" + Environment.NewLine);
                db.Append(ttt + t + "}" + Environment.NewLine);
                db.Append(ttt + "}" + Environment.NewLine);
                //****************************************************************
                db.Append(t + "}" + Environment.NewLine);
                db.Append("}" + Environment.NewLine);
                #endregion

                Form_Code_Area fca_model = new Form_Code_Area(sw.ToString(), string.Format("实体类({0})", param.TableName));
                fca_model.Show(Home.Instance.dockPanel1);
                Form_Code_Area fca_data = new Form_Code_Area(db.ToString(), string.Format("数据类({0})", param.TableName));
                fca_data.Show(Home.Instance.dockPanel1);
                #endregion
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            this.Close();
        }