我想使用c#从表单更新ms-access数据库中的记录,但单击更新按钮时没有任何反应。没有错误也没有例外,它只是......它没有做任何事情。我很困惑,几乎疯了,试图找出问题所在。请帮助我,我是C#的新手。
我的表单有四个文本框和按钮(更新,添加,删除和清除)和列表视图。到目前为止,我得到了"添加"和"清除"好的,"删除"尚未奏效,我目前在"更新"。
方面遇到了麻烦listview充当表格,在我的数据库中显示员工的名字和姓氏时,无形地保存员工的ID(宽度0)。
每次从列表视图中选择记录时,计算机将获得所选行的不可见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)
{
}
}
问题
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);
}
}
}
}
注意:"记录更新"显示更新功能的消息框显示无法实际更新任何内容。
答案 0 :(得分:1)
按照它们在sql语句中出现的顺序添加参数。访问需要按照它们出现的顺序创建它们。
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();