Example #1
0
        private void tvw_AfterSelect(object sender, TreeViewEventArgs e)
        {
            mysqlFields.Clear();

            tbx_TableName.Text = e.Node.Text;
            dt.Rows.Clear();
            //dgv.Rows.Clear();
            MySqlConnection dbConn = new MySqlConnection();

            try {
                dbConn.ConnectionString = getConnectionString();
                dbConn.Open();

                string       schemaName = e.Node.Text;
                MySqlCommand dbCmd      = new MySqlCommand();
                dbCmd.CommandText = "SHOW full COLUMNS FROM " + schemaName;
                dbCmd.Connection  = dbConn;

                MySqlDataAdapter dbAdapter = new MySqlDataAdapter(dbCmd);
                dbAdapter.Fill(dt);
                dt.TableName = Guid.NewGuid().ToString().Replace("-", "");

                dgv.DataSource = dt;
                List <MySQLField> _addFields = new List <MySQLField>();
                foreach (DataRow row in dt.Rows)
                {
                    MySQLField field = new MySQLField();
                    field.FieldName = row["field"].ToString();
                    field.Type      = row["type"].ToString();
                    field.Key       = row["key"].ToString();
                    field.Comment   = row["comment"].ToString();

                    mysqlFields.Add(field);
                    _addFields.Add(field);
                }


                if (_SelectedFields.Exists(t => t.tbname.Equals(schemaName, StringComparison.CurrentCultureIgnoreCase)) == false)
                {
                    string tpnick = "a";
                    //NickIndex index = nickIndex.Find(t => t.Name.Equals(schemaName));
                    //if(index == null) {
                    //    index = new NickIndex();
                    //    index.Name = schemaName;
                    //    index.Index = newNickIndex++;

                    //    nickIndex.Add(index);
                    //}
                    //tpnick += index.Index;

                    MySQLSchema mysqlSchema = new MySQLSchema();
                    mysqlSchema.tbname   = schemaName;
                    mysqlSchema.tbnick   = tpnick;
                    mysqlSchema.tbfields = _addFields;

                    _SelectedFields.Add(mysqlSchema);
                }
                //if(_SelectedFields.ContainsKey(e.Node.Text)== false) {
                //    _SelectedFields.Add(e.Node.Text,_addFields);
                //}
                //if(e.Node.Tag == null) {
                //    e.Node.Tag = mysqlFields;
                //}
            } catch (Exception exc) {
                Console.WriteLine(exc.ToString());
            } finally {
                dbConn.Close();
                dbConn = null;
            }

            if (tabControl2.SelectedTab == tp_selectBuilder && e.Node.Level >= 1)
            {
                contextMenuStrip1.Show(tvw, tvw.PointToClient(MousePosition));
            }
        }
