簡體   English   中英

如何檢查MS Access數據庫文件中是否已存在ID,以及如何在TextBox中使用TextChanged進行檢查?

[英]How to check if ID already exists in MS Access database file, and how to check it by using TextChanged in TextBox?

我正在處理我的程序的一部分,我正在使用提供的條目ID刪除條目。

截至目前,我正在刪除用戶指定的任何條目。 這很好,但我想要做的是告知用戶沒有這樣的ID要刪除。 此外,我正在使用文本框TextChanged ,它允許我在用戶輸入時檢查用戶輸入中的某些內容。

現在,如何檢查條目ID是否已存在? 我應該在if語句中包含哪些內容來執行此操作?

另外,有沒有辦法通過使用TextChanged事件處理程序檢查? 我不確定,因為我知道如果我在TextChanged事件中打開和關閉連接,那么每次用戶輸入時都會打開/關閉連接, 所以我認為這不是一個好主意 但是我怎么能避免這種情況,所以我可以實時做到這一點? 也許當用戶停止輸入,然后花一兩秒來檢查輸入ID?

這是我刪除條目窗口的代碼:

public partial class DeleteEntryWindow : Form
{
    string user, pass, filePath;

    // Initializing MainWindow form.
    MainWindow mainWindow;

    public DeleteEntryWindow()
    {
        InitializeComponent();

        txtEntryID.TextChanged += new EventHandler(ValidateInput);
    }

    public DeleteEntryWindow(MainWindow viaParameter, 
        string user, string pass, string filePath)
        : this()
    {
        mainWindow = viaParameter;
        this.user = user;
        this.pass = pass;
        this.filePath = filePath;
    }

    private void ValidateInput(object sender, EventArgs e)
    {
        int intNumber;

        if (!string.IsNullOrEmpty(txtEntryID.Text) &&
            int.TryParse(txtEntryID.Text, out intNumber) &&
            intNumber > 0)
        {
            lblMessage.Text = "Entry ID is valid.";
            lblMessage.ForeColor = Color.Green;
            btnDeleteEntry.Enabled = true;
        }
        else
        {
            lblMessage.Text = "You must enter Entry ID number!";
            lblMessage.ForeColor = Color.IndianRed;
            btnDeleteEntry.Enabled = false;
        }
    }

    private void btnDeleteEntry_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show
            ("Are you sure you want to remove this entry?",
            "Information", MessageBoxButtons.YesNo,
            MessageBoxIcon.Information);

        if (result == DialogResult.Yes)
        {
            // SQL query which will delete entry by using entry ID.
            string sql = "DELETE FROM PersonalData WHERE DataID = " +
                txtEntryID.Text;

            DeleteData(sql);

            lblMessage.Text = "Entry was deleted!";
            lblMessage.ForeColor = Color.Green;
        }
        else
        {
            // Do nothing.
        }
    }

    private void DeleteData(string sql)
    {
        HashPhrase hash = new HashPhrase();

        string hashShortPass = hash.ShortHash(pass);

        // Creating a connection string. Using placeholders make code
        // easier to understand.
        string connectionString =
            @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};
              Persist Security Info=False; Jet OLEDB:Database Password={1};";

        using (OleDbConnection connection = new OleDbConnection())
        {
            // Creating command object.
            // Using a string formatting let me to insert data into
            // place holders I have used earlier.
            connection.ConnectionString =
                string.Format(connectionString, filePath, hashShortPass);

            using (OleDbCommand command = new OleDbCommand(sql, connection))
            {
                OleDbParameter prmDataID = new OleDbParameter
                    ("@DataID", txtEntryID.Text);

                command.Parameters.Add(prmDataID);

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }
    }
}

要檢查ID是否已存在,您將需要像刪除方法一樣使用SQL。 以下可能為您提供一個起點:

private bool DoesIDExist(string ID)
{
    string filePath = ""; //TODO
    string hashShortPass = ""; //TODO
    DataTable temp = new DataTable();
    bool result = false;
    string connectionString =""; //TODO

    using (OleDbConnection connection = new OleDbConnection(ConnectionString))
    {

            string sql = @"SELECT * FROM PersonalData WHERE DataID = @DataID";


            using (OleDbCommand command = new OleDbCommand(sql, connection))
            {
                command.Parameters.Add(new OleDbParameter("@DataID", ID));

                using (OleDbDataAdapter oda = new OleDbDataAdapter(command))
                {
                    try
                    {
                        oda.Fill(temp);

                        if (temp != null && temp.Rows.Count > 0)
                            result = true; //ID exists

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: " + ex.Message);
                    }
                }
            }
    }
return result;
}

暫無
暫無

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

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