예제 #1
0
        /// <summary>
        /// 加载数据表
        /// </summary>
        /// <param name="connectionName"></param>
        private void LoadTable(String connectionName)
        {
            // 加载数据库
            nowConnectionName = connectionName;
            var dal   = new DbConnConfigDAL();
            var model = dal.FindOne(connectionName);

            this.nowDbSchema = DbSchemaFactory.Create(new MySqlDatabase(model.ConnectionString, MySqlClientFactory.Instance));
            try
            {
                nowDb = this.nowDbSchema.GetDatabase(model.DefaultDb);
            }
            catch (Exception e1)
            {
                MsgBox.ShowErrorMessage("数据库打开失败:" + e1.Message);
                return;
            }

            // 加载表
            this.listBox1.Items.Clear();
            this.listBox2.Items.Clear();
            this.unSelectedTableList.Clear();
            this.selectedTableList.Clear();
            foreach (SOTable t in nowDb.TableList)
            {
                listBox1.Items.Add(t);
                unSelectedTableList.Add(t);
            }
        }
예제 #2
0
        /// <summary>
        /// 创建一个数据提供程序实例
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <returns></returns>
        public static Database Create(string connectionStringName)
        {
            CheckUtil.ArgumentNotNullOrEmpty(connectionStringName, "connectionStringName");
            DbConnConfigDAL dal = new DbConnConfigDAL();

            var model = dal.FindOne(connectionStringName);

            if (model == null)
            {
                throw new Exception(string.Format(Resources.Data.ConnectionStringNameNotFound, connectionStringName));
            }

            string            connectionString = model.ConnectionString;
            string            providerName     = model.ProviderName;
            Database          db = new SqlServerDatabase(connectionString);
            DbProviderFactory providerFactory = null;

            if (string.IsNullOrEmpty(providerName))
            {
                return(db);
            }

            //if (css.ProviderName == "System.Data.OleDb")
            //{
            //    providerFactory = OleDbFactory.Instance;
            //}
            //else
            //{
            //    providerFactory = DbProviderFactories.GetFactory(css.ProviderName);
            //}
            //if (providerFactory == null) throw new Exception(string.Format(Resources.Data.DataProviderNotFound, css.ProviderName));

            switch (providerName)
            {
            //case "System.Data.SqlClient":
            //    break;
            case "System.Data.Odbc":
                db = new OdbcDatabase(connectionString);
                break;

            case "System.Data.OleDb":
                db = new OleDbDatabase(connectionString);
                break;

            case "System.Data.SQLite":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new SQLiteDatabase(connectionString, providerFactory);
                break;

            case "MySql.Data.MySqlClient":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new MySqlDatabase(connectionString, providerFactory);
                break;

            case "IBM.Data.DB2":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new DB2Database(connectionString, providerFactory);
                break;

            case "FirebirdSql.Data.FirebirdClient":
                providerFactory = DbProviderFactories.GetFactory(providerName);
                db = new FirebirdDatabase(connectionString, providerFactory);
                break;

            default:
                break;
            }

            return(db);
        }
예제 #3
0
        /// <summary>
        /// 保存数据库连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            var logObj        = Singleton.Container.Resolve <IOutput>();
            var connectName   = cbConnectStringName.Text.Trim();
            var providerName  = cbProviderName.Text.Trim();
            var connectString = txtConnectString.Text.Trim();
            var defaultDb     = txtDefaultDb.Text.Trim();

            if (string.IsNullOrEmpty(connectName))
            {
                MsgBox.Show("请选择或输入连接名称!");
                return;
            }

            if (string.IsNullOrEmpty(providerName))
            {
                MsgBox.Show("请选择Provider名称!");
                return;
            }

            if (string.IsNullOrEmpty(connectString))
            {
                MsgBox.Show("请输入ConnectString内容!");
                return;
            }

            if (string.IsNullOrEmpty(defaultDb))
            {
                MsgBox.Show("请输入默认数据库!");
                return;
            }

            try
            {
                var model = dal.FindOne(connectName);
                if (model == null)
                {
                    model = new DbConnConfig();
                    model.ConnectionString = connectString;
                    model.Name             = connectName;
                    model.ProviderName     = providerName;
                    model.DefaultDb        = defaultDb;

                    dal.Insert(model);
                }
                else
                {
                    model.ConnectionString = connectString;
                    model.Name             = connectName;
                    model.ProviderName     = providerName;
                    model.DefaultDb        = defaultDb;
                    dal.Update(model);
                }

                this.DialogResult = DialogResult.Yes;
                MsgBox.Show("保存成功!");
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
                this.DialogResult = DialogResult.No;
            }
        }