コード例 #1
0
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                IConnectionInfo ci = new ConnectionInfo();
                switch (this.ConnectionType)
                {
                case "gviConnectionMySql5x":
                    ci.ConnectionType = gviConnectionType.gviConnectionMySql5x;
                    break;

                case "gviConnectionFireBird2x":
                    ci.ConnectionType = gviConnectionType.gviConnectionFireBird2x;
                    break;

                case "gviConnectionSQLite3":
                    ci.ConnectionType = gviConnectionType.gviConnectionSQLite3;
                    break;
                }
                ci.Server         = this.Server;
                ci.Port           = this.Port;
                ci.UserName       = this.UserName;
                ci.Password       = this.Password;
                dataSourceFactory = new DataSourceFactory();
                string[] names = (string[])dataSourceFactory.GetDataBaseNames(ci, true);
                for (int i = 0; i < names.Length; i++)
                {
                    if (CanOpen(names[i], ci))
                    {
                        this.cbDatabases.Items.Add(names[i]);
                    }
                }


                if (this.cbDatabases.Items.Count == 0)
                {
                    return;
                }
                this.cbDatabases.Text = this.cbDatabases.Items[0].ToString();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
        /// <summary>
        /// 连接数据库并获取数据库名
        /// </summary>
        /// <param name="connectionType"></param>
        /// <param name="dcInfor"></param>
        /// <returns></returns>
        public string[] ConnectAndGetDatabaseNames(string connectionType, DabaseConnectionInfo dcInfor)
        {
            try
            {
                switch (connectionType)
                {
                case "i3dConnectionMySql5x":
                    ci.ConnectionType = i3dConnectionType.i3dConnectionMySql5x;
                    break;

                case "i3dConnectionFireBird2x":
                    ci.ConnectionType = i3dConnectionType.i3dConnectionFireBird2x;
                    break;

                case "i3dConnectionSQLite3":
                    ci.ConnectionType = i3dConnectionType.i3dConnectionSQLite3;
                    break;

                default:
                    break;
                }
                ci.Server   = dcInfor.Server;
                ci.Port     = dcInfor.Port;
                ci.UserName = dcInfor.UserName;
                ci.Password = dcInfor.PassWord;

                if (TryOpenDatabase(ci))
                {
                    string[] dataBaseNames = (string[])dsFactory.GetDataBaseNames(ci, true);
                    Marshal.ReleaseComObject(ci);
                    return(dataBaseNames);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }