I'm currently learning vb.net and I'm working on a little test project. Thing is, I have five tables:
Login, where I keep login information for students:
IDUser | Name | Surname | Username | Password
---------------------------------------------
1 | John | Smith | john.smith | john123
Students, where I keep basic informations about students
IDStudent | Name | Surname
--------------------------
1 | John | Smith
Professors, where I keep basic informations about professors
IDProfessor | Name | Surname
----------------------------
1 | Mike | Petersen
Subjects, where I keep basic information about subjects
IDSubject | SubjectName | ProfessorName
---------------------------------------
1 | Programming | Gary Williams
and for a final table, in which I need to insert data from tables above, I have Courses:
IDCourse | StudentID | SubjectID | ProfessorID
----------------------------------------------
1 | ?? | ??? | ????
The question is, how do I insert data in Course table from tables above. I'm currently using MySql as my database, and I've created foreign keys and connected them with the primary keys. I also have a ComboBox (filled with subjects from Subjects table) where a student can choose a Subject and by pressing the button, insert it in a Courses table.
Use DataTable
s for each table and insert table data from one to another.For example :
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
The following code will insert Login
table's first 2 cells' data in Courses
table.
Note that this is just a basic example.You need to modify the code based on your requirements
UPDATE I must've added this yesterday but i was a bit busy...However let's begin....
For each column in each tables,you can create a list(of string)
.For example,create a list(of string)
sub/variable named StudentnameandID
.. Now, use a datatable
for the first two tables(Student and Professor).Pass the values of cell 1 from the datatable to the List(of string) sub STUDENTNAMEANDID
Make sure you add a special character like a comma(,) after every value...Then finally you can separate each value from Studentnameandid
aas you've added commas... Then for each
value in Studentnameandid
, you add data to your last table.....
If you want a sample code for this,leave a comment and i'll add it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.