【发布时间】:2015-12-02 16:47:21
【问题描述】:
我有一些学生的关系 ms-access 数据库。 (数据库已经填充了一些数据) 我的数据库中有两个表: 1. tblStuInfo 用于学生的一般信息。 2. tblStuEducation 用于管理他们每年的成绩和兴趣。
名为“StuID”的列是 tblStuInfo 中的主键和外键 在 tblStuEducation 中。
My database structure looks like this & example data looks like this
在我的表单加载事件中,为 tblStuEducation 表中的每条记录自动创建动态文本框,并且它们被适当地填充没有问题。
我的问题是当我通过 winform 编辑一些信息(这里假设只是 EduYear)并单击保存时,所有记录将获得相同的值(最后一个文本框)
这是我的保存按钮代码:
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.
等等……
【问题讨论】:
标签: c# database winforms datatable