簡體   English   中英

如何捕獲和處理“找不到或無法訪問服務器”

[英]How to catch and handle “the server was not found or was not accessible”

我正在使用下面的代碼。 關閉SQL Server時出現錯誤

服務器未找到或無法訪問

當我在VS2013中處於調試模式時(如預期)。 如果我在IDE外部啟動程序,它將崩潰。

如果無法聯系SQL Server,如何處理並處理這種情況?

using (SqlConnection con = new SqlConnection(connectionString))
{
    CmdString = "SELECT * FROM Conversions WHERE Completed = 'False'";
    SqlCommand cmd = new SqlCommand(CmdString, con);

    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable("Conversions");

    sda.Fill(dt);  // this is where the exception happens
}

這行得通,謝謝大家!

using (SqlConnection con = new SqlConnection(connectionString))
            {
                CmdString = "SELECT * FROM Conversions WHERE Completed = 'False'";
                SqlCommand cmd = new SqlCommand(CmdString, con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable("Conversions");


                try
                {
                    sda.Fill(dt);
                }
                catch (SqlException sqlexc)
                {
                    MessageBox.Show("Error:  " + sqlexc.Message);
                    Environment.Exit(0);
                }
                catch (Exception exc)
                {
                    MessageBox.Show("Error:  " + exc.Message);
                    Environment.Exit(0);
                }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM