Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保持连接打开以监视和SQL Server连接_C#_Sql Server - Fatal编程技术网

C# 保持连接打开以监视和SQL Server连接

C# 保持连接打开以监视和SQL Server连接,c#,sql-server,C#,Sql Server,我正在尝试监视SQL Server数据库连接,但我遇到了问题或它的行为符合设计要求。 我想这样做: Open Connection Write "Open" Leave it open for 1 minute (using System.Threading.Thread.Sleep) If the connection drops during this time write "Down" to the console Close the connection Write "

我正在尝试监视SQL Server数据库连接,但我遇到了问题或它的行为符合设计要求。
我想这样做:

Open Connection
   Write "Open"
Leave it open for 1 minute (using System.Threading.Thread.Sleep)
   If the connection drops during this time write "Down" to the console
Close the connection
   Write "Closed" to the console
Repeat forever
因此,当我的SQL Express服务启动后,我就开始编写我得到的代码

打开
关闭
打开
封闭的

(间隔适当的时间) 然后,在我再次看到“Open”之后,我停止了我的sqlexpress服务,接下来的几行仍然是

打开
关闭
打开
封闭的

现在,如果在SQL Express服务停止时重新启动程序,则会得到正确的异常。
这是我的密码

   static void Main(string[] args)
    {
        //
        string strConnectionString;
        string strEnabled;
        string strPath;
        strConnectionString = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\GlobalNet Direct\\DatabaseMonitor", "ConnectionString", false).ToString();
        strEnabled = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\GlobalNet Direct\\DatabaseMonitor", "Enabled", false).ToString();
        strPath = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\GlobalNet Direct\\DatabaseMonitor", "LogPath", false).ToString(); 
        do
        {
            strEnabled = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\GlobalNet Direct\\DatabaseMonitor", "Enabled", false).ToString(); 


            //System.Threading.Thread.Sleep(15000);


            SqlConnection myConnection = new SqlConnection(strConnectionString);

            try
            {
                myConnection.Open();

                Console.WriteLine(myConnection.State.ToString());
                System.Threading.Thread.Sleep(1000);
            }
            catch (Exception)
            {
                Console.WriteLine("Down");

            }
            try
            {
                if (myConnection.State.ToString() == "Open")
                {
                    myConnection.Close();
                    System.Threading.Thread.Sleep(1000);
                    Console.WriteLine(myConnection.State.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
        }while (strEnabled == "True");

    }

你知道那是一秒钟,不是一分钟…废话,是的,我知道。我不耐烦,将其设置为1秒以进行测试,然后它将其包含在我的粘贴中。如果SQL express服务实际关闭需要几秒钟,那么在它实际失败之前,您可能会看到一些打开/关闭的消息。这可能与连接池有关。如果您的应用程序认为它有一个可以使用的连接,那么它可能实际上并没有与sql server对话。如果是这样,那么如果在open命令之后执行select 0,它将引发异常。