通过文本框更新数据网格视图
Updating datagridview via textboxes
我已经编写了允许我通过文本框更新 datagridview 中的值的代码,但是它不起作用。在运行期间,当我按下更新按钮时,没有任何反应,甚至没有弹出错误消息。
代码如下:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Connection.ConnectionString = "provider= Microsoft.ACE.OLEDB.12.0;Data Source= SRTDB.accdb"
Dim connectionString As String = "provider= Microsoft.ACE.OLEDB.12.0;Data Source= SRTDB.accdb"
Dim SQLQuery As String = "UPDATE Students SET StudentFirstName = @StudentFirstName, " & _
"StudentSurname = @StudentSurname, StudentPassword= @StudentPassword, StudentGroup = @Studentgroup " & _
"WHERE StudentID = @StudentID"
Using Connection = New System.Data.OleDb.OleDbConnection(connectionString)
Using cmd = New System.Data.OleDb.OleDbCommand(SQLQuery, Connection)
Connection.Open()
cmd.Parameters.AddWithValue("@StudentID", txtStudentID.Text)
cmd.Parameters.AddWithValue("@StudentFirstName", txtStudentFirstname.Text)
cmd.Parameters.AddWithValue("@StudentSurname", txtStudentSurname.Text)
cmd.Parameters.AddWithValue("@StudentPassword", txtStudentPassword.Text)
cmd.Parameters.AddWithValue("@StudentGroup", cbxStudentGroup.Text)
Dim rowsInserted = cmd.ExecuteNonQuery
If rowsInserted > 0 Then
MessageBox.Show("Record successfully updated!", "Updated!")
ShowItems()
Else
MessageBox.Show("Failure to update new record!", "Failure!")
End If
End Using
End Using
End Sub
OleDB
不使用命名参数作为命名参数 - 您需要按照它们在 SQL 中出现的确切顺序分配值。您的 SQL 最后指定了 StudentID,但您将其指定为第一个参数。 MSDN 建议您使用 ?
占位符而不是命名占位符。
这意味着当它执行时,它会尝试更新记录WHERE StudentID = cbxStudentGroup.Text
。由于不太可能有这样的记录,因此它不会更新任何内容并且 rowsInserted
将为 0.
您的代码中还有另外一件事,加上一些提示:
Private Sub btnUpdate_Click...
Connection.ConnectionString = "provider= Microsoft.ACE.OLEDB.12.0;..."
Dim connectionString As String = "provider= Microsoft.ACE...."
Dim SQLQuery As String = "UPDATE Students SET StudentFirstName = @StudentFirstName, " & _ ...
第一个 Connection
对象有时可能是 Nothing
。因为它会被 USING 块中的那个覆盖,所以把它去掉。您可以使用 XML 文字使您的 SQL 更易于阅读:
Dim SQLQuery As String = <sql>
UPDATE Students
SET StudentFirstName = @StudentFirstName,
StudentSurname = @StudentSurname,
StudentPassword= @StudentPassword,
StudentGroup = @Studentgroup
WHERE StudentID = @StudentID
</sql>.Value
如果将 Imports System.Data.OleDb
添加到代码文件的顶部,则可以缩短这些引用:
Using Connection = New OleDbConnection(connectionString)
Using cmd = New OleDbCommand(SQLQuery, Connection)
Connection.Open()
'...
可能的问题是参数的顺序:
cmd.Parameters.AddWithValue("@StudentFirstName", txtStudentFirstname.Text)
cmd.Parameters.AddWithValue("@StudentSurname", txtStudentSurname.Text)
cmd.Parameters.AddWithValue("@StudentPassword", txtStudentPassword.Text)
cmd.Parameters.AddWithValue("@StudentGroup", cbxStudentGroup.Text)
cmd.Parameters.AddWithValue("@StudentID", txtStudentID.Text)
强制性警告:密码永远不应存储为明文,而是散列。
另请参阅:
Can we stop using AddWithValue() already?
避免在任何地方粘贴您的连接字符串
我已经编写了允许我通过文本框更新 datagridview 中的值的代码,但是它不起作用。在运行期间,当我按下更新按钮时,没有任何反应,甚至没有弹出错误消息。
代码如下:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Connection.ConnectionString = "provider= Microsoft.ACE.OLEDB.12.0;Data Source= SRTDB.accdb"
Dim connectionString As String = "provider= Microsoft.ACE.OLEDB.12.0;Data Source= SRTDB.accdb"
Dim SQLQuery As String = "UPDATE Students SET StudentFirstName = @StudentFirstName, " & _
"StudentSurname = @StudentSurname, StudentPassword= @StudentPassword, StudentGroup = @Studentgroup " & _
"WHERE StudentID = @StudentID"
Using Connection = New System.Data.OleDb.OleDbConnection(connectionString)
Using cmd = New System.Data.OleDb.OleDbCommand(SQLQuery, Connection)
Connection.Open()
cmd.Parameters.AddWithValue("@StudentID", txtStudentID.Text)
cmd.Parameters.AddWithValue("@StudentFirstName", txtStudentFirstname.Text)
cmd.Parameters.AddWithValue("@StudentSurname", txtStudentSurname.Text)
cmd.Parameters.AddWithValue("@StudentPassword", txtStudentPassword.Text)
cmd.Parameters.AddWithValue("@StudentGroup", cbxStudentGroup.Text)
Dim rowsInserted = cmd.ExecuteNonQuery
If rowsInserted > 0 Then
MessageBox.Show("Record successfully updated!", "Updated!")
ShowItems()
Else
MessageBox.Show("Failure to update new record!", "Failure!")
End If
End Using
End Using
End Sub
OleDB
不使用命名参数作为命名参数 - 您需要按照它们在 SQL 中出现的确切顺序分配值。您的 SQL 最后指定了 StudentID,但您将其指定为第一个参数。 MSDN 建议您使用 ?
占位符而不是命名占位符。
这意味着当它执行时,它会尝试更新记录WHERE StudentID = cbxStudentGroup.Text
。由于不太可能有这样的记录,因此它不会更新任何内容并且 rowsInserted
将为 0.
您的代码中还有另外一件事,加上一些提示:
Private Sub btnUpdate_Click...
Connection.ConnectionString = "provider= Microsoft.ACE.OLEDB.12.0;..."
Dim connectionString As String = "provider= Microsoft.ACE...."
Dim SQLQuery As String = "UPDATE Students SET StudentFirstName = @StudentFirstName, " & _ ...
第一个 Connection
对象有时可能是 Nothing
。因为它会被 USING 块中的那个覆盖,所以把它去掉。您可以使用 XML 文字使您的 SQL 更易于阅读:
Dim SQLQuery As String = <sql>
UPDATE Students
SET StudentFirstName = @StudentFirstName,
StudentSurname = @StudentSurname,
StudentPassword= @StudentPassword,
StudentGroup = @Studentgroup
WHERE StudentID = @StudentID
</sql>.Value
如果将 Imports System.Data.OleDb
添加到代码文件的顶部,则可以缩短这些引用:
Using Connection = New OleDbConnection(connectionString)
Using cmd = New OleDbCommand(SQLQuery, Connection)
Connection.Open()
'...
可能的问题是参数的顺序:
cmd.Parameters.AddWithValue("@StudentFirstName", txtStudentFirstname.Text)
cmd.Parameters.AddWithValue("@StudentSurname", txtStudentSurname.Text)
cmd.Parameters.AddWithValue("@StudentPassword", txtStudentPassword.Text)
cmd.Parameters.AddWithValue("@StudentGroup", cbxStudentGroup.Text)
cmd.Parameters.AddWithValue("@StudentID", txtStudentID.Text)
强制性警告:密码永远不应存储为明文,而是散列。
另请参阅:
Can we stop using AddWithValue() already?