将数据从对象和变量插入到MySql数据库中 [英] Insert data to a from objects and variablesinto a MySql adatabse
问题描述
我正在尝试从具有各种字段的from中将数据插入到我的数据库中,
Hi I am trying to insert data into my databse from a from with various fields,
这是我的代码
将性别作为字符串暗淡
MysqlConn =新的MySqlConnection
MysqlConn.ConnectionString ="server = localhost; user = root; database = medicalreembureseme"
昏暗的阅读器为MySqlDataReader
试试
MysqlConn.Open()
性别= CmbGender.SelectedItem
昏暗查询为字符串
查询=将内容插入medicalreembureseme.staff(姓氏,中间名,名字,电子邮件,DateOfEmp,DateOfReport,JobGroup,部门,分配,StaffId,角色,性别,IdNo,Dob)VALUES('''& TxtLname.Text
& ,","& TxtMname.Text& ,","& TxtFname.Text& ,","&文本角色,","& DtpDemp.Text& ,","& DtpDrep.Text& ,","& TxtJobGroup.Text&
,","& TxtDept.Text& ,","&文本分配,","& TxtStaffId.Text& ,","&文本角色,","& TxtIdno.Text& ,","&性别与,",
& DtpDob.Text& "')"
命令=新的MySqlCommand(Query,MysqlConn)
reader = command.ExecuteReader
结果= MessageBox.Show(工作人员已成功添加.添加另一个?",成功注册",MessageBoxButtons.OKCancel)
如果result = DialogResult.OK然后
Me.Show()
TxtFname.Focus()
其他
Admin.Show()
Me.Close()
如果结束
MysqlConn.Close()
捕获为MySqlException
MessageBox.Show(ex.Message)
终于
MysqlConn.Dispose()
结束尝试
Dim Gender As String
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;user=root;database=medicalreembureseme"
Dim reader As MySqlDataReader
Try
MysqlConn.Open()
Gender = CmbGender.SelectedItem
Dim Query As String
Query = "INSERT INTO medicalreembureseme.staff (LastName,MiddleName,FirstName,Email,DateOfEmp,DateOfReport,JobGroup,Department,Allocation,StaffId,Role,Gender,IdNo,Dob) VALUES('" & TxtLname.Text
& "','" & TxtMname.Text & "','" & TxtFname.Text & "','" & TxtRole.Text & "','" & DtpDemp.Text & "','" & DtpDrep.Text & "','" & TxtJobGroup.Text &
"','" & TxtDept.Text & "','" & TxtAllocation.Text & "','" & TxtStaffId.Text & "','" & TxtRole.Text & "','" & TxtIdno.Text & "','" & Gender & "','"
& DtpDob.Text & "')"
command = New MySqlCommand(Query, MysqlConn)
reader = command.ExecuteReader
result = MessageBox.Show("Staff added Succesfully. Add another?", "Successfull registration", MessageBoxButtons.OKCancel)
If result = DialogResult.OK Then
Me.Show()
TxtFname.Focus()
Else
Admin.Show()
Me.Close()
End If
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
我的问题是,如果我运行该应用程序并尝试保存,则会收到以下错误性别行1的整数无效"
My problem is that if I ran the application and try to save , I get the following error ' Invalid integer for Gender row1'
这可能是什么原因,我该怎么办?
What is could be the cause for this and what should I do ?
谢谢
推荐答案
你好,
要更改的第一件事是您的INSERT语句,不要使用字符串连接,而要使用 参数. Parematers有一个AddWithValue方法,该方法将获取您提供的数据并将其键入正确的类型,其中您现在拥有的所有东西都是字符串.
The first thing to change is your INSERT statement, to not use string concatenation but instead use parameters. Parematers has a method AddWithValue which will take the data provided by you and type it to the proper type where what you have now every thing is a string.
下面显示的是C#的示例,但这没有区别,因为它也适用于VB.NET.
The following shows examples with C# but that makes no difference as this works with VB.NET too.
https://dev.mysql.com/doc/connector -net/en/connector-net-tutorials-parameters.html
这篇关于将数据从对象和变量插入到MySql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!