Example #1
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;
                }
            }

            Model.ConnectionModel connModel = new WEF.ModelGenerator.Model.ConnectionModel();

            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
            {
                using (SQLiteConnection oledbConn = new SQLiteConnection(connModel.ConnectionString))
                {
                    oledbConn.Open();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接失败!\n\r" + ex.Message);
                return;
            }

            connModel.DbType = DatabaseType.Sqlite3.ToString();
            connModel.ID     = Guid.NewGuid();

            UtilsHelper.AddConnection(connModel);

            this.DialogResult = DialogResult.OK;

            this.Close();
        }
Example #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("请选择Access文件!");
                    return;
                }
            }

            Model.ConnectionModel connModel = new WEF.ModelGenerator.Model.ConnectionModel();

            if (rbdatabaseselect.Checked)
            {
                StringBuilder cstring = new StringBuilder();

                string exstension = txtfilepath.Text.Substring(txtfilepath.Text.LastIndexOf('.') + 1);
                if (exstension.ToLower().Equals("mdb"))
                {
                    cstring.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
                }
                else if (exstension.ToLower().Equals("accdb"))
                {
                    cstring.Append("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=");
                }

                cstring.Append(txtfilepath.Text);
                cstring.Append(";");
                if (!string.IsNullOrEmpty(txtPassword.Text))
                {
                    cstring.Append("User Id=");
                    cstring.Append(txtUserName.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
            {
                using (OleDbConnection oledbConn = new OleDbConnection(connModel.ConnectionString))
                {
                    oledbConn.Open();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接失败!\n\r" + ex.Message);
                return;
            }

            connModel.DbType = DatabaseType.MsAccess.ToString();
            connModel.ID     = Guid.NewGuid();

            UtilsHelper.AddConnection(connModel);

            this.DialogResult = DialogResult.OK;

            this.Close();
        }