簡體   English   中英

為什么我不能拋出自己的 sql 連接異常?

[英]Why I cannot throw my own sql connection exception?

我正在嘗試創建一個簡單的 WPF 應用程序。 我希望應用程序指示它是否未連接到數據庫,以便它無法發送請求並檢查其連接。 只需拋出並捕獲異常,異常就會打印在屏幕上

我的IsConnected方法:

public static bool IsConnected(SqlConnection conn)
{
    bool isConnected = false;

    try
    {
        if (conn == null)
        {
            throw new ConnectionException("It is not possible to connect to the database. Please check your settings and try again");
        }
        else
        {
            isConnected = true;
        }
    }
    catch (ConnectionException ex)
    {
        MessageBox.Show(ex.Message);
    }

    return isConnected;
}

我在哪里使用這個IsConnected()方法:

public User UserLogin(string email, string password)
{
    query = @"SELECT * FROM [User] WHERE email = @email AND password = @password";`

    User user = null;

    try
    {
        using (SqlConnection conn = new SqlConnection(DatabaseSingleton.connString))
        {
            if (DatabaseSingleton.IsConnected(conn))
            {
                using (SqlCommand command = new SqlCommand(query, conn))
                {
                    conn.Open();
                    command.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = email;
                    command.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = password;

                    SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        user = new User
                            {
                                Id = reader.GetInt32(0),
                                Name = reader.GetString(1),
                                Second_name = reader.GetString(2),
                                Email = reader.GetString(3),
                                Password = reader.GetString(4),
                                User_type = (Type_Of_User)Enum.ToObject(typeof(Type_Of_User), reader.GetInt32(5))
                            };
                    }

                    reader.Close();
                }
            }
        }
    }
    catch (InvalidInput e)
    {
        MessageBox.Show(e.Message);
    }

    return user;
}

由於數據庫訪問的工作方式,您的方法本質上是次優的。 如果超時,ADO 在任何情況下都應該拋出錯誤。

當您嘗試連接時

 SqlConnection conn = new SqlConnection

這將在超時之前嘗試連接默認時間段。 我認為那是 30 秒。 您可以在連接字符串中覆蓋它。

https://learn.microsoft.com/en-us/do.net/api/system.data.sqlclient.sqlconnection.connectiontimeout?view=do.net-plat-ext-7.0

您最初可以將其設置為 2 秒並減少等待時間。 不過這仍然是 2 秒,也許有一天您的數據庫會有點慢。

如果數據庫服務器可能可用也可能不可用,那么您可以首先嘗試使用 ping 來查看服務器是否存在並且正在工作。

在 c# 中使用 ping

當您嘗試對數據庫執行任何操作時,都會有延遲。 你發出一個請求。 dbms 接收它。 去獲取你的數據並返回它。

大多數延遲通常歸因於數據庫服務器上發生的事情。

您應該通過在另一個線程上執行所有數據庫訪問並返回結果來釋放您的 ui 線程。 您應該對所有數據庫訪問使用異步方法,包括打開數據庫。

例如 OpenAsync

https://learn.microsoft.com/en-us/do.net/api/system.data.common.dbconnection.openasync?view.net-7.0

而不是 voids 使用 async Task 進行數據庫訪問。

https://www.pluralsight.com/guides/using-task-run-async-await

暫無
暫無

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

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