【发布时间】:2021-07-25 06:20:43
【问题描述】:
我在 VB.NET 上有一个表单,我用它来管理 mysql 数据库的数据。 除了用于输入数据的各种字段(文本框 + 1 个日期选择器)之外,该表单还包括一个“保存”按钮(当我按下“编辑”按钮时它变成“更新”)、一个“编辑”按钮、一个“删除”按钮和一个 DataGridView。
我创建了一个ShowData() 来在DataGridView 中显示数据。 此功能适用于表单加载,当我使用“保存”命令(使用 mysql 插入命令)和“删除”命令(使用 mysql 删除命令)时也可以使用,但是当我使用“更新”命令。
当我使用“更新”命令时,数据库中的数据会更新,但“ShowData ()”函数给我错误您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以了解在第 1 行的 '' 附近使用正确的语法,因此,DataGridView 内的数据不会更新。
“ShowData()”函数也出现在 DataGridView 的“CellMouseDown”事件中。当我首先使用“更新”命令然后在行中移动时,仅会出现相同的错误。
公开课
Dim Connection As New MySqlConnection("server=localhost; user=root; password=; database=dbname")
Dim MySQLCMD As New MySqlCommand
Dim MySQLDA As New MySqlDataAdapter
Dim DT As New DataTable
Dim Table_Name As String = "tablename"
Dim Dati As Integer
Dim LoadImagesStr As Boolean = False
Dim data1Ram, data2Ram As String
Dim StatusInput As String = "Save"
Dim SqlCmdSearchstr As String
ShowData()
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connection failed !!!" & vbCrLf & "Please check that the server is ready !!!", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Try
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "SELECT data1,data2 FROM " & Table_Name & " WHERE cid=" & cid.Text & ""
MySQLDA = New MySqlDataAdapter(MySQLCMD.CommandText, Connection)
DT = New DataTable
Dati = MySQLDA.Fill(DT)
If Dati > 0 Then
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = DT
DataGridView1.DefaultCellStyle.ForeColor = Color.Black
DataGridView1.ClearSelection()
Else
DataGridView1.DataSource = DT
End If
Catch ex As Exception
MsgBox("Connection Error !!!" & vbCr & ex.Message, MsgBoxStyle.Critical, "Error Message")
Connection.Close()
Return
End Try
DT = Nothing
Connection.Close()
保存/更新按钮点击
Dim mstream As New System.IO.MemoryStream()
If StatusInput = "Save" Then
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connection Failed" & vbCrLf & "Check internet connection !!!", "No connection", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Try
MySQLCMD = New MySqlCommand
With MySQLCMD
.CommandText = "INSERT INTO " & Table_Name & " (data1, data2) VALUES (@data1, @data2)"
.Connection = Connection
.Parameters.AddWithValue("@data1", textbox1.Text)
.Parameters.AddWithValue("@data2", textbox2.Text)
.ExecuteNonQuery()
End With
MsgBox("Data saved successfully", MsgBoxStyle.Information, "Information")
ClearInputUpdateData()
Catch ex As Exception
MsgBox("Data failed to save !!!" & vbCr & ex.Message, MsgBoxStyle.Critical, "Error Message")
Connection.Close()
Return
End Try
Connection.Close()
Else
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connection failed !!!" & vbCrLf & "Please check that the server is ready !!!", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Try
MySQLCMD = New MySqlCommand
With MySQLCMD
.CommandText = "UPDATE " & Table_Name & " SET `data1` = @data1, `data2` = @data2 WHERE id=@id"
.Connection = Connection
.Parameters.AddWithValue("@data1", textbox1.Text)
.Parameters.AddWithValue("@data2", textbox2.Text)
.Parameters.AddWithValue("@id", id.Text)
.ExecuteNonQuery()
End With
MsgBox("Data updated successfully", MsgBoxStyle.Information, "Information")
ButtonSave.Text = "Save"
DatePicker1.Enabled = True
ClearInputUpdateData()
Catch ex As Exception
MsgBox("Data failed to Update !!!" & vbCr & ex.Message, MsgBoxStyle.Critical, "Error Message")
Connection.Close()
Return
End Try
Connection.Close()
StatusInput = "Save"
End If
ShowData()
编辑按钮点击
If DataGridView1.RowCount = 1 Then
textbox1.Text = data1Ram
textbox2.Text = data2Ram
ButtonSave.Text = "Update"
StatusInput = "Update"
Return
End If
DataGridView1 CellMouseDown
Try
If AllCellsSelected(DataGridView1) = False Then
If e.Button = MouseButtons.Left Then
DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, e.RowIndex)
Dim i As Integer
With DataGridView1
If e.RowIndex >= 0 Then
i = .CurrentRow.Index
LoadImagesStr = True
data1Ram = .Rows(i).Cells("data1").Value.ToString
data2Ram = .Rows(i).Cells("data2").Value.ToString
ShowData()
End If
End With
End If
End If
Catch ex As Exception
Return
End Try
编辑 1
正如错误消息所解释的,这是一个命令语法问题。 事实上,我尝试在 ShowData() 中替换这个字符串
"SELECT data1,data2 FROM " & Table_Name & " WHERE cid=" & cid.Text & ""
有了这个
"SELECT data1,data2 FROM tablename WHERE cid = 10"
并且没有出现错误。 所以我需要弄清楚如何将表名和条件放在命令字符串中
【问题讨论】:
-
好吧,很抱歉指出显而易见的问题。更新 SQL 中有语法错误。字段名称周围的 ' 真的需要吗?另请注意,您的 WHERE id=@id,没有看到您在哪里定义参数@id,也没有在执行之前将其添加到 MySQLCMD.Parameters 集合中
-
连接和命令需要在使用它们的方法中声明和处理。请勿在此方法之外声明 then。
-
了解如何使用
Using...End Using块来确保数据库对象被关闭和释放。 -
@Mary 我尝试了“使用端使用”方法,但出现了同样的错误。检查此屏幕截图:Image
-
@Hursey “更新”语法工作正常。数据在数据库中更新。当 datagridview 尝试使用 ShowData() 更新自身并且仅当上一个命令是“UPDATE”时才会出现错误。总而言之:使用 insert 命令 + showdata () 它可以使用命令 delete + showdata () 它可以使用命令 update + showdata () 它不起作用并返回错误“您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以便在第 1 行的 '' 附近使用正确的语法
标签: mysql vb.net datagridview command connection