使用来自vb.net中listview的数据更新表

时间:2019-07-24 16:18:52

标签: vb.net

Public Sub FeesFromSetFeesContinuous(ByRef lst As ListView, ByRef txtAmt As TextBox, ByRef txtYear As TextBox, ByRef cboClass As ComboBox, ByRef cboTerm As ComboBox, cboMode As ComboBox, toText As TextBox, add As TextBox)
    Try
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim id As New TextBox
        Dim bal As New TextBox
        Dim cmd As New SqlCommand

        For Each item As ListViewItem In lst.Items
            id.Text = item.SubItems(0).Text
            bal.Text = item.SubItems(1).Text
            sql = "update fees set class='" & cboClass.Text & "'," &
                  "year ='" & txtYear.Text & "'," & "mode='" & cboMode.Text & "'," &
                  "term ='" & cboTerm.Text & "'," &
                  "balance='" & bal.Text & "'" & "where id = '" & id.Text & "'"
            cmd = New SqlCommand(sql, con)
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            MessageBox.Show("done")
        Next

        con.Close()
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
End Sub

我的消息已经完成,但是什么也没有真正改变或影响表中的数据。请任何帮助

1 个答案:

答案 0 :(得分:0)

请勿通过控件。不要通过他们ByRef。仅传递您要使用的值。我想您已经厌倦了在参数列表末尾键入ByRef。我认为最后两个都不用。

将数据库对象保留在本地,以便您可以控制它们的关闭和处理。 Using...End Using块会为您解决这个问题。

不要将值存储在不可见的文本框中。使用变量。

您正在为For循环的每次迭代创建新的字符串和新的命令。这是非常低效的。

使用参数来避免Sql注入。只有2个参数可以更改其值。将所有参数仅添加一次到命令。其中大多数的值仅设置一次。循环中只设置了两个更改。

您在循环的每次迭代中都显示“完成”消息框。连接打开时,切勿显示消息框。您的用户本可以去吃午饭,直到他们返回时才对消息框做出响应。

如下创建数据访问类。

Public Class Fees
    Public Sub FeesFromSetFeesContinuous(IDList As List(Of String), BalanceList As List(Of String), Amt As String, Year As String, Clss As String, Term As String, Mode As String)
        Dim Sql = "Update fees Set class = @Class, year = @Year, mode = @Mode,
              term = @Term, balance = @Balance where id = @ID;"
        Using cn As New SqlConnection("Your connection string")
            Using cmd As New SqlCommand(Sql, cn)
                With cmd.Parameters
                    .Add("@Class", SqlDbType.VarChar).Value = Clss 'Class is a word in the language
                    .Add("@Year", SqlDbType.VarChar).Value = Year
                    .Add("@Mode", SqlDbType.VarChar).Value = Mode
                    .Add("@Term", SqlDbType.VarChar).Value = Term
                    .Add("@Balance", SqlDbType.VarChar)
                    .Add("@ID", SqlDbType.VarChar)
                End With
                cn.Open()
                For index = 0 To IDList.Count - 1
                    cmd.Parameters("@Balance").Value = BalanceList(index)
                    cmd.Parameters("@ID").Value = IDList(index)
                    cmd.ExecuteNonQuery()
                Next
            End Using
        End Using
        MessageBox.Show("done")
    End Sub
End Class

然后在用户界面(窗体)中

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lstId As New List(Of String)
    Dim lstBalance As New List(Of String)
    For Each li As ListViewItem In ListView1.Items
        lstId.Add(li.SubItems(0).ToString)
        lstBalance.Add(li.SubItems(1).ToString)
    Next
    Dim f As New Fees()
    Try
        f.FeesFromSetFeesContinuous(lstId, lstBalance, txtAmt.Text, txtYear.Text, cboClass.Text, cboTerm.Text, cboMode.Text)
        MessageBox.Show("Done")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

您将需要检查数据库以获取正确的字段数据类型,并相应地调整代码。重做SqlDbType值,并根据需要转换任何字符串。由于您将所有值都括在单引号中,因此我假设使用VarChar(字符串),但我怀疑至少id,balance和year可能是数字类型。