在ms access 2007数据库中更新查询 [英] Update Query in ms access 2007 database

查看:88
本文介绍了在ms access 2007数据库中更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在ms access 2007数据库中创建了一个表StudentsInformation。我成功建立了与WPF和ms access 2007的连接。 />
我可以在表格中添加新记录,我也可以查看记录。



问题是我无法更新一行。



如果我在一行中的一个字段中进行一次更改,它会反映在所有行中。



例如:



如果更新特定记录的姓氏,姓氏将在所有记录中更新。



请帮忙。



请找到下面提到的代码



我的班级名是Students.cs



public void更新()

{

OleDbConnection con = new OleDbConnection (Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\SR0150 \\Documents\\Stude nts.accdb);

con.Open();

OleDbCommand cmd = new OleDbCommand(更新StudentInformation设置Fname = @Fname,Lname = @Lname,Clgname = @ Clgname,Mark = @ Mark Where RollNo = @ RollNo,con);

//分配值并添加参数

cmd.Parameters.AddWithValue(@ Fname ,_ _ FirstName);

cmd.Parameters.AddWithValue(@ Lname,_ LastName);

cmd.Parameters.AddWithValue(@ Clgname,_ COLLlegeName);

cmd.Parameters.AddWithValue(@ Mark,_ MarksObtained);

cmd.Parameters.AddWithValue(@ Exam,_Exam);

cmd.ExecuteNonQuery();

}



我的MainWindow.xaml.cs代码



private void btnUpDate_Click(object sender,RoutedEventArgs e)

{

stud.RollNo = Convert.ToInt32(txtRollNo.Text);

stud.Firs tName = txtFirstName.Text;

stud.LastName = txtLastName.Text;

stud.CollegeName = txtClgName.Text;

stud.MarksObtained = txtMarks.Text;

stud.Exam = txtExamResults.Text;

stud.Update(); //更新记录的调用对象

MessageBox 。显示(更新记录,更新);

}



谢谢

Sophia

解决方案

嗨索菲亚,

在更新语句中,在查询中添加@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 list

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;, ?????); // Modify this line



what is stud variable doing here? and you are not using stud var value to update statement. check that too.


这篇关于在ms access 2007数据库中更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