/// <summary> /// Get database servers setting. /// </summary> /// <returns>database servers setting</returns> public List <DatabaseServerInfo> GetDatabaseServers() { try { var databases = new List <DatabaseServerInfo>(); using (var conn = new SQLiteConnection(registryConnectionString)) { conn.SetPassword(dbPassword); conn.Open(); using (var command = new SQLiteCommand(SQLiteText.Registry_Get_DatabaseServers, conn)) { using (var rdr = command.ExecuteReader(CommandBehavior.CloseConnection)) { while (rdr.Read()) { var db = new DatabaseServerInfo(); db.UniqueID = ComUtility.DBNullGuidHandler(rdr["unique_id"]); db.DatabaseIntention = ComUtility.DBNullDBIntentionHandler(rdr["database_intention"]); db.DatabaseType = ComUtility.DBNullDBTypeHandler(rdr["database_type"]); db.DatabaseIP = ComUtility.DBNullStringHandler(rdr["database_ip"]); db.DatabasePort = ComUtility.DBNullInt32Handler(rdr["database_port"]); db.DatabaseUser = ComUtility.DBNullStringHandler(rdr["database_user"]); db.DatabasePwd = ComUtility.DBNullStringHandler(rdr["database_pwd"]); db.DatabaseName = ComUtility.DBNullStringHandler(rdr["database_name"]); db.UpdateTime = ComUtility.DBNullDateTimeHandler(rdr["update_time"]); databases.Add(db); } } } } return(databases); } catch { throw; } }
/// <summary> /// Test database connection. /// </summary> private void TestBtn_Click(object sender, EventArgs e) { try { if (paramTab.SelectedIndex != 0 && paramTab.SelectedIndex != 1) { return; } if (paramTab.SelectedIndex == 0) { if (String.IsNullOrWhiteSpace(masterDBServer.Text)) { masterDBServer.Focus(); MessageBox.Show("数据库地址不能为空", "主数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(masterDBName.Text)) { masterDBName.Focus(); MessageBox.Show("数据库名称不能为空", "主数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(masterDBUser.Text)) { masterDBUser.Focus(); MessageBox.Show("登录名不能为空", "主数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(masterDBPwd.Text)) { masterDBPwd.Focus(); MessageBox.Show("密码不能为空", "主数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var database = new DatabaseServerInfo(); database.UniqueID = Guid.NewGuid(); database.DatabaseIntention = EnmDBIntention.Master; database.DatabaseType = ComUtility.DBNullDBTypeHandler(masterDBType.SelectedValue); database.DatabaseIP = masterDBServer.Text; database.DatabasePort = (int)masterDBPort.Value; database.DatabaseName = masterDBName.Text; database.DatabaseUser = masterDBUser.Text; database.DatabasePwd = masterDBPwd.Text; database.UpdateTime = DateTime.Now; paramTab.Enabled = false; TestBtn.Enabled = false; OKBtn.Enabled = false; var connectionString = Common.CreateConnectionString(database); var testthread = new Thread(() => { try { var message = ""; var timeout = 60; var thread = new Thread(() => { using (var conn = new SqlConnection(connectionString)) { try { conn.Open(); conn.Close(); TestStatus = EnmTestStatus.Success; } catch (Exception err) { TestStatus = EnmTestStatus.Failure; message = err.Message; } } }); TestStatus = EnmTestStatus.Testing; thread.IsBackground = true; thread.Start(); var sw = System.Diagnostics.Stopwatch.StartNew(); var ts = TimeSpan.FromSeconds(timeout); while (sw.Elapsed < ts) { thread.Join(TimeSpan.FromMilliseconds(1000)); if (TestStatus != EnmTestStatus.Testing) { break; } } sw.Stop(); if (TestStatus == EnmTestStatus.Testing) { MessageBox.Show("SQL Server服务器连接超时", "主数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (TestStatus == EnmTestStatus.Success) { MessageBox.Show("测试连接成功", "主数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (TestStatus == EnmTestStatus.Failure) { MessageBox.Show(message, "主数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception err) { MessageBox.Show(err.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Invoke(new MethodInvoker(delegate { paramTab.Enabled = true; TestBtn.Enabled = true; OKBtn.Enabled = true; CancelBtn.Enabled = true; })); TestStatus = EnmTestStatus.Default; } }); testthread.IsBackground = true; testthread.Start(); } else { if (String.IsNullOrWhiteSpace(hisDBServer.Text)) { hisDBServer.Focus(); MessageBox.Show("数据库地址不能为空", "历史数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(hisDBName.Text)) { hisDBName.Focus(); MessageBox.Show("数据库名称不能为空", "历史数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(hisDBUser.Text)) { hisDBUser.Focus(); MessageBox.Show("登录名不能为空", "历史数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(masterDBPwd.Text)) { hisDBPwd.Focus(); MessageBox.Show("密码不能为空", "历史数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var database = new DatabaseServerInfo(); database.UniqueID = Guid.NewGuid(); database.DatabaseIntention = EnmDBIntention.History; database.DatabaseType = ComUtility.DBNullDBTypeHandler(hisDBType.SelectedValue); database.DatabaseIP = hisDBServer.Text; database.DatabasePort = (int)hisDBPort.Value; database.DatabaseName = hisDBName.Text; database.DatabaseUser = hisDBUser.Text; database.DatabasePwd = hisDBPwd.Text; database.UpdateTime = DateTime.Now; paramTab.Enabled = false; TestBtn.Enabled = false; OKBtn.Enabled = false; var connectionString = Common.CreateConnectionString(database); var testthread = new Thread(() => { try { var message = ""; var timeout = 60; var thread = new Thread(() => { using (var conn = new SqlConnection(connectionString)) { try { conn.Open(); conn.Close(); TestStatus = EnmTestStatus.Success; } catch (Exception err) { TestStatus = EnmTestStatus.Failure; message = err.Message; } } }); TestStatus = EnmTestStatus.Testing; thread.IsBackground = true; thread.Start(); var sw = System.Diagnostics.Stopwatch.StartNew(); var ts = TimeSpan.FromSeconds(timeout); while (sw.Elapsed < ts) { thread.Join(TimeSpan.FromMilliseconds(1000)); if (TestStatus != EnmTestStatus.Testing) { break; } } sw.Stop(); if (TestStatus == EnmTestStatus.Testing) { MessageBox.Show("SQL Server服务器连接超时", "历史数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (TestStatus == EnmTestStatus.Success) { MessageBox.Show("测试连接成功", "历史数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (TestStatus == EnmTestStatus.Failure) { MessageBox.Show(message, "历史数据库测试", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception err) { MessageBox.Show(err.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Invoke(new MethodInvoker(delegate { paramTab.Enabled = true; TestBtn.Enabled = true; OKBtn.Enabled = true; CancelBtn.Enabled = true; })); TestStatus = EnmTestStatus.Default; } }); testthread.IsBackground = true; testthread.Start(); } } catch (Exception err) { Common.WriteLog(DateTime.Now, EnmMsgType.Error, "System", "Delta.MPS.AccessSystem.ParamForm", err.Message, err.StackTrace); MessageBox.Show(err.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Save system paramters information. /// </summary> private void OKBtn_Click(object sender, EventArgs e) { try { if (String.IsNullOrWhiteSpace(masterDBServer.Text)) { if (paramTab.SelectedIndex != 0) { paramTab.SelectedIndex = 0; } masterDBServer.Focus(); MessageBox.Show("数据库地址不能为空", "主数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(masterDBName.Text)) { if (paramTab.SelectedIndex != 0) { paramTab.SelectedIndex = 0; } masterDBName.Focus(); MessageBox.Show("数据库名称不能为空", "主数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(masterDBUser.Text)) { if (paramTab.SelectedIndex != 0) { paramTab.SelectedIndex = 0; } masterDBUser.Focus(); MessageBox.Show("登录名不能为空", "主数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(masterDBPwd.Text)) { if (paramTab.SelectedIndex != 0) { paramTab.SelectedIndex = 0; } masterDBPwd.Focus(); MessageBox.Show("密码不能为空", "主数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(hisDBServer.Text)) { if (paramTab.SelectedIndex != 1) { paramTab.SelectedIndex = 1; } hisDBServer.Focus(); MessageBox.Show("数据库地址不能为空", "历史数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(hisDBName.Text)) { if (paramTab.SelectedIndex != 1) { paramTab.SelectedIndex = 1; } hisDBName.Focus(); MessageBox.Show("数据库名称不能为空", "历史数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(hisDBUser.Text)) { if (paramTab.SelectedIndex != 1) { paramTab.SelectedIndex = 1; } hisDBUser.Focus(); MessageBox.Show("登录名不能为空", "历史数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(hisDBPwd.Text)) { if (paramTab.SelectedIndex != 1) { paramTab.SelectedIndex = 1; } hisDBPwd.Focus(); MessageBox.Show("密码不能为空", "历史数据库配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(interfaceServer.Text)) { if (paramTab.SelectedIndex != 2) { paramTab.SelectedIndex = 2; } interfaceServer.Focus(); MessageBox.Show("通信地址不能为空", "接口配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(interfaceUser.Text)) { if (paramTab.SelectedIndex != 2) { paramTab.SelectedIndex = 2; } interfaceUser.Focus(); MessageBox.Show("登录用户不能为空", "接口配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (String.IsNullOrWhiteSpace(interfacePwd.Text)) { if (paramTab.SelectedIndex != 2) { paramTab.SelectedIndex = 2; } interfacePwd.Focus(); MessageBox.Show("登录密码不能为空", "接口配置", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var masterDB = new DatabaseServerInfo(); masterDB.UniqueID = Guid.NewGuid(); masterDB.DatabaseIntention = EnmDBIntention.Master; masterDB.DatabaseType = ComUtility.DBNullDBTypeHandler(masterDBType.SelectedValue); masterDB.DatabaseIP = masterDBServer.Text; masterDB.DatabasePort = (int)masterDBPort.Value; masterDB.DatabaseName = masterDBName.Text; masterDB.DatabaseUser = masterDBUser.Text; masterDB.DatabasePwd = masterDBPwd.Text; masterDB.UpdateTime = DateTime.Now; var hisDB = new DatabaseServerInfo(); hisDB.UniqueID = Guid.NewGuid(); hisDB.DatabaseIntention = EnmDBIntention.History; hisDB.DatabaseType = ComUtility.DBNullDBTypeHandler(hisDBType.SelectedValue); hisDB.DatabaseIP = hisDBServer.Text; hisDB.DatabasePort = (int)hisDBPort.Value; hisDB.DatabaseName = hisDBName.Text; hisDB.DatabaseUser = hisDBUser.Text; hisDB.DatabasePwd = hisDBPwd.Text; hisDB.UpdateTime = DateTime.Now; var paramter = new InterfaceInfo(); paramter.UniqueID = Guid.NewGuid(); paramter.InterfaceIP = interfaceServer.Text; paramter.InterfacePort = (int)interfacePort.Value; paramter.InterfaceUser = interfaceUser.Text; paramter.InterfacePwd = interfacePwd.Text; paramter.InterfaceDetect = (int)interfaceDetect.Value; paramter.InterfaceInterrupt = (int)interfaceInterrupt.Value; paramter.InterfaceSyncTime = interfaceSyncTime.Checked; paramter.UpdateTime = DateTime.Now; registryEntity.SaveDatabaseServers(new List <DatabaseServerInfo>() { masterDB, hisDB }); registryEntity.SaveInterfaceParamters(paramter); MessageBox.Show("数据保存完成", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception err) { Common.WriteLog(DateTime.Now, EnmMsgType.Error, "System", "Delta.MPS.AccessSystem.ParamForm", err.Message, err.StackTrace); MessageBox.Show(err.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Create contents of connection strings used by the SqlConnection class. /// </summary> /// <param name="database">database information</param> /// <returns>Connection String</returns> public static String CreateConnectionString(DatabaseServerInfo database) { return(SQLHelper.CreateConnectionString(false, database.DatabaseIP, database.DatabasePort, database.DatabaseName, database.DatabaseUser, database.DatabasePwd, 120)); }