【问题标题】:UPDATE Records in a SQL Server Database through vb.net通过 vb.net 更新 SQL Server 数据库中的记录
【发布时间】:2017-04-06 04:25:19
【问题描述】:

你好,我需要你的帮助

我正在努力解决如何从 vb.net 更新数据库 sql server 中的任何记录

首先。我在 vb 中创建了一个类,以将所有功能和所有功能从按钮 UPDATE 事件中放入所有功能,即更有条理。

我已经成功地完成了所有的编码(更新功能),没有任何错误,但令人惊讶的是,它没有更新任何字段。

请帮忙

这是我的课程,我在其中创建所有函数并从按钮事件中调用它们:

 Public Function UpdateDataRecord(Command As String) As Integer
    Try
        SQLConnector.Open()
        SQLCommand = New SqlCommand(Command, SQLConnector)


        Dim ChangeCount As Integer = SQLCommand.ExecuteNonQuery()

        SQLConnector.Close()

        Return ChangeCount
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Return 0
End Function

这里是我的更新按钮

 Private Sub updateBtn_Click(sender As Object, e As EventArgs) Handles updateBtn.Click

    If IDTextBox.Text <> "" Then
        If (NameTextBox.Text.Length) >= 3 Then

            Dim UpdateThisNow As String = "Update tblActivity " &
                           "Set Name='" & NameTextBox.Text & "' " &
                         "WHERE ID ='" & IDTextBox.Text & "' "


            If sqlcontrol.UpdateDataRecord(UpdateThisNow) = 0 Then

                MsgBox("Not Found")

            Else
                MsgBox("Successfully updated")

            End If

        Else
            MsgBox("Data Entered too short!")
        End If

    Else
        MsgBox("You must provide ID number")

    End If




End Sub

【问题讨论】:

  • 我忘了说我要添加更多单元格,即“Set Name='” & NameTextBox.Text & “'” &.
  • 那么如何添加更多单元格
  • 建议您使用参数化字符串重写。你到达执行处了吗? ChangeCount 是 0 吗?得到任何错误?您的数据库连接字符串在哪里分配和使用?
  • 首先:尽量使用参数避免SQL注入。然后:您的函数有错误。将“返回 0”改为“返回更改计数”。
  • 附录:这也行不通。您必须在函数的第一行写“Dim ChangeCount As Integer = 0”

标签: asp.net sql-server vb.net


【解决方案1】:

尝试用这个替换你的函数。

Public Function UpdateDataRecord(ByVal name As String, ByVal id As Integer) As Integer
    Dim result As Integer 'will save the affected records count later
    Dim sqlConnStr As String = "ConnectionString" 'put a valid connection string value here
    Dim sqlCmdStr As String = "UPDATE tblActivity SET Name = @Name WHERE ID = @ID" 'your SQL UPDATE query with parameters to avoid SQL injection attacks
    Using sqlConn As New SqlConnection(sqlConnStr) 'declare a SQL connection object and uses your connection string
        Using sqlCmd As New SqlCommand(sqlCmdStr) With {.Connection = sqlConn} 'declare a SQL command object and uses your UPDATE query, and the connection object above
            Try
                sqlConn.Open() 'Open the connection
                With sqlCmd.Parameters
                    .Clear() 'clear the parameters
                    .AddWithValue("Name", name) 'add the value to the parameterized query using the values you got from your textbox
                    .AddWithValue("ID", id)
                End With
                result = sqlCmd.ExecuteNonQuery() 'set the value of the result integer
            Catch ex As Exception
                result = 0
            Finally
                sqlConn.Close()
            End Try
        End Using
    End Using
    Return result
End Function

然后这样称呼它:

sqlcontrol.UpdateDataRecord(NameTextBox.Text, IDTextBox.Text)

【讨论】:

  • 但这是一个类中的函数,即使在主窗体中,我也需要从更新按钮调用它。如何正确调用它?
  • And Also where it saya: Dim sqlCmdStr As String = "UPDATE tblActivity SET Name = @Name WHERE ID = @ID" '你的 SQL UPDATE 查询带有参数以避免 SQL 注入攻击 ,,,,, ,,,,, 我需要添加更多行
  • 你的意思是更多的列?给我特定的列,以便我可以编辑答案并向您展示如何做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 2019-12-17
相关资源
最近更新 更多