![](/img/trans.png)
[英]How to bind DataGridView with two tables, and perform CRUD operation in WPF
[英]How to perform UPDATE operation on two tables having two similar fields and one different field?
我有兩個名為“ studentBio ”和“ subjects ”的表。 這兩個表的字段在下面給出(帶有一些值):在表單上,我得到了這樣的內容: AND在清單框1中,我以A + B + C的形式顯示相應的主題。 從用戶檢索到的內容將在“ +”的基礎上進行拆分,並立即添加到主題表中。
StudentBio表的字段如下所示: WHERE (RollNo,RegYear,程序和教員組合以構成復合主鍵):
RollNo RegYear stuName program faculty phoneNuber Address 1 2010 John Intermediate Pre-Engineering 343483834 London 2 2011 Leonard Intermediate Pre-Medical 454545445 NewYork 3 2012 Henry Graduation BA 565656565 Oslo
主題表的相似字段如下: WHERE (RollNo,RegYear,程序和教職員工組合以構成復合主鍵):
RollNo RegYear program faculty subjectName 1 2010 Intermediate Pre-Engineering A 1 2010 Intermediate Pre-Engineering B 1 2010 Intermediate Pre-Engineering C 2 2011 Intermediate Pre-Medical D 2 2011 Intermediate Pre-Medical E 2 2011 Intermediate Pre-Medical F
等等。 現在讓我們來解決問題 。 我正在做的是在表中同時更新 UPDTING ( 教師和程序 ),在主題表中同時更新 subjectN 。 到目前為止,我所做的是僅更新了StudentBio表,這很容易,但是我無法弄清楚,因為我該如何構造我的更新查詢來更新主題表? 有人可以幫我構造查詢嗎?
如果我理解得很好,請嘗試使用以下重組后的查詢版本:
string update_Personal = "UPDATE studentBio INNER JOIN subjects ON studentBio.RollNo = subjects.RollNo AND studentBio.RegYear = subjects.RegYear AND studentBio.program = subjects.program AND studentBio.faculty = subjects.faculty SET studentBio.program = ProgramU, studentBio.faculty = FacultyU, subjects.subjectName = SubjectNameU WHERE studentBio.RollNo = " + Convert.ToInt32(this.rollNumber7_combo.SelectedItem.ToString()) + " AND studentBio.RegYear = " + Convert.ToInt32(this.comboBox3.SelectedItem.ToString()) + " AND studentBio.program = '" + this.comboBox1.SelectedItem.ToString() + "' AND studentBio.faculty = '" + this.comboBox2.SelectedItem.ToString() + "'";
我添加了以下部分以使其更新subjects
表:
studentBio INNER JOIN subjects ON studentBio.RollNo = subjects.RollNo AND studentBio.RegYear = subjects.RegYear AND studentBio.program = subjects.program AND studentBio.faculty = subjects.faculty
這部分加入了subjects
表使得可以更新
, subjects.subjectName = SubjectNameU
subject.subjectName , subjects.subjectName = SubjectNameU
- SET
塊中的片段更新subjects表中的subjectName列
WHERE studentBio.RollNo = " + Convert.ToInt32(this.rollNumber7_combo.SelectedItem.ToString()) + " AND studentBio.RegYear = " + Convert.ToInt32(this.comboBox3.SelectedItem.ToString()) + " AND studentBio.program = '" + this.comboBox1.SelectedItem.ToString() + "' AND studentBio.faculty = '" + this.comboBox2.SelectedItem.ToString() + "'"
-最后一部分是WHERE
塊,前綴為studentBio.
已添加到每個鍵列以精確地將過濾應用於哪個表(因為兩個表具有相同的鍵列名稱)
我希望它可以在某種程度上幫助您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.