-1

我正在尝试在我的 vb 应用程序的列表视图中显示 sql 数据库表数据。我使用了以下代码

Public Class Form1
    Dim conn As SqlClient.SqlConnection
    Dim cmd As SqlClient.SqlCommand
    Dim da As SqlClient.SqlDataAdapter
    Dim ds As DataSet
    Dim itemcoll(100) As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.ListView1.View = View.Details
    Me.ListView1.GridLines = True
    conn = New SqlClient.SqlConnection("Data Source=AYYAGARI-PC\WINCC;Initial Catalog=anand;Integrated Security=True")
    conn.Open()
    Dim strQ As String = String.Empty
    strQ = "SELECT * FROM [anand].[dbo].[WINCC] ORDER BY [dateandtime]"
    cmd = New SqlClient.SqlCommand(strQ, conn)
    da = New SqlClient.SqlDataAdapter(cmd)
    ds = New DataSet
    da.Fill(ds, "Table")
    Dim i As Integer = 0
    Dim j As Integer = 0
    ' adding the columns in ListView
    For i = 0 To ds.Tables(0).Columns.Count - 1
        Me.ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
    Next
    'Now adding the Items in Listview
    Try
        Call Timer1_Tick(sender, e)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    For Each i As ListViewItem In ListView1.SelectedItems
        ListView1.Items.Remove(i)
    Next
    Try
        For i = 0 To ds.Tables(0).Rows.Count - 1
            For j = 0 To ds.Tables(0).Columns.Count - 1
                itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
            Next
            Dim lvi As New ListViewItem(itemcoll)
            Me.ListView1.Items.Add(lvi)
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

上面代码的问题是,每当我在 sql 中更新表数据(插入或删除数据)时,列表视图中都不会更新。请帮我解决这个问题。

4

1 回答 1

0
    Me.ListView1.View = View.Details
    Me.ListView1.GridLines = True
    conn = New SqlClient.SqlConnection("Data Source=AYYAGARI-PC\WINCC;Initial Catalog=anand;Integrated Security=True")
    conn.Open()
    Dim strQ As String = String.Empty
    strQ = "SELECT * FROM [anand].[dbo].[WINCC] ORDER BY [dateandtime]"
    cmd = New SqlClient.SqlCommand(strQ, conn)
    da = New SqlClient.SqlDataAdapter(cmd)
    ds = New DataSet
    da.Fill(ds, "Table")
    Dim i As Integer = 0
    Dim j As Integer = 0
    ' adding the columns in ListView
    For i = 0 To ds.Tables(0).Columns.Count - 1
        Me.ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
    Next
    'Now adding the Items in Listview
    Try
        Call Timer1_Tick(sender, e)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

尝试再次将此代码添加到进行插入或更新的按钮中。

更新

我给你另一个提示,这不会让你的代码那么长并且浪费内存或容量..

尝试将所有代码插入到新子中,例如:

    private sub mycode
    msgbox("Hello World")
    end sub >> This code won't work when you debug, cause it just like a stored code, so need to be called to make it work

Private sub Form_load......  > E.g this is your form load code
call mycode  >> Called the code above
end sub >> when you debug, your form will auto called the mycode
于 2013-04-08T06:14:11.793 回答