VB.Net 和 MySQL
我使用 VB.Net 和 MySQL 作为数据库,我是新手。我在 MySQL 中使用外键时遇到问题。在 MySQL 中,我创建了 inq
表作为其主表和 inqcontact
表。这是我的 MySQL 代码:
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));
这是我的 VB.Net 代码:
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!", "")
我的 VB.Net 代码将 NULL 值返回到 inqcontact
表中的 number
外键。我的意思是,在 inq
表中,number
字段会自动递增,因此没有问题。但在inqcontact
表中,number
字段(即外键)为NULL 值。您能告诉我我提供的代码有什么问题吗?我认为错误在于从我的 VB.Net 插入数据。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,
Try this,
我通常使用 SQL Server 而不是我的 SQL,但我相信您应该能够基本上执行以下操作:
然后使用 id 作为插入项的键的值继续执行第二个查询。我从上面的答案中复制了代码,因为它更合适(防止注入等的安全性)和一种习惯,但是 ;接下来的第二个查询获取刚刚插入的 id 也应该适用于您的查询。
I generally use SQL server rather than my SQL, but I believe that you should be able to do basically the following:
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.