之前我问过一个关于使用phpMyAdmin更新datagridview中的数据的问题。您可以通过以下链接 - > Updating data in phpmyadmin part 1
来引用它代码工作正常,但现在的问题是,当我在localhost中检查更新的数据时,我看到的都是空字段。下面是我的vb设计截图。我根据我的数据库标记了文本框。屏幕截图中的文本框设置为在运行我的winform时不可见。
究竟可能是什么问题?
@Kakarot 这是我最初的作品
MysqlConn = New MySqlConnection MysqlConn.ConnectionString =“server = localhost; userid = server; password = server; database = heavisa_database” Dim rabit As MySqlDataReader
MysqlConn.Open()
Dim pin As String
pin = "UPDATE heavisa_database.new_employee SET (Employee_ID = '" & txtemployeeid.Text & "', Nat_ID = '" & txtnatid.Text & "', First_Name = '" & txtfirstname.Text & "', Middle_Name = '" & txtmiddlename.Text & "', Surname = '" & txtsurname.Text & "', NSSF_No = '" & txtnssfno.Text & "', KRA_Pin = '" & txtkrapin.Text & "', NHIF_No = '" & txtnhifno.Text & "', Residence = '" & txtresidence.Text & "', Mobile_No = '" & txtmobileno.Text & "', Email = '" & txtemail.Text & "', Job_Group = '" & cbojobgroup.Text & "', Employment_Date = '" & dtpemploymentdate.Text & "') WHERE Employee_ID = '" & txtemployeeid1.Text & "'"
Try
con = New MySqlCommand(pin, MysqlConn)
rabit = con.ExecuteReader
MessageBox.Show("Update Successful.")
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
这就是我现在拥有的东西(信用证给了ekad先生一个人)
Dim pin As String pin =“UPDATE heavisa_database.new_employee SET Employee_ID = @Employee_ID,Nat_ID = @Nat_ID,First_Name = @First_Name,Middle_Name = @ Middle_Name,Surname = @Surname,NSSF_No = @NSSF_No,KRA_Pin = @KRA_Pin,NHIF_No = @NHIF_No,Residence = @Residence,Mobile_No = @Mobile_No,Email = @Email,Job_Group = @Job_Group,Employment_Date = @Employment_Date WHERE Employee_ID如'%{0}%'“
Try
Using MysqlConn As New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=server;password=server;database=heavisa_database"
MysqlConn.Open()
Using con As New MySqlCommand(pin, MysqlConn)
With con
con.Parameters.AddWithValue("@Employee_ID", txtemployeeid.Text)
con.Parameters.AddWithValue("@Nat_ID", txtnatid.Text)
con.Parameters.AddWithValue("@First_Name", txtfirstname.Text)
con.Parameters.AddWithValue("@Middle_Name", txtmiddlename.Text)
con.Parameters.AddWithValue("@Surname", txtsurname.Text)
con.Parameters.AddWithValue("@NSSF_No", txtnssfno.Text)
con.Parameters.AddWithValue("@KRA_Pin", txtkrapin.Text)
con.Parameters.AddWithValue("@NHIF_No", txtnhifno.Text)
con.Parameters.AddWithValue("@Residence", txtresidence.Text)
con.Parameters.AddWithValue("@Mobile_No", txtmobileno.Text)
con.Parameters.AddWithValue("@Email", txtemail.Text)
con.Parameters.AddWithValue("@Job_Group", cbojobgroup.Text)
con.Parameters.AddWithValue("@Employment_Date", dtpemploymentdate.Text)
End With
con.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Update Successful.")
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
第一个代码给了我一个错误(请参阅上面的链接)。第二个代码可以工作,但它正在清空我的字段而不是更新。
答案 0 :(得分:0)
您是如何更新数据库中的表格的?应该是这样的: (我要把我的代码用于我做过的旧项目)
Private Sub disconnect()
conn.Close()
End Sub
Private Sub connect()
conn.ConnectionString = connectionString
conn.Open()
End Sub
Private Sub btnSaveEdit_Click(sender As Object, e As EventArgs) Handles btnSaveEdit.Click
connect()
Dim query as string
Dim command As New MySqlCommand
query = "Update `tblcandidates` set firstname = '" & txtEditFname.Text & "', lastname = '" & txtEditLName.Text & "', position='" & cboEditPosition.Text & "', fullname = '" & fullname & "' where recNum = '" & txtRec.Text & "'"
command.Connection = conn
command.CommandText = query
command.ExecuteNonQuery()
disconnect()
End Sub
答案 1 :(得分:0)
首先,只是一个简单的问题,heavisa_database.new_employee
是一张桌子吗?正确命名它们是非常明智的,如果它是一个表,你可以将tbl
放在你想要的名字之前,如果它是一个数据库,你可以在db
之前放置它。只是为了防止混淆。我认为在更新表格中的记录时你不需要reader
,如果我错了,请纠正我:p
好吧,说实话,我并不太了解ekad先生提供的代码。这是您可以尝试的代码。哦,你不需要SET
之后的支架。
//Global Vars
Dim connectionString As String = "server=localhost;userid=server;password=server;database=heavisa_database"
Dim conn As New MySqlConnection
首先,让我们轻松制作一些功能(您可以将其放在代码中的任何位置):
//remember that conn is our MySqlConnection
Private Sub connect()
conn.ConnectionString = connectionString
conn.Open()
End Sub
Private Sub disconnect()
conn.Close()
End Sub
因此,更新按钮的事件:
connect()
Dim pin As String
pin = "UPDATE new_employee SET Employee_ID = '" & txtemployeeid.Text & "', Nat_ID = '" & txtnatid.Text & "', First_Name = '" & txtfirstname.Text & "', Middle_Name = '" & txtmiddlename.Text & "', Surname = '" & txtsurname.Text & "', NSSF_No = '" & txtnssfno.Text & "', KRA_Pin = '" & txtkrapin.Text & "', NHIF_No = '" & txtnhifno.Text & "', Residence = '" & txtresidence.Text & "', Mobile_No = '" & txtmobileno.Text & "', Email = '" & txtemail.Text & "', Job_Group = '" & cbojobgroup.Text & "', Employment_Date = '" & dtpemploymentdate.Text & "' WHERE Employee_ID = '" & txtemployeeid1.Text & "'"
//lets create new command
Dim command As New MySqlCommand
//sets the connection for our command
command.Connection = conn
command.CommandText = pin
command.ExecuteNonQuery()
disconnect()
MessageBox.Show("Record saved!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
尝试上面给出的代码,我很确定它会起作用。古德勒克!