【问题标题】:How to Add Data from SQL to a table and use that table in a listview如何将 SQL 中的数据添加到表中并在列表视图中使用该表
【发布时间】:2016-09-26 15:08:24
【问题描述】:

我有一个从 sql server 上的表中获取数据的类,我将数据分配给变量,然后在列表视图中的列下输出数据,问题是我只得到我需要的 sql 表中的第一行一个循环来填充一些充满数据值的容器并将所有行传递给listview。我将如何做到这一点,我的编程经验非常有限。如何首先从下面的代码将数据从 sql 获取到可用的容器中?

Public Class Inventory
Public mFirstName As String
Public mLastName As String
Public mComputerType As String
Public mAssetTag As String
Public mCheckOutDate As Date
Public mCheckInDate As Date
Public mExpectedReturnDate As Date
Public mUserEmailAddress As String
Public mLoanSubmitter As String
Public mDeployed As Integer

Public Sub New()
    LoadData()
End Sub

Public Sub New(dr As DataRow)

End Sub

Private Sub LoadData()
    Dim dbConn As HUG.Core.Database.SQLConnection
    Dim sql As String = ""
    Dim ds As New DataSet

    sql = "SELECT * FROM HDData.dbo.TravelLaptopRecords "

    dbConn = New HUG.Core.Database.SQLConnection("WorkFiles")
    ds = dbConn.FillDataSet(sql)

    If Not IsNothing(ds) Then
        If ds.Tables(0).Rows.Count > 0 Then
            With ds.Tables(0).Rows(0)
                mFirstName = CStr(.Item("FirstName"))
                mLastName = CStr(.Item("LastName"))
                mComputerType = CStr(.Item("ComputerType"))
                mAssetTag = CStr(.Item("AssetTag"))
                mCheckOutDate = CDate(.Item("CheckOutDate"))
                mCheckInDate = CDate(.Item("CheckInDate"))
                mExpectedReturnDate = CDate(.Item("ExpectedReturnDate"))
                mUserEmailAddress = CStr(.Item("UserEmailAddress"))
                mLoanSubmitter = CStr(.Item("LoanSubmitter"))
                mDeployed = CInt(.Item("Deployed"))
            End With
        End If
    End If

End Sub
 End Class

这是表单页面

Public Class Form1
Private mLaptopInventory As Inventory
Private isLoad As Boolean
Private mFirstName As String
Private mLastName As String
Private mComputerType As String
Private mAssetTag As String
Private mCheckOutDate As Date
Private mCheckInDate As Date
Private mExpectedReturnDate As Date
Private mUserEmailAddress As String
Private mLoanSubmitter As String
Private mDeployed As Integer

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    HUG.Core.Globals.BootStrap("G:\Programs\somefile.ini")

    isLoad = True
End Sub

Private Sub Form1_Shown(Sender As Object, e As EventArgs) Handles Me.Shown
    mLaptopInventory = New Inventory()
    LoadForm()
    isLoad = False

End Sub

Private Sub LoadForm()
    GroupBox1.Text = "Travel Laptop Inquiry"
    InventoryList.View = View.Details

    mFirstName = mLaptopInventory.mFirstName
    mLastName = mLaptopInventory.mLastName
    mComputerType = mLaptopInventory.mComputerType
    mAssetTag = mLaptopInventory.mAssetTag
    mCheckOutDate = mLaptopInventory.mCheckOutDate
    mCheckInDate = mLaptopInventory.mCheckInDate
    mExpectedReturnDate = mLaptopInventory.mExpectedReturnDate
    mUserEmailAddress = mLaptopInventory.mUserEmailAddress
    mLoanSubmitter = mLaptopInventory.mLoanSubmitter
    mDeployed = mLaptopInventory.mDeployed


    InventoryList.Items.Add(mFirstName)
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mLastName)
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mComputerType)
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mAssetTag)
    If mDeployed = -1 Then
        InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mCheckOutDate)
        InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add("Item Not Returned").ToString()
        InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mExpectedReturnDate)
    Else
        InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add("Item is on Hand").ToString()
        InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mCheckInDate)
        InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add("").ToString()
    End If
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mUserEmailAddress)
    InventoryList.Items(InventoryList.Items.Count - 1).SubItems.Add(mLoanSubmitter)




