Пример #1
0
        private void BtnOk_Click(object sender, System.EventArgs e)
        {
            try
            {
                string user   = this.txtUser.Text.Trim();
                string pass   = this.txtPass.Text.Trim();
                string server = this.txtServer.Text.Trim();

                if ((user.Trim() == "") || (server.Trim() == ""))
                {
                    MessageBox.Show(this, "用户名或密码不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                constr = "Data Source=" + server + "; user id=" + user + ";password="******"正在连接服务器,请稍候...";
                    myCn.Open();
                }
                catch
                {
                    this.Text = "连接服务器失败!";
                    myCn.Close();
                    MessageBox.Show(this, "连接服务器失败!请检查服务器地址或用户名密码是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                myCn.Close();
                this.Text = "连接服务器成功!";


                if (dbobj == null)
                {
                    dbobj = new DbSettings();
                }

                //将当前配置写入配置文件
                dbobj.DbType        = "Oracle";
                dbobj.Server        = server;
                dbobj.ConnectStr    = constr;
                dbobj.DbName        = "";
                dbobj.ConnectSimple = chk_Simple.Checked;
                bool succ = DbConfig.AddSettings(dbobj);
                if (!succ)
                {
                    MessageBox.Show(this, "该服务器已经存在!请更换服务器地址或检查输入是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //将当前数据库系统类型写入配置
                //MainForm.setting.DbType="Oracle";
                //ModuleConfig.SaveSettings(MainForm.setting);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LogHelper.WriteException(ex);
            }
        }
Пример #2
0
        private void btnOk_Click(object sender, System.EventArgs e)
        {
            try
            {
                string server = this.comboBoxServer.Text.Trim();
                string user   = this.txtUser.Text.Trim();
                string pass   = this.txtPass.Text.Trim();
                if ((user == "") || (server == ""))
                {
                    MessageBox.Show(this, "服务器或用户名不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                dbName = this.cmbDBlist.SelectedIndex > 0 ? cmbDBlist.Text : "master";

                constr = GetSelVerified() == "Windows"
                                ? "Integrated Security=SSPI;Data Source=" + server + ";Initial Catalog=" + dbName
                                : (pass == ""
                                    ? "user id=" + user + ";initial catalog=" + dbName + ";data source=" + server
                                    : "user id=" + user + ";password="******";initial catalog=" + dbName + ";data source=" + server);

                string strtype = GetSelVer();

                #region 判断版本 GetSelVer()
                try
                {
                    string ver = GetSQLVer(constr);
                    if ((ver == "8") && (strtype == "SQL2005"))
                    {
                        DialogResult dr = MessageBox.Show(this, "该服务器并非SQLServer 2005,是否进行更改选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        if (dr == DialogResult.OK)
                        {
                            comboBoxServerVer.SelectedIndex = 2;
                        }
                    }

                    if ((ver == "9") && (strtype == "SQL2000"))
                    {
                        DialogResult dr = MessageBox.Show(this, "该服务器并非SQLServer 2000,是否进行更改选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        if (dr == DialogResult.OK)
                        {
                            comboBoxServerVer.SelectedIndex = 1;
                        }
                    }

                    if ((ver == "10") && (strtype != "SQL2008"))
                    {
                        DialogResult dr = MessageBox.Show(this, "该服务器并非SQLServer 2008,是否进行更改选择?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        if (dr == DialogResult.OK)
                        {
                            comboBoxServerVer.SelectedIndex = 0;
                        }
                    }
                }
                catch
                {
                }

                #endregion

                //测试连接
                var myCn = new SqlConnection(constr);
                try
                {
                    this.Text = "正在连接服务器,请稍候...";
                    myCn.Open();
                }
                catch (System.Exception ex)
                {
                    this.Text = "连接服务器失败!";
                    LogHelper.WriteException(ex);
                    MessageBox.Show(this, "连接服务器失败!请检查服务器地址或用户名密码是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    myCn.Close();
                }

                this.Text = "连接服务器成功!";

                if (dbobj == null)
                {
                    dbobj = new DbSettings();
                }

                //string strtype="SQL2000";
                //if (this.comboBoxServerVer.Text == "SQL Server2005")
                //{
                //    strtype="SQL2005";
                //}

                //将当前配置写入配置文件
                dbobj.DbType        = strtype;
                dbobj.Server        = server;
                dbobj.ConnectStr    = constr;
                dbobj.DbName        = dbName;
                dbobj.ConnectSimple = chk_Simple.Checked;
                bool succ = DbConfig.AddSettings(dbobj);
                if (!succ)
                {
                    MessageBox.Show(this, "该服务器已经存在!请更换服务器地址或检查输入是否正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LogHelper.WriteException(ex);
            }
        }
Пример #3
0
        public void GenerateDataGridView()
        {
            if (this.CurrentDbViewForm == null)
            {
                MessageBox.Show("无可用对象!");
            }

            dgvDefine.AutoGenerateColumns = false;

            #region 手动生成列(已注释)

            /*
             * DataGridViewTextBoxColumn colAttribute = new System.Windows.Forms.DataGridViewTextBoxColumn();
             * colAttribute.DataPropertyName = "ColumnName";
             * colAttribute.HeaderText = "属性名称";
             * colAttribute.Name = "colAttribute";
             *
             * DataGridViewComboBoxColumn colType = new System.Windows.Forms.DataGridViewComboBoxColumn();
             * colAttribute.DataPropertyName = "TypeName";
             * colAttribute.HeaderText = "类型";
             * colAttribute.Name = "colType";
             *
             * DataGridViewTextBoxColumn colTitle = new System.Windows.Forms.DataGridViewTextBoxColumn();
             * colAttribute.DataPropertyName = "ColDescription";
             * colAttribute.HeaderText = "标题";
             * colAttribute.Name = "colTitle";
             *
             * DataGridViewTextBoxColumn colDTColumnName = new System.Windows.Forms.DataGridViewTextBoxColumn();
             * colAttribute.DataPropertyName = "ColumnName";
             * colAttribute.HeaderText = "列名";
             * colAttribute.Name = "colDTColumnName";
             *
             * DataGridViewComboBoxColumn colDTDataType = new System.Windows.Forms.DataGridViewComboBoxColumn();
             * colAttribute.DataPropertyName = "";
             * colAttribute.HeaderText = "数据类型";
             * colAttribute.Name = "colDTDataType";
             *
             * DataGridViewTextBoxColumn colDTDataLength = new System.Windows.Forms.DataGridViewTextBoxColumn();
             * colAttribute.DataPropertyName = "Length";
             * colAttribute.HeaderText = "长度";
             * colAttribute.Name = "colDTDataLength";
             *
             * DataGridViewTextBoxColumn colDTDataPreci = new System.Windows.Forms.DataGridViewTextBoxColumn();
             * colAttribute.DataPropertyName = "Preci";
             * colAttribute.HeaderText = "小数";
             * colAttribute.Name = "colDTDataPreci";
             *
             * DataGridViewCheckBoxColumn colDTDataNull = new System.Windows.Forms.DataGridViewCheckBoxColumn();
             * colAttribute.DataPropertyName = "IsNull";
             * colAttribute.HeaderText = "非空";
             * colDTDataNull.Width = 45;
             * colAttribute.Name = "colDTDataNull";
             *
             * DataGridViewCheckBoxColumn colDTDataPrimaryKey = new System.Windows.Forms.DataGridViewCheckBoxColumn();
             * colAttribute.DataPropertyName = "IsPK";
             * colAttribute.HeaderText = "主键";
             * colDTDataNull.Width = 45;
             * colAttribute.Name = "colDTDataPrimaryKey";
             *
             * DataGridViewCheckBoxColumn colDTDataIdentity = new System.Windows.Forms.DataGridViewCheckBoxColumn();
             * colAttribute.DataPropertyName = "IsIdentity";
             * colAttribute.HeaderText = "自增";
             * colDTDataNull.Width = 45;
             * colAttribute.Name = "colDTDataIdentity";
             *
             * DataGridViewTextBoxColumn colDTDataDefaultValue = new System.Windows.Forms.DataGridViewTextBoxColumn();
             * colAttribute.DataPropertyName = "DefaultVal";
             * colAttribute.HeaderText = "默认值";
             * colAttribute.Name = "colDTDataDefaultValue";
             *
             * this.dgvDefine.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[]
             * {
             *  colAttribute, colType, colTitle, colDTColumnName, colDTDataType, colDTDataLength,
             *  colDTDataPreci, colDTDataNull, colDTDataPrimaryKey, colDTDataIdentity, colDTDataDefaultValue
             * });
             */
            #endregion

            var selectedNode = this.CurrentDbViewForm.treeView1.SelectedNode;
            if (selectedNode == null)
            {
                return;
            }
            this.ServerName = selectedNode.Parent.Parent.Parent.Text;
            this.DBName     = selectedNode.Parent.Parent.Text;
            this.TableName  = selectedNode.Text;

            txtName.Text      = this.TableName;
            txtTableName.Text = this.TableName;
            dbSet             = DbConfig.GetSetting(this.ServerName);

            var listDataTypeRefer = GetDataTypeRefer();
            colType.ValueMember      = "To";
            colType.DisplayMember    = "To";
            colType.DataPropertyName = "To";
            colType.DataSource       = listDataTypeRefer;

            colDTDataType.ValueMember      = "From";
            colDTDataType.DisplayMember    = "From";
            colDTDataType.DataPropertyName = "From";
            colDTDataType.DataSource       = listDataTypeRefer;
            //todo:这儿要尽量做到通用。
            idbObj = DBOMaker.CreateDbObj(dbSet.DbType);
            idbObj.DbConnectStr = dbSet.ConnectStr;
            var colList = idbObj.GetColumnInfoList(this.DBName, this.TableName);
            dgvDefine.DataSource = colList;
        }