更新时间:2022-12-07 17:36:42
嗨索菲亚,
在更新语句中,在查询中添加@RollNo参数。
检查您的变量_FirstName,_LastName,_CollegeName等使用最新值更新,RollNo字段未添加到参数列表中cmd.Parameters .AddWithValue( @ Fname,_ FirstName);
cmd.Parameters.AddWithValue( @ Lname,_ LastName);
cmd.Parameters.AddWithValue( @ Clgname,_ CollegeName);
cmd.Parameters.AddWithValue( @ Mark,_ MarksObtained);
cmd.Parameters.AddWithValue( @ Exam,_ Exam);
cmd.Parameters.AddWithValue( @ RollNo;,?????); //修改此项line
什么是stud变量在这里做什么?而且你没有使用stud var值来更新语句。检查一下
Hi ,
I have create a table "StudentsInformation" in ms access 2007 database.I successfully established the connection also with WPF and ms access 2007.
I am able to add new records in to the table and i am able to view the records also .
The problem is i am unable to update one row.
If i make one change in one field in one row ,it gets reflected in all the rows.
For ex:
if update last name for a particular record ,the last name gets updated in all records.
Please help.
Please find the below mentioned code
My class name is Students.cs
public void Update()
{
OleDbConnection con = new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SR0150\\Documents\\Students.accdb");
con.Open();
OleDbCommand cmd = new OleDbCommand("Update StudentInformation set Fname=@Fname,Lname=@Lname,Clgname=@Clgname,Mark=@Mark Where RollNo=@RollNo",con);
//assign the values and add parameters
cmd.Parameters.AddWithValue("@Fname", _FirstName);
cmd.Parameters.AddWithValue("@Lname", _LastName);
cmd.Parameters.AddWithValue("@Clgname", _CollegeName);
cmd.Parameters.AddWithValue("@Mark", _MarksObtained);
cmd.Parameters.AddWithValue("@Exam", _Exam);
cmd.ExecuteNonQuery();
}
My MainWindow.xaml.cs code
private void btnUpDate_Click(object sender, RoutedEventArgs e)
{
stud.RollNo = Convert.ToInt32(txtRollNo.Text);
stud.FirstName = txtFirstName.Text;
stud.LastName = txtLastName.Text;
stud.CollegeName = txtClgName.Text;
stud.MarksObtained = txtMarks.Text;
stud.Exam = txtExamResults.Text;
stud.Update();//call object for update record
MessageBox.Show("Update Record", "Update");
}
Thanks
Sophia
Hi sophia,
In update statement add the @RollNo parameter in your query.
Check your variables _FirstName, _LastName, _CollegeName etc are updated with recent values and RollNo field is not added to the parameters listcmd.Parameters.AddWithValue("@Fname", _FirstName); cmd.Parameters.AddWithValue("@Lname", _LastName); cmd.Parameters.AddWithValue("@Clgname", _CollegeName); cmd.Parameters.AddWithValue("@Mark", _MarksObtained); cmd.Parameters.AddWithValue("@Exam", _Exam); cmd.Parameters.AddWithValue("@RollNo;, ?????); // Modify this line
what is stud variable doing here? and you are not using stud var value to update statement. check that too.