using Api.Framework.Tools; using Api.Framework.Tools; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using UI.Framework.Forms; namespace Api.Framework.UIForm { /// /// 选择数据库 /// public partial class SetConnectionConfig : BaseForm { public SetConnectionConfig() { InitializeComponent(); } private void SetConnectionConfig_Load(object sender, EventArgs e) { this.Icon = PublickIcon; var config = ApiClient.Setting.DbConfig; //Data Source = localhost; Initial Catalog = test_db; Persist Security Info = True; User ID = root; Password = 123456; Min Pool Size = 1; Max Pool Size = 3; if (config != null && !string.IsNullOrEmpty(config.ConnectionString)) { var reg = Regex.Match(config.ConnectionString, "Data Source=(?.*?);Initial Catalog=(?.*?);Persist Security Info=True;User ID=(?.*?);Password=(?.*?);Port=(?.*?);"); if (reg.Success) { this.textEdit1.Text = reg.Groups["db_host"].ToString(); this.textEdit3.Text = reg.Groups["db_name"].ToString(); this.textEdit2.Text = reg.Groups["db_port"].ToString(); this.textEdit4.Text = reg.Groups["db_user"].ToString(); this.textEdit5.Text = reg.Groups["db_pass"].ToString(); } } radioGroup1_SelectedIndexChanged(null,null); } ConnectionConfig tempConfig = null; public bool IsOk { get; set; } private void simpleButton1_Click(object sender, EventArgs e) { try { tempConfig = new ConnectionConfig(); //SessionThreadLocal.Clear(); tempConfig.ConnectionString =string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};Port={4};Min Pool Size = 5; Max Pool Size = 30;Charset=utf8;", this.textEdit1.Text,this.textEdit3.Text,this.textEdit4.Text,this.textEdit5.Text, this.textEdit2.Text); tempConfig.DatabaseType = DatabaseType.MYSQL; var session = ApiClient.GetSession(tempConfig); session.BeginTransaction(); session.Rollback(); ShowSuccess("恭喜您,数据库可以成功连接!"); } catch (Exception ex) { tempConfig = null; ShowError(ex); } } private void simpleButton2_Click(object sender, EventArgs e) { try { if (this.radioGroup1.SelectedIndex==0) { string src = string.Format("Data Source={0};Version=3;password={1};Journal Mode=WAL;", Util.MapFile("数据库.db", "Config"), ""); tempConfig = new ConnectionConfig() { ConnectionString = src, DatabaseType = DatabaseType.SQLITE }; ApiClient.Setting.DbConfig = tempConfig; IsOk = true; } else { if (tempConfig == null) throw new Exception("请先测试Mysql 是否可以正常连接!"); IsOk = true; } ApiClient.Setting.DbConfig = tempConfig; //Util.Save(ApiClient.Setting); this.Close(); } catch (Exception ex) { ShowError(ex); } } private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e) { this.groupControl2.Enabled = this.radioGroup1.SelectedIndex == 1; } } }