Ejemplo n.º 1
0
        private void LoadProject()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "Xml文件|*.xml";

            Nullable <bool> result = dlg.ShowDialog();

            if (result == true)
            {
                //projectPath = dlg.FileName;
                Project = Project.Load(dlg.FileName);
                if (!string.IsNullOrEmpty(Project.Namespace))
                {
                    TreeFrm loadtreeFrm = new TreeFrm(this);
                    loadtreeFrm.projectPath = dlg.FileName;

                    loadtreeFrm.Project = Project;
                    projectPath         = dlg.FileName;
                    loadtreeFrm.Text    = Project.Namespace + " - 基础数据结构";
                    loadtreeFrm.UpdateUI();
                    loadtreeFrm.Show();
                }
                else
                {
                    CMessageBox.Show(this, "项目载入失败,请检查该文件的格式项目是否是正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Ejemplo n.º 2
0
 public TableFrm(TreeFrm treeFrm, MainFrm owner)
 {
     InitializeComponent();
     _treeFrm = treeFrm;
     _Owner   = owner;
     // this.MdiParent = owner;
 }
Ejemplo n.º 3
0
        private void Save(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxTitle.Text) || string.IsNullOrEmpty(tbxTitle.Text))
            {
                tbxTitle.Focus();
                CMessageBox.Show(Owner, "项目名称或命名空间必须填写!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Visible = false;
            TreeFrm treeFrm = new TreeFrm(_Owner, this);

            //treeFrm.TopMost = true;
            treeFrm.TopLevel = false;   //这个必须有不然会提示:"不能向tabControl中添加顶级控件"
            //treeFrm.Dock = DockStyle.Fill;
            treeFrm.UpdateProjectNode(Project);
            Helpler.ChangeTitle(false, treeFrm);
            _Owner.mainTab.TabPages.Add(treeFrm.Text);
            //treeFrm.FormBorderStyle = FormBorderStyle.None;
            foreach (Control con in treeFrm.Controls)
            {
                con.Dock = DockStyle.None;
                _Owner.mainTab.TabPages[_Owner.mainTab.TabPages.Count - 1].Controls.Add(con);
                _Owner.mainTab.SelectedIndex = _Owner.mainTab.TabPages.Count - 1;
            }
            // treeFrm.Show();
            // treeFrm.mod
            //tabControl1.TabPages[0].Controls.Add(form);
            this.Close();
        }
Ejemplo n.º 4
0
 public ColFrm(MainFrm owner, TreeFrm treeFrm)
 {
     InitializeComponent();
     _Owner   = owner;
     _treeFrm = treeFrm;
     //this.MdiParent = _Owner;
     tbxDataType.DataSource  = Enum.GetNames(typeof(System.Data.SqlDbType));
     this.tbxName.LostFocus += new System.EventHandler(tbxName_LostFocus);
 }
Ejemplo n.º 5
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(comDatabaseList.Text))
            {
                return;
            }

            project.Namespace = comDatabaseList.Text;
            project.Title     = comDatabaseList.Text;

            string conStr1 = "Server={0};Port={1};Uid={2};Pwd={3};Database={4}";

            if (string.IsNullOrEmpty(txtHost.Text.Trim()) || string.IsNullOrEmpty(txtPort.Text.Trim()) || string.IsNullOrEmpty(txtUser.Text.Trim()) || string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                CMessageBox.Show(this, "服务器信息没填写完整。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string conStr = string.Format(conStr1, txtHost.Text.Trim(), txtPort.Text.Trim(), txtUser.Text.Trim(), txtPassword.Text.Trim(), comDatabaseList.Text);

            //  CessageBox.Show(conStr);

            MySqlConnection con = new MySqlConnection(conStr);

            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                CMessageBox.Show(ex.Message);
                return;
            }

            List <string> tableNames = new List <string>();
            MySqlCommand  cmd        = new MySqlCommand("show tables", con);
            IDataReader   reader     = cmd.ExecuteReader();

            while (reader.Read())
            {
                tableNames.Add(reader.GetString(0));
            }
            reader.Close();

            #region 查询列结构
            foreach (string strTableName in tableNames)
            {
                ZippyCoder.Entity.Table table = new ZippyCoder.Entity.Table();
                table.Name = strTableName;
                //查询表的备注  by chuchur 2014年9月22日 16:18:37
                string       sqlTableComment    = "SELECT table_name, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES  WHERE table_name ='" + strTableName + "'";
                MySqlCommand cmdTableComment    = new MySqlCommand(sqlTableComment, con);
                IDataReader  readerTableComment = cmdTableComment.ExecuteReader();
                while (readerTableComment.Read())
                {
                    if (readerTableComment.GetValue(1) != null)
                    {
                        table.Remark = readerTableComment.GetValue(1).ToString();
                        table.Title  = (table.Remark.Length > 15) ? table.Remark.Substring(0, 15) + "..." : table.Remark;
                    }
                    else
                    {
                        table.Remark = "这家伙很懒,毛都没留一根....";
                        table.Title  = strTableName;
                    }
                }
                readerTableComment.Dispose();

                project.Tables.Add(table);

                //声明列..
                //List<ZippyCoder.Entity.Col> fkCols = new List<ZippyCoder.Entity.Col>();

                //查询列名的备注 by chuchur 2014年9月22日 16:18:41
                string       sqlColComment    = "SELECT COLUMN_NAME ,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS where table_name='" + strTableName + "'";
                MySqlCommand cmdColComment    = new MySqlCommand(sqlColComment, con);
                IDataReader  readerColComment = cmdColComment.ExecuteReader();

                Dictionary <string, string> Cols = new Dictionary <string, string>();
                //取得字段和字段备注
                while (readerColComment.Read())
                {
                    string colName = string.Empty, colRemark = string.Empty;
                    if (readerColComment.GetValue(0) != null)
                    {
                        colName = readerColComment.GetValue(0).ToString();
                    }
                    if (readerColComment.GetValue(1) != null)
                    {
                        colRemark = readerColComment.GetValue(1).ToString();
                    }
                    if (!Cols.Keys.Contains(colName))
                    {
                        Cols.Add(colName, colRemark == "" ? colName : colRemark);
                    }
                }
                readerColComment.Dispose();
                foreach (var s in Cols)
                {
                    ZippyCoder.Entity.Col col = table.Exists(s.Key);
                    if (col == null)
                    {
                        col = new ZippyCoder.Entity.Col();
                        table.Cols.Add(col);
                        col.Parent = table;
                    }
                    col.Name   = s.Key;
                    col.Remark = s.Value;
                    col.Title  = s.Value;
                }


                //查询列名
                string       sql            = "SHOW COLUMNS FROM " + strTableName;
                MySqlCommand cmdColProperty = new MySqlCommand(sql, con);
                IDataReader  readerCol      = cmdColProperty.ExecuteReader();


                while (readerCol.Read())
                {
                    object _colName = readerCol.GetValue(0);
                    object _mysqlType = readerCol.GetValue(1);
                    object _canNull = readerCol.GetValue(2);
                    object _isKey = readerCol.GetValue(3);
                    object _defVal = readerCol.GetValue(4);
                    object _extra = readerCol.GetValue(5);
                    string colName = _colName == null ? "" : _colName.ToString();
                    string mysqlType = _mysqlType == null ? "" : _mysqlType.ToString();
                    string canNull = _canNull == null ? "" : _canNull.ToString();
                    string isKey = _isKey == null ? "" : _isKey.ToString();
                    string defVal = _defVal == null ? "" : _defVal.ToString();
                    string extra = _extra == null ? "" : _extra.ToString();
                    string dataType = string.Empty, dataLen = string.Empty;
                    var    match      = System.Text.RegularExpressions.Regex.Match(mysqlType, @"([\w]+).*?([\d\,]+).*");
                    var    matchCount = match.Groups.Count;
                    if (matchCount > 1)
                    {
                        dataType = match.Groups[1].Value;
                    }
                    if (matchCount > 2)
                    {
                        dataLen = match.Groups[2].Value;
                    }

                    if (string.IsNullOrEmpty(dataType))
                    {
                        dataType = mysqlType;
                    }



                    ZippyCoder.Entity.Col col = table.Exists(colName);
                    if (col == null)
                    {
                        col = new ZippyCoder.Entity.Col();
                        table.Cols.Add(col);
                        col.Parent = table;
                    }
                    col.Name = colName;
                    //col.Title = colName;
                    col.DataType     = ZippyCoder.TypeConverter.ToSqlDbType(dataType);
                    col.Default      = (defVal == "NULL" ? "" : defVal);
                    col.Length       = dataLen;
                    col.IsPK         = (isKey != null && isKey.ToLower() == "pri");
                    col.IsNull       = (canNull != null && canNull.ToLower() == "yes");
                    col.AutoIncrease = (extra != null && extra.ToLower() == "auto_increment");
                }
                readerCol.Close();
            }

            #endregion

            con.Close();

            //_Owner.Project = project;
            //_Owner.UpdateUI();

            TreeFrm _treeFrm = new TreeFrm(_Owner);

            _treeFrm.Project = project;
            _treeFrm.UpdateUI();
            _treeFrm.Text = project.Namespace + " - 基础数据结构";
            _treeFrm.Show();
            this.Close();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 从文件加载项目
        /// </summary>
        /// <param name="path"></param>
        public void LoadProject(string path)
        {
            if (string.IsNullOrEmpty(path)) //如果传入的路径为空就打开新的窗口重建
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.Filter = "Xml文件|*.xml";
                Nullable <bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    path = dlg.FileName;
                }
                else
                {
                    return;
                }
            }
            //projectPath = dlg.FileName;
            if (!File.Exists(path)) //如果找不到这个文件 ,则删除配置文件.
            {
                CMessageBox.Show(this, "该项目文件已经被删除或被迁移", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //从菜单里面移除
                ToolStripMenuItem mi = mLocalHistory.DropDownItems.Find(path, true)[0] as ToolStripMenuItem;
                mLocalHistory.DropDownItems.Remove(mi);
                //从列表里面移除
                ListItem li = listHistory.Items.Find(s => s.Value == path);
                listHistory.Items.Remove(li);

                //从配置文件移除
                LocalHistory lh = Program.history.LocalHistory.Find(s => s.SolutionPath == path);
                Program.history.LocalHistory.Remove(lh);
                return;
            }
            Project = Project.Load(path);
            if (!string.IsNullOrEmpty(Project.Namespace))
            {
                TreeFrm treeFrm = new TreeFrm(this);
                treeFrm.projectPath = path;

                treeFrm.Project = Project;
                projectPath     = path;
                treeFrm.Text    = Project.Namespace;
                treeFrm.UpdateUI();
                mainTab.TabPages.Add(treeFrm.Text);
                foreach (Control con in treeFrm.Controls)
                {
                    mainTab.TabPages[mainTab.TabPages.Count - 1].Controls.Add(con);
                    mainTab.SelectedIndex = mainTab.TabPages.Count - 1;
                }
                if (Program.history.LocalHistory == null || Program.history.LocalHistory.Find(s => s.SolutionPath == path) == null)
                {
                    var history = new LocalHistory();
                    history.SolutionPath = path;
                    history.SolutionName = Path.GetFileNameWithoutExtension(path);
                    Program.history.LocalHistory.Add(history);
                }
                //loadtreeFrm.Show();
            }
            else
            {
                CMessageBox.Show(this, "项目载入失败,请检查该文件的格式项目是否是正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 7
0
        private void Save(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(combbDatabase.Text)) return;

            project.Namespace = combbDatabase.Text;
            project.Title = combbDatabase.Text;

            string conStr1 = "Persist Security Info=False;User ID={1};Password={2};Server={0};Initial Catalog={3}";
            string conStr2 = "Persist Security Info=False;Integrated Security=true;Server={0};Initial Catalog={1}";

            string conStr = "";
            if (string.IsNullOrEmpty(tbxUserName.Text.Trim()))
            {
                conStr = string.Format(conStr2, tbxServer.Text, combbDatabase.Text);
            }
            else
            {
                conStr = string.Format(conStr1, tbxServer.Text, tbxUserName.Text, tbxPassword.Text, combbDatabase.Text);
            }

            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                CMessageBox.Show(Owner, ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            List<string> tableNames = new List<string>();
            SqlCommand cmd = new SqlCommand("select name from sysobjects where xtype='U' order by name", con);
            IDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                tableNames.Add(reader.GetString(0));
            }
            reader.Close();

            foreach (string strTableName in tableNames)
            {

                Table table = new Table();
                table.Name = strTableName;
                table.Title = strTableName;
                project.Tables.Add(table);

                string sql = @"SELECT 
INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 
INFORMATION_SCHEMA.COLUMNS.DATA_TYPE,
INFORMATION_SCHEMA.COLUMNS.COLUMN_DEFAULT, 
INFORMATION_SCHEMA.COLUMNS.CHARACTER_MAXIMUM_LENGTH, 
INFORMATION_SCHEMA.COLUMNS.NUMERIC_PRECISION, 
INFORMATION_SCHEMA.COLUMNS.NUMERIC_SCALE, 
INFORMATION_SCHEMA.COLUMNS.IS_NULLABLE, 
COLUMNPROPERTY(OBJECT_ID('" + strTableName + @"'), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 'IsIdentity') AS IsIdentity,
INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.COLUMNS LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ON INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.COLUMN_NAME AND INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.TABLE_NAME LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS ON INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.CONSTRAINT_NAME = INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME AND INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.TABLE_NAME = INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME WHERE INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = '" + strTableName + "'";


                //System.Diagnostics.Debug.WriteLine(sql);

                //return;


                SqlCommand cmdColProperty = new SqlCommand(sql, con);
                IDataReader readerCol = cmdColProperty.ExecuteReader();

                List<Col> fkCols = new List<Col>();

                while (readerCol.Read())
                {
                    string colName = readerCol.GetValue(0).ToString();
                    Col col = table.Exists(colName);
                    if (col == null)
                    {
                        col = new Col();
                        table.Cols.Add(col);
                        col.Parent = table;
                    }
                    col.Name = colName;
                    col.Title = colName;
                    col.DataType = TypeConverter.ToSqlDbType(readerCol.GetValue(1).ToString());
                    col.Default = readerCol.GetValue(2).ToString();
                    if (col.DataType == SqlDbType.VarChar || col.DataType == SqlDbType.NVarChar || col.DataType == SqlDbType.Char || col.DataType == SqlDbType.NChar)
                    {
                        col.Length = readerCol.GetValue(3).ToString();
                    }
                    else if (col.DataType == SqlDbType.Decimal)
                    {
                        col.Length = "(" + readerCol.GetValue(4).ToString() + "," + readerCol.GetValue(5).ToString() + ")";
                    }
                    if ((readerCol.GetValue(6).ToString().ToUpper() == "NO"))
                        col.IsNull = false;
                    if (readerCol.GetValue(7).ToString() == "1")
                        col.AutoIncrease = true;
                    if (readerCol.GetValue(8).ToString() == "PRIMARY KEY")
                        col.IsPK = true;
                    if (readerCol.GetValue(8).ToString() == "UNIQUE")
                        col.Unique = true;
                    if (readerCol.GetValue(8).ToString() == "FOREIGN KEY") //将有外键约束的列记录下来,待查。
                    {
                        fkCols.Add(col);
                    }
                }
                readerCol.Close();

            }

            con.Close();
            TreeFrm _treeFrm = new TreeFrm(_Owner);

            _treeFrm.Project = project;
            _treeFrm.UpdateUI();
            _treeFrm.Text = project.Namespace + " - 基础数据结构";
            _treeFrm.Show();
            this.Close();
        }