gpt4 book ai didi

mysql - 如何在 MySQL 表中的多个文本框的不同列中插入记录?

转载 作者:行者123 更新时间:2023-11-30 23:18:32 25 4
gpt4 key购买 nike

如何将 VB.net WinForm 中多个文本框中的记录添加到远程 MySQL 数据库?我想将文本框 (fname, lname) 中的值插入到 mySQL 表中的相应列中。一次只能看到一个文本框。下一个文本框仅在前一个文本框已提交后才可见。我正在使用以下代码输入单个记录,但无法将其用于多列。

Imports MySql.Data.MySqlClient


Public Class Regform
Dim serverstring As String = "server=***;Uid=****;database=****;password=****"
Dim SQLstate As String
Dim firstn As String
Dim SQLconnection As MySqlConnection = New MySqlConnection


Private Sub Regform_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SQLconnection.ConnectionString = serverstring

Try
If SQLconnection.State = ConnectionState.Closed Then
SQLconnection.Open()
MsgBox("Connection Established")
Else
SQLconnection.Close()
MsgBox("Connection is Closed")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
text2.Text = "Well, Hello " & firstn & "Please give me your last name"
End Sub

Public Sub savename(ByRef SQLstatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLstatement
.CommandType = CommandType.Text
.Connection = SQLconnection
.ExecuteNonQuery()

End With

SQLconnection.Close()
MsgBox("Added")
SQLconnection.Dispose()

End Sub

Private Sub cmdsubmit_Click(sender As Object, e As EventArgs) Handles cmdsubmit.Click
SQLstate = "INSERT INTO users(fname) VALUES('" & fname.Text & "')"

savename(SQLstate)
text1.Visible = False
text2.Visible = True
fname.Visible = False
' lname.visible = True

End Sub

Private Sub fname_TextChanged(sender As Object, e As EventArgs) Handles fname.TextChanged
firstn = fname.Text
End Sub
End Class

最佳答案

您能否将名字存储在本地变量中,直到您拥有姓氏? (您似乎已经有了 Dim firstn As String)

firstn = fname.Text

然后使用类似的东西:

"INSERT INTO users(fname,lname) VALUES('" & firstn & "','" & lname.Text & "')"

如果不使用这种方法,或者您出于这个或其他原因想要选择回插入行的 ID 以便您可以更新它们,那么请使用如下技术:

Return Last ID (IDENTITY) On Insert row VB.NET MySQL

顺便说一句,我不会仅根据名字选择退出数据库。它们不是很独特;)

关于mysql - 如何在 MySQL 表中的多个文本框的不同列中插入记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com