private void OpenDbSource(string connectionStr, bool addToList = true) { ConnectionModel connectionModel = ConnectionUtils.GetConnectionModel(connectionStr); if (null == connectionModel) { MessageBox.Show("打开数据库失败,连接为:" + connectionStr); return; } DatabaseListBox.ItemsSource = null; TableListBox.ItemsSource = null; MessageTxtBox.Text = null; try { string connectIp = connectionModel.RemoteIp; int connectPort = connectionModel.Port; string userName = connectionModel.UserName; string userPass = connectionModel.UserPass; string dbSourceStr = $"Data Source={connectIp};User Id={userName};Password={userPass};pooling=false;CharSet=utf8;port={connectPort}"; List <string> databaseNames = new List <string>(); // 打开之前先关闭已经打开的连接 CloseMysqlConnection(); _mySqlConnection = MysqlUtil.GetMySqlConnection(dbSourceStr); var sqlCmd = MysqlUtil.GetSqlCommand("show databases;", _mySqlConnection); _mySqlConnection.Open(); try { using (MySqlDataReader mySqlDataReader = sqlCmd.ExecuteReader()) { while (mySqlDataReader.Read()) { if (mySqlDataReader.HasRows) { string tableName = mySqlDataReader.GetString(0); databaseNames.Add(tableName); } } } } catch (Exception e) { MessageBox.Show("打开数据库失败," + e.Message); return; } _tableList = databaseNames; DatabaseListBox.ItemsSource = _tableList; // 显示数据库列表 } catch (Exception e) { MessageBox.Show("连接数据库失败,错误信息:" + e.Message); return; } // 添加进列表(使用打开功能的话就加,listbox的selectionChanged就不加) if (addToList) { ConnectionUtils.AddConnection(connectionStr); LoadConnectionList(); } }