[C#] 纯文本查看 复制代码
//数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
private const string connectionString = "Server=192.168.1.100;UserId=rootassword=root;Database=data;";
#region 测试数据库连接
private static bool IsCanConnectioned = false;
/// <summary>
/// 测试连接数据库是否成功
/// </summary>
/// <returns></returns>
public static bool ConnectionTest()
{
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
IsCanConnectioned = true;
}
catch
{
IsCanConnectioned = false;
}
finally
{
connection.Close();
}
if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
{
return IsCanConnectioned;
}
else
{
return IsCanConnectioned;
}
}
#endregion
[C#] 纯文本查看 复制代码
//数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
private const string connectionString = "Server=127.0.0.1;UserId=rootassword=root;Database=data;";
#region 测试数据库连接
private static bool IsCanConnectioned = false;
/// <summary>
/// 测试连接数据库是否成功
/// </summary>
/// <returns></returns>
public static bool ConnectionTest()
{
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
IsCanConnectioned = true;
}
catch
{
IsCanConnectioned = false;
}
finally
{
connection.Close();
}
if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
{
return IsCanConnectioned;
}
else
{
return IsCanConnectioned;
}
}
#endregion