VB.Net 和 MySQL

发布于 2024-12-10 18:46:43 字数 1433 浏览 6 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛歌少女 2024-12-17 18:46:43

试试这个,

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()

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()
诗笺 2024-12-17 18:46:43

我通常使用 SQL Server 而不是我的 SQL,但我相信您应该能够基本上执行以下操作:

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())

然后使用 id 作为插入项的键的值继续执行第二个查询。我从上面的答案中复制了代码,因为它更合适(防止注入等的安全性)和一种习惯,但是 ;接下来的第二个查询获取刚刚插入的 id 也应该适用于您的查询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文