End Sub
End Class

【问题讨论】:

  • 你在哪里有ListView?通常你会将DataTable 设置为ListView 上的DataSource 属性。您将一组变量显式设置为结果中第一条记录中的值,但您在这里根本没有使用ListView
  • 假设您使用的是 Windows 窗体,您应该做的只是将您的 DataTable 分配给 DataGridViewDataSource 属性。 ListView 不是网格控件,不应用作一个。您正在编写一大堆代码。它是填充网格的单线。
  • 一个数据表“一个可用的容器”,它将保存类型化的数据(日期是日期,整数是整数,布尔值是布尔值)
  • 对不起,我也发布了表单页面,我在列表视图中分配项目,好的,这听起来不错,我需要做的就是用我从 sql 中获取的数据填充数据表然后?

标签: sql sql-server vb.net


【解决方案1】:

您只使用 ds.Tables(0).Rows(0) 检查第一行。您应该遍历所有行并将数据添加到列表中。

从 New() 中移除加载

Public Sub New()
End Sub

然后将您的 LoadData 更改为公共共享

Public Shared Function LoadData() As List(Of Inventory)
    Dim dbConn As HUG.Core.Database.SQLConnection
    Dim sql As String = ""
    Dim ds As New DataSet
    Dim result As New List(Of Inventory)

    sql = "SELECT * FROM HDData.dbo.TravelLaptopRecords "

    dbConn = New HUG.Core.Database.SQLConnection("WorkFiles")
    ds = dbConn.FillDataSet(sql)

    If Not IsNothing(ds) Then
        For i As Integer = 0 To ds.Tables(0).Rows.Count-1
            Dim newInventory As New Inventory

            newInventory.mFirstName = CStr(ds.Tables(0).Rows(i).Item("FirstName"))
            newInventory.mLastName = CStr(ds.Tables(0).Rows(i).Item("LastName"))
            newInventory.mComputerType = CStr(ds.Tables(0).Rows(i).Item("ComputerType"))
            newInventory.mAssetTag = CStr(ds.Tables(0).Rows(i).Item("AssetTag"))
            newInventory.mCheckOutDate = CDate(ds.Tables(0).Rows(i).Item("CheckOutDate"))
            newInventory.mCheckInDate = CDate(ds.Tables(0).Rows(i).Item("CheckInDate"))
            newInventory.mExpectedReturnDate = CDate(ds.Tables(0).Rows(i).Item("ExpectedReturnDate"))
            newInventory.mUserEmailAddress = CStr(ds.Tables(0).Rows(i).Item("UserEmailAddress"))
            newInventory.mLoanSubmitter = CStr(ds.Tables(0).Rows(i).Item("LoanSubmitter"))
            newInventory.mDeployed = CInt(ds.Tables(0).Rows(i).Item("Deployed"))

            result.Add(newInventory)
        Next
    End If

    Return result
End Function

至少现在当您调用 Inventory.LoadData() 时,您将获得所有数据。这不是最好的做事方式,但它应该让你朝着正确的方向前进。

【讨论】:

  • 在我的 Form1_shown 中。我有 mLaptopInventory = New Inventory() 然后 Inventory.LoadData() 但没有填充
  • @Edgar 如果你使用我写的,应该是 mLaptopInventory = Inventory.LoadData()
【解决方案2】:

您正在选择第 0 行(使用 ds.Tables(0).Rows(0)),它需要像这样处于循环中:

For i As Integer = 0 To ds.Tables(0).Rows.Count-1
    ' Get the strings here
    ' ...
    mFirstName = ds.Tables(0).Rows(i).Item("FirstName").ToString
Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多