VB.NET更新方法

问题描述:

大家好日子。我想请求帮助我的代码更新数据库中的特定记录。我的后端是Microsoft Access,前端是Visual Basic。它给了我一个错误“没有给出一个或多个必需参数的值”。另外,我收到一个关于“未将对象引用设置为对象实例”的问题。VB.NET更新方法

这是我的代码。谢谢:)

Private Sub UpdateClient() 

    Dim sqlUpdateClient As String = "UPDATE tblClient SET clientCompany = @clientCompany, clientStreetAddress = @clientStreetAddress, clientCity = @clientCity, clientContactPerson = @clientContactPerson, clientContactNumber = @clientContactNumber, clientEmail = @clientEmail, clientMobileNumber = @clientMobileNumber WHERE clientID = " + selectedClient 
    Dim recordsAffected As Integer = 0 
    Dim accessCommand As New OleDbCommand(sqlUpdateClient, accessConnection) 
    accessCommand.CommandText = sqlUpdateClient 

    accessCommand.Parameters.AddWithValue("@clientCompany", txtClientCompany.Text) 
    accessCommand.Parameters.AddWithValue("@clientStreetAddress", txtClientStreetAddress.Text) 
    accessCommand.Parameters.AddWithValue("@clientCity", txtClientCity.Text) 
    accessCommand.Parameters.AddWithValue("@clientContactPerson", txtClientContactPerson.Text) 
    accessCommand.Parameters.AddWithValue("@clientContactNumber", txtClientPhoneNumber.Text) 
    accessCommand.Parameters.AddWithValue("@clientEmail", txtClientEmailAddress.Text) 
    accessCommand.Parameters.AddWithValue("@clientMobileNumber", txtClientMobileNumber.Text) 

    Try 

     accessConnection.Open() 
     recordsAffected = accessCommand.ExecuteNonQuery 

    Catch ex As Exception 

     lblError.Text = ex.ToString 

    Finally 

     accessConnection.Close() 

    End Try 

    If recordsAffected = 0 Then 
     MsgBox("Record updated failed!", MsgBoxStyle.Exclamation, "Project Analysis System") 
    Else 
     MsgBox("Record updated successfully!", MsgBoxStyle.Information, "Project Analysis System") 
     PopulateClientList() 
    End If 

End Sub 
+1

哪里'accessConnection'和'selectedClient'定义,什么是表'tblClient'的定义是什么? – vane

+0

你在哪里收到“对象引用未设置为对象实例”?另外,为什么不分配selectedClient作为参数@clientID? –

+0

@vane:accessConnection包含我的连接字符串,而selectedClient来自listview,当我从listview中选择一个项目时,它返回一个整数。 –

accessConnection全球(连接对象)变量?您必须在该过程中创建该对象的另一个实例。

还有一件事,clientID也被参数化。然后加入这一行

accessCommand.Parameters.AddWithValue("@clientID", selectedClient) 

更新1

Private Sub UpdateClient() 

    Dim recordsAffected As Integer = 0 
    Dim sqlUpdateClient As String = "UPDATE tblClient " & _ 
            "SET clientCompany = ?, " & _ 
            " clientStreetAddress = ?, " & _ 
            " clientCity = ?, " & _ 
            " clientContactPerson = ?, " & _ 
            " clientContactNumber = ?, " & _ 
            " clientEmail = ?, " & _ 
            " clientMobileNumber = ? " & _ 
            "WHERE clientID = ?" 

    Using accessConnection As New OleDbConnection("connectionStringHere") 
     Using accessCommand As New OleDbCommand() 
      With accessCommand 
       .Connection = accessConnection 
       .CommandType = CommandType.Text 
       .CommandText = sqlUpdateClient 
       .Parameters.AddWithValue("clientCompany", txtClientCompany.Text) 
       .Parameters.AddWithValue("clientStreetAddress", txtClientStreetAddress.Text) 
       .Parameters.AddWithValue("clientCity", txtClientCity.Text) 
       .Parameters.AddWithValue("clientContactPerson", txtClientContactPerson.Text) 
       .Parameters.AddWithValue("clientContactNumber", txtClientPhoneNumber.Text) 
       .Parameters.AddWithValue("clientEmail", txtClientEmailAddress.Text) 
       .Parameters.AddWithValue("clientMobileNumber", txtClientMobileNumber.Text) 
       .Parameters.AddWithValue("clientID", selectedClient) 
      End With 
      Try 
       accessConnection.Open() 
       recordsAffected = accessCommand.ExecuteNonQuery() 
      Catch ex As OleDBException 
       lblError.Text = ex.Message.ToString() 
      Finally 
       accessConnection.Close() 
      End Try 

      If recordsAffected = 0 Then 
       MsgBox("Record updated failed!", MsgBoxStyle.Exclamation, "Project Analysis System") 
      Else 
       MsgBox("Record updated successfully!", MsgBoxStyle.Information, "Project Analysis System") 
       PopulateClientList() 
      End If 
     End Using 
    End Using 

End Sub 

enter image description here

+0

@JohnWoo clientID不需要参数化;这是最好的练习,但不会将其作为参数并将其连接起来,就像他正在做的那样。 – vane

+0

@vane当你用其他值提供参数而有些不提供参数时,它很奇怪。无论如何,这是用户的选择。 –

+0

@JohnWoo我完全同意,并认为它应该参数化,我只是不想错误信息浮动,因为你说它**必须**参数化,这种说法是错误的。 – vane