簡體   English   中英

如果數據庫中已存在值,如何顯示消息?

[英]How can I display a message if a value already exists in database?

我目前正在用Visual Studio編寫我的第一個.Net&C#應用程序,並且需要從該應用程序將生成的值寫入MySQL。 目前,我可以很好地編寫值-但我需要能夠檢查值是否存在,並在存在的情況下顯示該行,否則將新行插入表中。 我的連接字符串在表格頂部定義。

我已經定義了以下內容,如果LicenseKey列中不存在重復值,它將成功寫入數據庫。 如果存在重復項,則會引發未處理的異常。

private void SaveDetails()
{
    // MySQL 'insert' command
    string InsertNewLicense = "insert into BCOM.LicenseDetails(LicenseeName,ComputerName,ContactName,ContactEmail,LicenseKey,CreationDate) values('" +this.textBoxLicenseeName.Text+ "','" +this.textBoxComputerName.Text+ "','" +this.textBoxContactName.Text+ "','" +this.textBoxContactEmail.Text+ "','" +this.textBoxLicenseKey.Text+ "','" +this.textBoxCreationDate.Text+ "');";
    //MySQL instance details
    MySqlConnection InsertLicenseDetails = new MySqlConnection(LicenseDatabaseConnection);
    //MySQL command execution
    MySqlCommand InsertCommand = new MySqlCommand(InsertNewLicense, InsertLicenseDetails);
    // Handles command outputs.
    MySqlDataReader InsertReader;
    //Opens connection to run query on database
    InsertLicenseDetails.Open();
    // Here our query will be executed and data saved into the database.
    MessageBox.Show("License Details Saved. Please ensure you have emailed the license to the customer.");
    while (InsertReader.Read())
    {

    }
    InsertLicenseDetails.Close();
}

我想做的是在執行不同的操作之前,在LicenseKey列上運行檢查以查看該值是否存在。 如果該值不存在,我想在表中插入新行(就像現有命令一樣)。

exist, I would like to pop up a form showing the values from the line that the duplicate appears in as a form. 但是,如果存在該值,那么我想彈出一個表格,該表格顯示重復項以表格形式出現的那一行的值。

我將在哪里放置事件處理程序以讀取MySQLException值? 對於重復的值或沒有數據庫響應,我必須響應什么異常?

我同意其他人在評論中所說的,您可以更改SQL Query來進行檢查,而不用進行2。

IF(SELECT ... WHERE A = B)
     RETURN THAT THE VALUE ALREADY EXISTS    
ELSE 
     INSERT NEW VALUE

關於SQL注入和參數化查詢也有很好的評論。 查詢字符串應該看起來更像

INSERT into BCOM.LicenseDetails(LicenseeName,ComputerName,ContactName,ContactEmail,LicenseKey,CreationDate) values(@LicenseeName, @ComputerName, @ContactName ...);

和您的SqlCommand被參數化

InsertCommand.Paramaters.AddWithValue("@LicenseeName", this.textBoxLicenseeName.Text);
InsertCommand.Paramaters.AddWithValue("@ComputerName", this.textBoxComputerName.Text);
...

那應該是一個讓您前進的好開始。

在查詢了一段時間后,我決定嘗試使用其他方法-我選擇使用count(*)查詢,而不是直接檢查是否存在。 當我單擊表單上的保存按鈕時,buttonClick_event調用SaveDetails(),該命令運行以下命令:

    private void SaveDetails()
    {
        string InsertNewLicense = "INSERT into BCOM.LicenseDetails(LicenseeName,ComputerName,ContactName,ContactEmail,LicenseKey,CreationDate) values(@LicenseeName, @ComputerName, @ContactName, @ContactEmail, @LicenseKey, @CreationDate)";
        string LicenseExistence = "SELECT COUNT(*) FROM BCOM.LicenseDetails WHERE LicenseKey LIKE @LicenseKey";

        MySqlConnection LicenseDetails = new MySqlConnection(LicenseDatabaseConnection);

        MySqlCommand InsertCommand = new MySqlCommand(InsertNewLicense, LicenseDetails);
        InsertCommand.Parameters.AddWithValue("@LicenseeName", this.textBoxLicenseeName.Text);
        InsertCommand.Parameters.AddWithValue("@ComputerName", this.textBoxComputerName.Text);
        InsertCommand.Parameters.AddWithValue("@ContactName", this.textBoxContactName.Text);
        InsertCommand.Parameters.AddWithValue("@ContactEmail", this.textBoxContactEmail.Text);
        InsertCommand.Parameters.AddWithValue("@LicenseKey", this.textBoxLicenseKey.Text);
        InsertCommand.Parameters.AddWithValue("@CreationDate", this.textBoxCreationDate.Text);

        MySqlCommand QueryCommand = new MySqlCommand(LicenseExistence, LicenseDetails);
        QueryCommand.Parameters.AddWithValue("@LicenseKey", this.textBoxLicenseKey.Text);

        MySqlDataReader InsertReader;

        LicenseDetails.Open();
        if ((int)(long)QueryCommand.ExecuteScalar() >0)
        {
            MessageBox.Show("This license already exists in the database.");
        }
        else
        {
            InsertReader = InsertCommand.ExecuteReader();
            MessageBox.Show("License Details Saved. Please ensure you have emailed the license to the customer.");
            while (InsertReader.Read())
            {

            }
        }
        LicenseDetails.Close();

因此,如果對許可證密鑰的查詢根本沒有任何結果(返回的行數多於0),則會彈出一個消息框,顯示密鑰已存在。 如果結果行數為0,則運行insert命令。

可以通過查看MySQL命令注釋,使用phpMyAdmin進行測試,與現有項目進行在線匹配以及以下方面的支持來解決:SELECT查詢得到@Seige的大力支持。 根據Sani Huttunen的建議,在Seige的幫助下對查詢進行了參數化。 非常感謝他們倆。 轉換為計數方法是在另一個在線社區的一位編碼員的建議下完成的-一個好朋友和出色的編碼員。

暫無
暫無

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

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