Example #2
0
        private void btn_Build_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabPage1)
            {
                rtb_Code.Clear();
                StringBuilder sql = new StringBuilder();

                sql.Append("using System;\r\n");
                sql.Append("using System.Collections.Generic;\r\n");
                sql.Append("using System.Data;\r\n");
                sql.Append("using com.yuzz.dbframework;\r\n");
                sql.Append("using MySql.Data.MySqlClient;\r\n");

                //sql.Append("namespace JLJM {\r\n");
                sql.Append("[Serializable]\r\n");
                sql.Append("public class ").Append(tbx_TableName.Text).Append(" {\r\n");

                // 更新字段
                //sql.Append("\tpublic List<string> UpdateFields = new List<string>();").Append(Environment.NewLine);
                sql.Append("\tList<string> _UpdateFields = new List<string>();\r\n");
                sql.Append("\tpublic virtual List<string> UpdateFields {\r\n");
                sql.Append("\t\tget { return _UpdateFields; }\r\n");
                sql.Append("\t\tset { _UpdateFields = value; }\r\n");
                sql.Append("\t}\r\n");

                sql.Append("\tpublic static Type Type {").Append(Environment.NewLine);
                sql.Append("\t\tget {").Append(Environment.NewLine);

                sql.Append("\t\t\treturn typeof(").Append(tbx_TableName.Text).Append(");").Append(Environment.NewLine);
                sql.Append("\t\t}").Append(Environment.NewLine);
                sql.Append("\t}").Append(Environment.NewLine);

                sql.Append("\tpublic virtual string TableName {\r\n");
                sql.Append("\t\tget {\r\n");
                sql.Append("\t\t\treturn \"").Append(tbx_TableName.Text).Append("\";\r\n");
                sql.Append("\t\t}\r\n");
                sql.Append("\t}\r\n");

                MySQLField sqlField = mysqlFields.Find(t => t.Key.Equals("PRI"));
                if (sqlField == null)
                {
                    rtb_Code.Clear();
                    throw new Exception("数据表必须有主键Primary Key");
                }

                sql.Append("\tpublic virtual string PkFieldName {\r\n");
                sql.Append("\t\tget {\r\n");
                sql.Append("\t\t\treturn \"").Append(sqlField.FieldName).Append("\";\r\n");
                sql.Append("\t\t}\r\n");
                sql.Append("\t}\r\n");

                sql.Append("\tprivate List<SQLField> _Fields = null;\r\n");
                sql.Append("\tpublic List<SQLField> Fields{\r\n");
                sql.Append("\t\tget{\r\n");
                sql.Append("\t\t\tif(_Fields == null){\r\n");
                sql.Append("\t\t\t_Fields = new List<SQLField>();\r\n");

                foreach (MySQLField field in mysqlFields)
                {
                    string isPRI      = field.Key.Equals("PRI") ? "true" : "false";
                    string dateformat = "";
                    switch (field.Type)
                    {
                    case "date":
                        dateformat = "yyyy-MM-dd";
                        break;

                    case "datetime":
                        dateformat = "yyyy-MM-dd HH:mm:ss";
                        break;
                    }
                    sql.Append("\t\t\t\t");
                    sql.Append("_Fields.Add(new SQLField(\"").Append(field.FieldName).Append("\",").Append(ParseCSharpRuntimeType(field.Type, true)).Append(",").Append(isPRI).Append(",\"").Append(field.Comment.Trim()).Append("\"");
                    //if(string.IsNullOrEmpty(dateformat) == false) {
                    //    sql.Append(",\"").Append(dateformat).Append("\"");
                    //}

                    sql.Append("));\r\n");
                }
                sql.Append("\t\t\t}\r\n");
                sql.Append("\t\t\treturn _Fields;\r\n");
                sql.Append("\t\t}\r\n");
                sql.Append("\t}\r\n");

                foreach (MySQLField field in mysqlFields)
                {
                    string getCSharpType = ParseCSharpRuntimeType(field.Type, false);
                    sql.Append("\t").Append(getCSharpType + " _" + field.FieldName + ";").Append(Environment.NewLine);
                    if (string.IsNullOrEmpty(field.Comment) == false)    //  字段如果有备注就添加备注信息
                    {
                        sql.Append("\t/// <summary>\r\n");
                        sql.Append("\t/// ").Append(field.Comment).Append("\r\n");
                        sql.Append("\t/// </summary>\r\n");
                    }
                    sql.Append("\tpublic virtual ").Append(getCSharpType).Append(" ").Append(field.FieldName).Append(" {\r\n");
                    sql.Append("\t\tget{").Append(Environment.NewLine);
                    sql.Append("\t\t\treturn _" + field.FieldName + ";").Append(Environment.NewLine);
                    sql.Append("\t\t}").Append(Environment.NewLine);


                    sql.Append("\t\tset{").Append(Environment.NewLine);
                    if ("PRI".Equals(field.Key) == false)    // 非主键
                    {
                        sql.Append("\t\t\tif(value != this._" + field.FieldName + ") {").Append(Environment.NewLine);
                        sql.Append("\t\t\t    UpdateFields.Add(\"" + field.FieldName + "\");").Append(Environment.NewLine);
                        sql.Append("\t\t\t}").Append(Environment.NewLine);
                    }
                    sql.Append("\t\t\t_" + field.FieldName + " = value;").Append(Environment.NewLine);
                    sql.Append("\t\t}").Append(Environment.NewLine);
                    sql.Append("\t}\r\n");
                }
                sql.Append("}");
                //sql.Append("    }");

                rtb_Code.AppendText(sql.ToString());
            }
            else if (tabControl1.SelectedTab == tabPage2)
            {
            }
        }