/// <summary> /// Close this Form. /// </summary> private void CancelBtn_Click(object sender, EventArgs e) { try { if (TestStatus != EnmTestStatus.Default) { CancelBtn.Enabled = false; TestStatus = EnmTestStatus.Stop; } else { Close(); } } 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> /// 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); } }