Как вставить в базу данных MS Access в Windows Forms: C# - PullRequest
0 голосов
/ 07 марта 2020

Я занимаюсь этим уже несколько дней, и мне отчаянно нужна помощь. У меня есть назначение класса, в котором мне нужно создать приложение Windows Forms, которое может выбрать запись из созданных мной таблиц, а затем также вставить запись.

Я застрял на кнопке отправки и вставил ее в часть. Человек, работающий с приложением, должен иметь возможность щелкнуть правило, чтобы изменить его, выбрать сотрудника, которым он является, а затем вставить предложенное изменение в созданный мною текстовый ящик. Это где я застрял, и я не могу понять это. Пожалуйста, помогите, я схожу с ума!

    private void Submit_button_Click(object sender, EventArgs e)
    {
        string Rule = Rule_box.Text;
        string Employee = Employee_box.Text;
        string Proposed = Proposed_changes.SelectedText;
        string ConnectString = "provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=Z:\\BA 371\\BA371GroupProject.accdb";

        //EMPLOYEE
        /* --CONNECT TO DATABASE-- */
        //make a connection object
        OleDbConnection Connection = new System.Data.OleDb.OleDbConnection(ConnectString);

        //make an OleDbCommand object
        OleDbCommand SQLCommand = null;

        //make an OleDbDataReader
        OleDbDataReader SQLDataReader = null;

        //open the connection
        try
        {
            Connection.Open();
        }
        catch (System.Exception)
        {
            MessageBox.Show("Error opening the database connection.");
            return;
        }

        /* --SQL TO FIND EMPLOYEE ID BASED ON LAST NAME-- */
        string findEmployee = "SELECT Employee_ID FROM Employee WHERE Employee_last_name = {'0'}" + Employee;

        //make a SQLCommand from the query
        SQLCommand = new System.Data.OleDb.OleDbCommand(findEmployee, Connection);

        //execute the SQLCommand and load the results into the reader
        try
        {
            SQLDataReader = SQLCommand.ExecuteReader();
        }
        catch (System.Exception)
        {
            MessageBox.Show("Failed to load results into table. Please try again.");
            return;
        }

        /* --READ RESULT AND ASSIGN TO employeeID to int-- */
        int Employee_ID = -99;

        try
        {
            SQLDataReader.Read();
            Employee_ID = SQLDataReader.GetInt32(0);

            //we are done reading; close the Reader
            SQLDataReader.Close();
        }
        catch (System.Exception)
        {
            MessageBox.Show("Error closing the database connection.");
            return;
        }

        try
        {
            Connection.Open();
            string newQuery = "INSERT INTO Proposal (Proposal_ID, Proposal_employee_ID," +
            " Proposal_status_ID, Proposal_text, Last_modified_date) VALUES " +
            "(null," + Employee_ID + ", 1," + Proposed_changes.SelectedText + ", date())";

            SQLCommand = new OleDbCommand(newQuery, Connection);
            SQLCommand.ExecuteNonQuery();
            MessageBox.Show("Record Added");
            Connection.Close();
        }
        catch (Exception)
        {
            MessageBox.Show("Error!");
        }

        //Admin RULE 
        //do I need to open the connection again?
        try
        {
            Connection.Open();
        }
        catch (System.Exception)
        {
            MessageBox.Show("Error opening the database connection.");
            return;
        }

        /* --SQL TO FIND admin rule ID based on admin rule name - */
        string findRule = "SELECT Admin_rule_ID FROM Admin_Rule WHERE Admin_rule_name = {0}" + Rule;

        //make a SQLCommand from the query
        SQLCommand = new System.Data.OleDb.OleDbCommand(findRule, Connection);

        //execute the SQLCommand and load the results into the reader
        try
        {
            SQLDataReader = SQLCommand.ExecuteReader();
        }
        catch (System.Exception)
        {
            MessageBox.Show("Failed to load results into table. Please try again.");
            return;
        }
        /* --READ RESULT AND ASSIGN TO employeeID STRING-- */
        int Admin_rule_ID = -99;
        try
        {
            SQLDataReader.Read();
            Admin_rule_ID = SQLDataReader.GetInt32(0);

            //we are done reading; close the Reader
            SQLDataReader.Close();
        }
        catch (System.Exception)
        {
            MessageBox.Show("Error closing the database connection.");
            return;
        }
...