通过文本框更新datagridview

时间:2015-11-12 14:11:48

标签: database vb.net datagridview updating

我编写的代码允许我通过文本框更新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

1 个答案:

答案 0 :(得分:1)

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?
GetConnection Method以避免必须将连接字符串粘贴到任何地方