我有一些学生的关系型ms-access数据库。 (数据库已经填充了一些数据) 我的数据库中有两张表: 1. tblStuInfo为学生的一般信息。 2. tblStuEducation管理其年级和兴趣每年。c# - 独立更新具有相同ID的ms-access数据库记录
命名列 “StuID” 是tblStuInfo &在tblStuEducation外键 主键。
My database structure looks like this & example data looks like this
在我的窗体加载事件,动态文本框会自动在tblStuEducation表中每个记录创建的,他们没有问题,适当地填补。
我的问题是,当我编辑的一些信息(这里假设只是EduYear)通过WinForm的,然后点击保存,所有记录都将得到相同的值(最后一个文本框)
这里是我的保存按钮代码:
connection.Open();
OleDbCommand updateStuEducation = new OleDbCommand();
updateStuEducation.Connection = connection;
readStuEducation.CommandText = "select * from tblStuEducation where stuID = 1000";
//textBoxNamesArray use LINQ to save all values from textboxes with txtEduYear in their name
string[] textBoxNamesArray = tabpEdu.Controls.OfType<TextBox>().Where(a => a.Name.Contains("txtEduYear")).Select(a => a.Text).ToArray();
Int32 i = 0;
foreach (string value in textBoxNamesArray)
{
readStuEducation.CommandText = "update tblStuEducation set EduYear = '" + value[i] + "'";
readStuEducation.ExecuteNonQuery();
i++;
}
connection.Close();
我需要如下:
if i = 0 => value[0] Save in Row[0] of tblStuEducation where stuID = 1000.
if i = 1 => value[1] Save in Row[1] of tblStuEducation where stuID = 1000.
if i = 2 => value[2] Save in Row[2] of tblStuEducation where stuID = 1000.
等等...
谢谢你史蒂夫。但它仍然更新所有EduYear列的记录,其中stuID = 1000并带有最新的文本框值。我需要更新每一行的独立年份值。 –
然后,您可能需要WHERE子句中的另一个条件 - 我已经添加了一个作为示例;这将为某一年的特定学生设定分数。 –
这正是我的问题。除了stuID,所有其他列可以有不同的值,因此条件可能会有所不同。我正在寻找解决方案来浏览更新查询中的每一行。就像我们在使用DataReader while循环时一样 –