我需要开发一个例程,它将每5分钟触发一次,以检查SQL Server (10到12)的列表是否已启动并正在运行。
有没有一种方法可以简单地从C# one "ping“SQL Server,并且只需最少的代码和sql操作需求?
发布于 2010-03-14 05:22:18
执行SELECT 1并检查ExecuteScalar是否返回1。
发布于 2012-03-30 21:23:59
当服务器停止或暂停连接时,我遇到了EF的问题,我提出了同样的问题。因此,为了完整地回答上面的问题,下面是代码。
/// <summary>
/// Test that the server is connected
/// </summary>
/// <param name="connectionString">The connection string</param>
/// <returns>true if the connection is opened</returns>
private static bool IsServerConnected(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}发布于 2013-07-09 14:30:02
请参阅GitHub上的以下项目:https://github.com/ghuntley/csharp-mssql-connectivity-tester
try
{
Console.WriteLine("Connecting to: {0}", AppConfig.ConnectionString);
using (var connection = new SqlConnection(AppConfig.ConnectionString))
{
var query = "select 1";
Console.WriteLine("Executing: {0}", query);
var command = new SqlCommand(query, connection);
connection.Open();
Console.WriteLine("SQL Connection successful.");
command.ExecuteScalar();
Console.WriteLine("SQL Query execution successful.");
}
}
catch (Exception ex)
{
Console.WriteLine("Failure: {0}", ex.Message);
}https://stackoverflow.com/questions/2440060
复制相似问题