Пример #1
0
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbbServer.Text))
            {
                MessageBox.Show("服务器不能为空!");
                return;
            }

            if (cbbShenFenRZ.SelectedIndex == 1 && string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("登陆名不能为空!");
                return;
            }

            if (!isTestLink)
            {
                try
                {
                    initAllDataBase();
                    isTestLink          = true;
                    cbbDatabase.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("连接失败!\n\r" + ex.Message);
                    cbbDatabase.Enabled = false;
                    return;
                }
            }

            if (cbbDatabase.SelectedIndex == 0)
            {
                MessageBox.Show("请选择数据库!");
                return;
            }


            string tempconnectionstring = createConnectionString(cbbDatabase.SelectedIndex == 0 ? "master" : cbbDatabase.Text);

            Entity.Connection connectionModel = new SharpTools.Entity.Connection();
            connectionModel.Database = cbbDatabase.SelectedIndex == 0 ? "all" : cbbDatabase.Text;
            connectionModel.ID       = Guid.NewGuid();
            connectionModel.Name     = cbbServer.Text + "(" + cbbServerType.Text + ")[" + connectionModel.Database + "]";
            if (cbbServerType.SelectedIndex == 0)
            {
                connectionModel.DbType = DataBaseType.MsSql;
            }
            else
            {
                connectionModel.DbType = DataBaseType.MsSql;
            }
            connectionModel.ConnectionString = tempconnectionstring;

            Utils.AddConnection(connectionModel);

            this.DialogResult = DialogResult.OK;

            this.Close();
        }
Пример #2
0
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (rbconnstring.Checked)
            {
                if (string.IsNullOrEmpty(txtConnectionString.Text))
                {
                    MessageBox.Show("请填写连接字符串!");
                    return;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(txtfilepath.Text))
                {
                    MessageBox.Show("请选择Sqlite数据库文件!");
                    return;
                }
            }

            Entity.Connection connModel = new SharpTools.Entity.Connection();

            connModel.DbType = DataBaseType.Sqlite;
            connModel.ID     = Guid.NewGuid();

            if (rbdatabaseselect.Checked)
            {
                StringBuilder cstring = new StringBuilder("Data Source=");
                cstring.Append(txtfilepath.Text);
                cstring.Append(";");
                if (!string.IsNullOrEmpty(txtPassword.Text))
                {
                    cstring.Append("Password="******";");
                }

                connModel.ConnectionString = cstring.ToString();
                connModel.Name             = txtfilepath.Text;
                connModel.Database         = connModel.Name.Substring(connModel.Name.LastIndexOf('\\') + 1);
            }
            else
            {
                connModel.ConnectionString = txtConnectionString.Text;


                string constring       = txtConnectionString.Text;
                string templeftstring  = string.Empty;
                string temprightstring = string.Empty;
                if (constring.IndexOf('/') > 0)
                {
                    templeftstring  = constring.Substring(0, constring.LastIndexOf('/'));
                    temprightstring = constring.Substring(constring.LastIndexOf('/') + 1);
                }
                else if (constring.IndexOf('\\') > 0)
                {
                    templeftstring  = constring.Substring(0, constring.LastIndexOf('\\'));
                    temprightstring = constring.Substring(constring.LastIndexOf('\\') + 1);
                }
                else
                {
                    MessageBox.Show("连接字符串格式不正确!");
                    return;
                }

                if (temprightstring.IndexOf(';') > 0)
                {
                    temprightstring = temprightstring.Substring(0, temprightstring.IndexOf(';'));
                }

                templeftstring = templeftstring.Substring(templeftstring.IndexOf('=') + 1);


                connModel.Name     = templeftstring + temprightstring;
                connModel.Database = temprightstring;
            }

            try
            {
                DB db = new DB(DataBaseType.Sqlite, connModel.ConnectionString);
                db.QueryTable("select 1");
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接失败!\n\r" + ex.Message);
                return;
            }

            Utils.AddConnection(connModel);

            this.DialogResult = DialogResult.OK;

            this.Close();
        }