简体   繁体   English

使用C#更新MS-Access数据库中的记录未知错误

[英]Updating a record in ms-access database using c# unknown error

I wanted to update a record in my ms-access database from a form using c# but nothing happens when I clicked my update button. 我想使用c#从表单中更新ms-access数据库中的记录,但是当我单击更新按钮时没有任何反应。 There are no errors and no exceptions, it's just.. it doesn't do anything. 没有错误,也没有例外,这只是..它什么都不做。 I am puzzled and nearly going mad trying to figure out what is wrong. 我很困惑,几乎发疯,试图找出问题所在。 Please help me, I am new to C#. 请帮助我,我是C#的新手。

My form has four textboxes and buttons (update, add, delete, and clear) and a listview. 我的表单有四个文本框和按钮(更新,添加,删除和清除)和一个列表视图。 So far, I got the "add" and "clear" okay, the "delete" is not yet worked, and I am currently having trouble in the "update". 到目前为止,我可以“添加”和“清除”了,“删除”还没有用,并且我目前在“更新”中遇到了麻烦。

The listview acts as a table, invisibly holds the ID (width 0) of an employee while displaying the firstname and lastname of an employee from my database. 该列表视图充当一个表,在显示我数据库中某个雇员的名字和姓氏时,它不可见地保留了该雇员的ID(宽度0)。

Each time a record is selected from the listview, the computer will get the invisible ID number of the selected row, and will display the corresponding data associated with the ID to the textboxes (firstname, middlename, lastname, address, position) 每次从列表视图中选择一条记录时,计算机都会获得所选行的不可见ID号,并将在文本框中显示与该ID相关的相应数据(名字,中间名,姓,地址,位置)

namespace WindowsFormsApp1
{


public partial class Form1 : Form
{

    string employeeID; //global variable 
    public void LvRefresh()
    {

        //this method is used to load records from the database
        //to the listview, it is also used to REFRESH the records of the listview.

        listView1.Items.Clear();
        listView1.View = View.Details;
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
        con.Open();

        OleDbCommand cmdEmp = new OleDbCommand("Select ID,FN,LN from Employees", con);
        OleDbDataReader rdrEmp = cmdEmp.ExecuteReader();

        if (rdrEmp.HasRows)
        {

            while (rdrEmp.Read())
            {

                ListViewItem listitem = new ListViewItem(rdrEmp["ID"].ToString());
                listitem.SubItems.Add(rdrEmp["FN"].ToString());
                listitem.SubItems.Add(rdrEmp["LN"].ToString());
                listView1.Items.Add(listitem);

            }

        }
        con.Close();
        rdrEmp.Close();
        cmdEmp.Dispose();
    }



    public Form1()
    {

        InitializeComponent();
        LvRefresh(); //load the ID, FN, MN from the database to the listview
    }

    private void button2_Click(object sender, EventArgs e)
    {

        //clear the textboxes
        textADRS.Clear();
        textFN.Clear();
        textMN.Clear();
        textLN.Clear();
        textPOS.Clear();
    }

    private void buttonSUB_Click(object sender, EventArgs e)
    {

        //add records

        string Adrs = textADRS.Text;
        string Fname = textFN.Text;
        string Mname = textMN.Text;
        string Lname = textLN.Text;
        string Pos = textPOS.Text;

        try
        {

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
            OleDbCommand cmd = new OleDbCommand("Insert into Employees (FN,MN,LN,[Address],[Position]) Values (@FirstName,@MidName,@LastName,@Address,@Position)", con);
            cmd.Parameters.Add(new OleDbParameter("@FirstName", Fname));
            cmd.Parameters.Add(new OleDbParameter("@MidName", Mname));
            cmd.Parameters.Add(new OleDbParameter("@LastName", Lname));
            cmd.Parameters.Add(new OleDbParameter("@Address", Adrs));
            cmd.Parameters.Add(new OleDbParameter("@Position", Pos));
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Submitted", "Nice!");
            con.Close();
            cmd.Dispose();
            LvRefresh(); //refresh listview

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //codes that are responsible for the reflecting of records to the textboxes

        try
        {

            ListViewItem item = listView1.SelectedItems[0];
            employeeID = item.Text; //update the value of global variable employeeID
            textBox1.Text = employeeID;

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
            con.Open();

            OleDbCommand cmdEmp = new OleDbCommand("Select * from Employees where ID = @empID", con);
            cmdEmp.Parameters.Add("@empID", employeeID);
            OleDbDataReader rdrEmp = cmdEmp.ExecuteReader();

            if (rdrEmp.HasRows)
            {

                while (rdrEmp.Read()) 
                {

                    textFN.Text = rdrEmp["FN"].ToString();
                    textMN.Text = rdrEmp["MN"].ToString();
                    textLN.Text = rdrEmp["LN"].ToString();
                    textADRS.Text = rdrEmp["Address"].ToString();
                    textPOS.Text = rdrEmp["Position"].ToString();

                }

            }
            con.Close();
            rdrEmp.Close();
            cmdEmp.Dispose();
        }

        catch (Exception)
        {

        }

    }

THE PROBLEM 问题

    private void buttonUpdate_Click(object sender, EventArgs e)
    {
        string Adrs = textADRS.Text;
        string Fname = textFN.Text;
        string Mname = textMN.Text;
        string Lname = textLN.Text;
        string Pos = textPOS.Text;

        try
        {

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
            con.Open();

            OleDbCommand cmdEmp = new OleDbCommand("Update Employees set FN = @FirstName, MN = @MidName, LN = @LastName, [Address] = @Address, [Position] = @Pos where ID = @empID", con);
            cmdEmp.Parameters.Add(new OleDbParameter("@empID", employeeID));
            cmdEmp.Parameters.Add(new OleDbParameter("@FirstName", Fname));
            cmdEmp.Parameters.Add(new OleDbParameter("@MidName", Mname));
            cmdEmp.Parameters.Add(new OleDbParameter("@LastName", Lname));
            cmdEmp.Parameters.Add(new OleDbParameter("@Address", Adrs));
            cmdEmp.Parameters.Add(new OleDbParameter("@Position", Pos));
            cmdEmp.ExecuteNonQuery();

            MessageBox.Show("Record Updated!", "Nice!");
            con.Close();
            cmdEmp.Dispose();
            LvRefresh(); //refresh listview
        }

        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }

    }
}
}

NOTE: THE "RECORD UPDATED" MESSAGEBOX IN THE UPDATE FUNCTIONALITY IS DISPLAYED DESPITE NOT ACTUALLY UPDATING ANYTHING. 注意:尽管实际上没有更新任何内容,但更新功能中的“记录已更新”消息框已显示。

Add your parameters in the order they appear in your sql statment. 按照它们在sql语句中出现的顺序添加参数。 Access needs them created in the order they appear. Access需要按出现的顺序创建它们。

 OleDbCommand cmdEmp = new OleDbCommand("Update Employees set FN = @FirstName, MN = @MidName, LN = @LastName, [Address] = @Address, [Position] = @Pos where ID = @empID", con);
        cmdEmp.Parameters.Add(new OleDbParameter("@FirstName", Fname));
        cmdEmp.Parameters.Add(new OleDbParameter("@MidName", Mname));
        cmdEmp.Parameters.Add(new OleDbParameter("@LastName", Lname));
        cmdEmp.Parameters.Add(new OleDbParameter("@Address", Adrs));
        cmdEmp.Parameters.Add(new OleDbParameter("@Position", Pos));
        cmdEmp.Parameters.Add(new OleDbParameter("@empID", employeeID));
       cmdEmp.ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM