I am using VB.Net and MySQL as it's database, I'm a newbie. I have a problem using Foreign key in MySQL. In MySQL, I've created inq
Table as its primary table and inqcontact
Table. Here's my MySQL code:
CREATE TABLE inq(
number INT NOT NULL AUTO_INCREMENT,
lastname VARCHAR(20),
firstname VARCHAR(20),
middlename VARCHAR(20),
PRIMARY KEY(number));
CREATE TABLE inqcontact(
noinqcontact INT NOT NULL AUTO_INCREMENT,
mobile VARCHAR(20),
telephone VARCHAR(20),
emailadd VARCHAR(20),
number INT,
PRIMARY KEY(noinqcontact),
FOREIGN KEY(number) REFERENCES inq(number));
and here's my VB.Net code:
CommInq1 = New MySqlCommand("INSERT INTO inq VALUES (number,'" & txtLastName.Text & "','" & txtFirstName.Text & "','" & txtMiddleName.Text & "')", ConnInq)
ConnInq.Open()
CommInq1.ExecuteNonQuery()
CommInq2 = New MySqlCommand("INSERT INTO inqcontact VALUES (noinqcontact,'" & txtMobileNo.Text & "','" & txtTelephoneNo.Text & "','" & txtEmailAdd.Text & "',number )", ConnInq)
CommInq2.ExecuteNonQuery()
ConnInq.Close()
MessageBox.Show("Saved!", "")
My VB.Net code returns NULL value to the number
Foreign Key in inqcontact
Table. I mean, in inq
Table, the number
field automatically increments itself so there's no problem with it. But in inqcontact
Table, the number
Field, which is the Foreign Key, is NULL value. Could you tell me what's wrong with the code I've provided? I think, the error is in the insertion of data from my VB.Net.
Try this,
CommInq1 = New MySqlCommand("INSERT INTO inq (lastname,firstname,middlename)
VALUES (@lastname,@firstname,@middlename)",ConnInq)
CommInq1.Parameters.Add("@lastname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtLastName.Text
CommInq1.Parameters.Add("@firstname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtFirstName.Text
CommInq1.Parameters.Add("@middlename",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtMiddleName.Text
ConnInq.Open()
CommInq1.ExecuteNonQuery()
CommInq2 = New MySqlCommand("INSERT INTO inqcontact (mobile,telephone,emailadd,number)
VALUES (@mobile,@telephone,@emailadd,@number)",ConnInq)
CommInq2.Parameters.Add("@mobile",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtMobileNo.Text
CommInq2.Parameters.Add("@telephone",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtTelephoneNo.Text
CommInq2.Parameters.Add("@emailadde",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtEmailAdd.Text
CommInq2.Parameters.Add("@number",MySql.Data.MySqlClient.MySqlDbType.Int32)
.Value=CommInq1.LastInsertedId
' or use select last_insert_id() to get the last id
CommInq2.ExecuteNonQuery()
ConnInq.Close()
I generally use SQL server rather than my SQL, but I believe that you should be able to do basically the following:
CommInq1 = New MySqlCommand("INSERT INTO inq (lastname,firstname,middlename)
VALUES (@lastname,@firstname,@middlename); select last_insert_id()" ,ConnInq)
CommInq1.Parameters.Add("@lastname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtLastName.Text
CommInq1.Parameters.Add("@firstname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtFirstName.Text
CommInq1.Parameters.Add("@middlename",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtMiddleName.Text
ConnInq.Open()
Dim id as Integer = cint(CommInq1.ExecuteScalar())
And then proceed with the second query using id as the value for the inserted item's key. I copied the code from the answer above because its more proper (security against injection and all) and a habit, but the ; followed by the second query to get the id just inserted should work with your query as well.
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.