如何将 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