簡體   English   中英

將數據從一個表插入到另一個表| MySQL的

[英]Inserting data from one table to another | mysql

我目前正在學習vb.net,正在從事一個小測試項目。 問題是,我有五個表:

登錄,我保留學生的登錄信息:

IDUser | Name | Surname | Username   | Password
---------------------------------------------
1      | John | Smith   | john.smith | john123

學生,我保存有關學生的基本信息

IDStudent | Name | Surname
--------------------------
1         | John | Smith 

教授,我保存有關教授的基本信息

IDProfessor | Name | Surname 
----------------------------
1           | Mike | Petersen

主題,我保留有關主題的基本信息

IDSubject | SubjectName | ProfessorName
---------------------------------------
1         | Programming | Gary Williams

對於最終表,我需要從上面的表中插入數據,我有課程:

IDCourse | StudentID | SubjectID | ProfessorID
----------------------------------------------
1        | ??        | ???       | ????

問題是,如何從上表中的課程表中插入數據。 我當前正在使用MySql作為數據庫,並且已經創建了外鍵並將其與主鍵連接。 我還有一個組合框(填充有“主題”表中的主題),學生可以在其中選擇一個主題,然后按一下按鈕,將其插入“課程”表中。

對每個表使用DataTable並將表數據彼此插入,例如:

 Dim con as new MySqlConnection="connectionsstringhere"
 con.open
 Dim cmd as new Mysqlcommand = "Select * from Login",con)

 dim table as new datatable
 Dim adapter as new mysqldataadapter(cmd)
 adapter.filll(table)
 Dim cmd2 as new mysqlcommand("Insert into Courses([student id],[subject id],[Professor id])values(@sid,@sbid,@pid)",con)
 cmd2.parametres.add("@sid",mysqldbtype.nvarchar).Value = table.rows(0)(1)  'here (0) is the column no. and (1) is the cell no.

   cmd2.parametres.add("@sbid",mysqldbtype. nvarchar).Value = table.rows(0)(2)  
 cmd2.executenonquery
 con.close

以下代碼將在“ Courses表中插入“ Login表的前2個單元格的數據。

請注意,這只是一個基本示例,您需要根據需要修改代碼

更新我昨天必須添加了此內容,但我有點忙...不過,讓我們開始吧...。

對於每一個表中的每一列,你可以創建一個list(of string) 。例如,創建一個list(of string)命名的子/可變StudentnameandID 。現在,使用一個datatable前兩個表(學生和教授)。將單元格1的值從數據表傳遞到List(of string)子對象STUDENTNAMEANDID確保在每個值之后添加一個特殊字符,例如逗號(。)...然后,您最終可以將每個值與Studentnameandid分開, Studentnameandid添加逗號...然后for each Studentnameandid for each值,將數據添加到最后一個表中.....

如果您想要一個示例代碼,請留下評論,我將其添加

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM