C#验证数据库是否连接成功
private string ConnectionStringLW = "server=192.168.1.1,1430(端口号) ;database=DB;uid=sa;pwd=123456" //数据库连接字符串
/// <summary>
/// 本地数据库连接是否成功
/// </summary>
/// <returns></returns>
public bool connectTestW()
{
bool result = false;
//获取数据库连接字符串
ConnectionString = ConnectionStringLW();
//创建连接对象
mySqlConnection = new SqlConnection(ConnectionString);
try
{
mySqlConnection.Open();
if (mySqlConnection.State == ConnectionState.Open)
{
MessageBox.Show("连接成功!");
result = true;
}
else
{
MessageBox.Show("连接失败!");
}
}
catch
{
MessageBox.Show("连接失败!");
}
finally
{
mySqlConnection.Close();
}
return result;
}