时间间隔检查数据库连接是否在后端每 3 分钟存在一次,而应用程序是 运行 C#

Timeinterval Check if Database Connection exists every 3 minutes at the backend while application is running C#

在我的应用程序开始时,我能够成功地检查 SQL 服务器是否 运行,如果不是 运行 启动特定服务,然后检查是否连接了特定的数据库。但我想学习如何检查数据库是否在某个时间连接 interval.If 数据库断开连接,即如果数据库在后端被删除或重命名,而应用程序是 运行 那么应用程序应该自动终止.我怎样才能做到最好?

如何定时调用数据库连接函数?如果未连接,则应终止应用程序。

namespace CLearning
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool isServiceRunning = false;

            // Applying the required visual styles - we need to do it before we display any single form on the screen. 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            //Check specific SQL Server and database is running
            isServiceRunning = DataAccessContext.CheckSQLServiceStatus();

            if (isServiceRunning)
            {
                //SQL is running, checking for Local DB connection

                bool isLocalDAOConnected = DataAccessContext.CheckIsLocalDAOConnected();
                if (!isLocalDAOConnected)
                {
                    MessageBox.Show(
                        "Database connection is not available on the machine. The application will termiante.",
                        "No database connection available",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return;
                }

                Application.Run(new Form1());

            }
        }
    }
}

检查服务器是否 运行 和数据库是否已连接的 DataAccess class 在这里

namespace CLearning.DataAccess
{
    public class DataAccessContext 
    {

        private static bool _isSQLServiceRunning = false;
        private static bool _isLocalDAOConnected = false;

        public static bool CheckSQLServiceStatus()
        {
          string myServiceName = "MSSQL$CLearning"; //service name of SQL Server Express
          string status; //service status (For example, Running or Stopped)

            //service status: For example, Running, Stopped, or Paused
            ServiceController mySC = new ServiceController(myServiceName);

            try
            {
                status = mySC.Status.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("SQL Service not found. It is probably not installed, application will terminate. [exception=" + ex.Message + "]");
                return _isSQLServiceRunning;
            }
            //service status: For example, Running, Stopped, or Paused
            _isSQLServiceRunning = true;
            //if service is Stopped or StopPending, you can run it with the following code.
            if (mySC.Status.Equals(ServiceControllerStatus.Stopped) | mySC.Status.Equals(ServiceControllerStatus.StopPending))
            {
                try
                {
                    mySC.Start();
                    var timeout = new TimeSpan(0, 0, 5);
                    mySC.WaitForStatus(ServiceControllerStatus.Running,timeout);
                    _isSQLServiceRunning = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error in starting the service: " + ex.Message);
                    _isSQLServiceRunning = false;
                }
            }
            return _isSQLServiceRunning;
        }

        public static bool CheckIsLocalDAOConnected()
        {
            try
            {
                GlobalConfig.Connection.TestConnection(); //SQL QUERY That checks for openning the connection
                return _isLocalDAOConnected = true;
            }
            catch (Exception)
            {
                return _isLocalDAOConnected;
            }
        }
    }
}

请指导。

谢谢 AA

每三分钟检查一次数据库是否可用?

在您的状态线程中,每三分钟执行一次:

  1. 打开与数据库服务器的连接。
  2. 使用 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  3. 将连接设置为非阻塞读取
  4. 执行 SELECT TOP 1 somecolumn FROM a_key_table;
  5. 形式的原始查询
  6. 关闭连接。

try{}catch(){} 块中完成所有这些工作。如果出现异常,则表明您的数据库出现了重要问题——您无法从应用程序的重要 tables.

中读取一行。

异常会告诉你出了什么问题(如果你关心的话):连接超时、身份验证失败、无法读取 table 等