我的更新文本框名为:updateref 我想用所选列更新的数据字段是refid
我很抱歉,我是一个菜鸟,并且全新的将mysql与vb.net集成,任何帮助都将不胜感激!谢谢!
'MYSQL代码
Dim con As MySqlConnection = New MySqlConnection("my info")
Dim reader As MySqlDataReader
Try
con.Open()
Dim query As String
Dim command As MySqlCommand
query = "UPDATE exploitsociety SET reffer='" + updateref.Text + "' WHERE reffer='" + DataGridView1.CurrentCell.Selected + "';"
command = New MySqlCommand(query, con)
reader = command.ExecuteReader
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End try
con.Close()
基本上我只想编辑dataviewgrid中的列数据,然后点击更新,但它很复杂,所以我添加了一个文本框,看看我是否可以这样做,我也不能。
答案 0 :(得分:0)
您似乎尝试更新查询
Dim con As MySqlConnection = New MySqlConnection("my info")
Dim reader As MySqlDataReader
Try
con.Open()
Dim query As String
Dim command As MySqlCommand
query = "UPDATE exploitsociety SET reffer='" + updateref.Text + "' WHERE reffer='" + DataGridView1.CurrentCell.Selected + "';"
command = New MySqlCommand(query, con)
// reader = command.ExecuteReader
// you need to run ExecuteNonQuery instead of ExecuteReader
int UpdatedRows= command.ExecuteNonQuery();
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End try
con.Close()
仅供参考
<强>的ExecuteReader 强>
使用命令对象执行SQL查询或存储过程时,将使用Execute Reader返回行集。这个是仅向前检索记录,它用于从头到尾读取表值。(阅读更多关于ExecuteReader)
<强>的ExecuteNonQuery 强>
ExecuteNonQuery方法将返回使用INSERT,DELETE或UPDATE操作影响的行数。此ExecuteNonQuery方法仅用于insert,update和delete,Create和SET语句。 (Read More about ExecuteNonQuery)
答案 1 :(得分:0)
试试这个并查看它抛出的异常(如果有的话):
Using cn = New MySQLConnection("my info")
cn.Open()
Using cmd = New MySQLCommand("UPDATE exploitsociety SET reffer='" + updateref.Text + "' WHERE reffer='" + DataGridView1.CurrentCell.Selected + "'", cn)
cmd.ExecuteNonQuery()
End Using
End Using
答案 2 :(得分:0)
然后尝试以下查询进行更新,删除单引号。
query = "UPDATE exploitsociety SET refid=" + updateref.Text + " WHERE refid=" + DataGridView1.CurrentCell.Selected
command = New MySqlCommand(query, con)
command.ExecuteNonQuery()
您也可以获得所选单元格值,如下所示
DataGridView1.SelectedCells[<index of column>].Value.ToString();