【发布时间】: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分配给DataGridView的DataSource属性。ListView不是网格控件,不应用作一个。您正在编写一大堆代码。它是填充网格的单线。 -
一个数据表是“一个可用的容器”,它将保存类型化的数据(日期是日期,整数是整数,布尔值是布尔值)
-
对不起,我也发布了表单页面,我在列表视图中分配项目,好的,这听起来不错,我需要做的就是用我从 sql 中获取的数据填充数据表然后?
标签: sql sql-server vb.net