我需要知道使用vb.net更新数据库中的多行。 在我的数据库中,它有3行(彼此有id),在我的vb.net上我有3个文本框。我需要一次性使用这3个文本框更新数据。
这是我的vb.net代码
MysqlConn.Open()
Dim Query As String
Query = "update cs set subject=@subject, place=@place where=@id"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data Saved")
MysqlConn.Close()
答案 0 :(得分:0)
您需要传递参数
Dim sql as String = "update cs set subject=@subject, place=@place where id=@id" ' NOTE: id = @id
cmd = New MySqlCommand(sql, MysqlConn)
cmd.Parameters.addWithValue("@subject", txtSubject.Text)
cmd.Parameters.addWithValue("@place", txtPlace.Text)
cmd.Parameters.addWithValue("@id", CInt(txtId.Text)) ' id is int , I hope
' instead of reader do ExecuteNonQuery
Dim ret = cmd.ExecuteNonQuery()
MessageBox.Show("Success: " & (ret > 0).ToString())
您也可以执行ExecuteReader
,但是当您返回某些内容时也会这样做。我不知道MySql
,但在Sql Server
,您可以将Output
与Insert
和Update
一起使用。您可以使用ExecuteReader
来获取此输出。