作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将 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/
我是一名优秀的程序员,十分优秀!