快速判断SQL数据连接是否正常

我这里先做了一个Button按钮。用于测试所用!其Click事件:

private void Btn_Test_Click(object sender, EventArgs e) { if (!ContentSQL()) { MessageBox.Show("服务器连接失败!", "服务器连接", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show("服务器连接成功!", "服务器连接", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
private bool ContentSQL() { try { _sqlClient = new TcpClient(); var ar = _sqlClient.BeginConnect("192.168.0.1", 1433, null, null); ar.AsyncWaitHandle.WaitOne(500); return _sqlClient.Connected; } catch (Exception e) { ErrorLog.Add(e.ToString()); MessageBox.Show(e.ToString(),"连接失败",MessageBoxButtons.OK,MessageBoxIcon.Error); return false; } }

服务器IP:192.168.0.1

 

相关文章