【问题标题】:query database with each object in Arraylist and databind to gridview?使用Arraylist中的每个对象查询数据库并将数据绑定到gridview?
【发布时间】:2011-08-10 13:28:05
【问题描述】:

我有一个函数可以将帐号列表作为 Arraylist 返回。我正在尝试将每个帐户用作另一个子例程中的命令参数,以获取有关每个帐号的更多数据。这仅返回数组列表中最后一个帐号的数据。我需要使用每个帐号,调用数据库,获取附加信息并将所有数据存储到 Gridview(数据绑定)中。示例:如果我的数组列表中有 3 个帐号,则将 3 行数据返回到 gridview。我正在努力解决如何获取 Arraylist 中每个值(帐号)的所有信息。有人可以指出我正确的方向吗?我认为这是可以做到的,但我不确定我的方法是否正确。也许我需要创建包含通过 arraylist 传递的每个值的附加信息的数据表......有什么想法吗??

@jwatts1980 感谢您的评论:我会尽力澄清。我有一个帐号的数组列表(也许这是我偏离轨道的地方)我试图在另一个调用中使用这个 ArrayList 中的值作为命令参数,以返回有关这些帐户的更多信息的不同表/文件。我将提供一部分代码来帮助澄清我正在尝试做什么:

Private Function ReturnMultAccts(ByVal strAcct) As ArrayList
Dim acctsDetail As New ArrayList
Dim dsn As String = ConfigurationManager.ConnectionStrings.ConnectionString
    Dim sql As String = "SELECT DISTINCT * FROM FILE WHERE ACCTNUM=?"
    Using conn As New OdbcConnection(dsn)
        Using cmd As New OdbcCommand(sql, conn)
            conn.Open()
            cmd.Parameters.Add("ACCTNUM", OdbcType.VarChar, 20).Value = strAcct
            Dim rdrUsers As OdbcDataReader = cmd.ExecuteReader()
            If rdrUsers.HasRows Then
                While rdrUsers.Read()
            acctsDetail.Add(Trim(rdrUsers.Item("ACCTNUM")))
                End While
            End If
            rdrUsers.Close()
            conn.Close()
        End Using
    End Using

这将返回一个帐号的 Arraylist(假设它是 3 个帐号)。我从另一个 Sub 调用这个函数:

Private Sub GetMoreAcctInfo(ByVal strAcct)
    'Create New ArrayList
    Dim MultAccts As New ArrayList
    'Pass strAcct to Function to get Multiples
    MultAccts = ReturnMultAccts(strAcct)
    'Create the variable BachNum for the loop
    Dim BachNum As String = MultAccts.Item(0)
    For Each BachNum In MultAccts
   'Get All of the necessary info from OtherFile based on the BachNum for BOS's
        Dim dsn As String = ConfigurationManager.ConnectionStrings.ConnectionString
        Dim sql As String = "SELECT ACCTNUM, BILSALCOD1, BILSALCOD2, BILSALCOD3, OTHACCTNUM FROM OtherFile WHERE OTHACCTNUM=?" 'Equal to the items in the arraylist
        Using conn As New OdbcConnection(dsn)
            Using cmd As New OdbcCommand(sql, conn)
                conn.Open()
                cmd.Parameters.Add("OTHACCTNUM", OdbcType.VarChar, 20).Value = BachNum
                Using adapter = New OdbcDataAdapter(cmd)
                    Dim DS As New DataSet()
                    adapter.Fill(DS)
                    GridView1.DataSource = DS
                    GridView1.DataBind()
               End Using
            End Using
        End Using
    Next
End Sub

希望这能澄清我正在尝试做的事情......??

【问题讨论】:

  • 您正在使用....?我觉得你的东西和你的结束
  • 抱歉,gridview 之后的时间段放错了......它应该说“......并绑定到我正在使用的 gridview。”很抱歉造成混乱。
  • 我将其添加为评论而不是答案,因为我对此并不完全确定。但我认为,如果你从你的函数中返回一个数据数组,该函数采用 acct #,然后将所有这些数组放入一个数组列表(即对象数组的数组列表),那么你可以将该数组列表绑定为数据源网格视图。

标签: vb.net gridview datatable dataset data-binding


【解决方案1】:

要详细说明我的建议,您需要一个强类型对象列表。您可以将这些项目添加到列表中,然后将列表绑定到 GridView。

我将从头开始。您知道来自数据库的数据类型:ACCTNUM、BILSALCOD1、BILSALCOD2、BILSALCOD3 和 OTHACCTNUM。所以你可以使用它们来创建一个对象。

Friend Class AccountClass
    Private pACCTNUM As string = ""
    Private pBILSALCOD1 As string = ""
    Private pBILSALCOD2 As string = ""
    Private pBILSALCOD3 As string = ""
    Private pOTHACCTNUM As string = ""

    Public Property ACCTNUM() As string
        Get
            Return pACCTNUM
        End Get
        Set(ByVal value as string)
            Me.pACCTNUM = value
        End Set
    End Property

    Public Property BILSALCOD1() As string
        Get
            Return pBILSALCOD1
        End Get
        Set(ByVal value as string)
            Me.pBILSALCOD1 = value
        End Set
    End Property

    Public Property BILSALCOD2() As string
        Get
            Return pBILSALCOD2
        End Get
        Set(ByVal value as string)
            Me.pBILSALCOD2 = value
        End Set
    End Property

    Public Property BILSALCOD3() As string
        Get
            Return pBILSALCOD3
        End Get
        Set(ByVal value as string)
            Me.pBILSALCOD3 = value
        End Set
    End Property

    Public Property OTHACCTNUM() As string
        Get
            Return pOTHACCTNUM
        End Get
        Set(ByVal value as string)
            Me.pOTHACCTNUM = value
        End Set
    End Property

    Sub New(ByVal ACCTNUM As string, ByVal BILSALCOD1 As string, ByVal BILSALCOD2 As string, ByVal BILSALCOD3 As string, ByVal OTHACCTNUM As string)
        Me.ACCTNUM = ACCTNUM
        Me.BILSALCOD1 = BILSALCOD1
        Me.BILSALCOD2 = BILSALCOD2
        Me.BILSALCOD3 = BILSALCOD3
        Me.OTHACCTNUM = OTHACCTNUM
    End Sub
End Class

然后您重新编写 GetMoreAcctInfo() 例程以使用它。

Private Sub GetMoreAcctInfo(ByVal strAcct)
    'Create New ArrayList
    Dim MultAccts As ArrayList

    'Pass strAcct to Function to get Multiples
    MultAccts = ReturnMultAccts(strAcct)

    'Create the variable BachNum for the loop
    Dim BachNum As String

    'Create the list to bind to the grid
    Dim AcctInfo As New Generic.List(Of AccountClass)

    'create the dataset
    Dim DS As DataSet

    For Each BachNum In MultAccts
   'Get All of the necessary info from OtherFile based on the BachNum for BOS's
        Dim dsn As String = ConfigurationManager.ConnectionStrings.ConnectionString
        Dim sql As String = "SELECT ACCTNUM, BILSALCOD1, BILSALCOD2, BILSALCOD3, OTHACCTNUM FROM OtherFile WHERE OTHACCTNUM=?" 'Equal to the items in the arraylist
        Using conn As New OdbcConnection(dsn)
            Using cmd As New OdbcCommand(sql, conn)
                conn.Open()
                cmd.Parameters.Add("OTHACCTNUM", OdbcType.VarChar, 20).Value = BachNum
                Using adapter = New OdbcDataAdapter(cmd)
                    DS = New DataSet()
                    adapter.Fill(DS)

                    For Each t As DataTable In DS.Tables
                        For Each r As DataRow In t.Rows
                            AcctInfo.Add(new AccountClass(r("ACCTNUM"), r("BILSALCOD1"), r("BILSALCOD2"), r("BILSALCOD3"), r("OTHACCTNUM")))
                        Next
                    Next
               End Using
            End Using
        End Using
    Next

    GridView1.DataSource = AcctInfo
    GridView1.DataBind()
End Sub

【讨论】:

  • jwatts1980 感谢您的帮助(尤其是添加的代码)我一定会在早上第一件事尝试一下。如果我遇到任何“障碍”,我肯定会提出更多问题。谢谢,
  • 由于某种原因我无法让它工作。我今天将继续努力,看看我是否可以让它发挥作用。我想我可能对函数 ReturnMultAccts 有疑问。我可以将此函数中的数据显示/数据绑定到 Gridview,以查看 Sub GetMoreAcctInfo 工作所需的 OTHACCTNUM。但它似乎并没有像我认为的那样循环数据。
  • 我终于让它工作了。它现在在 gridview 中显示数据。我确实对 Gridview 中的数据有疑问。有没有办法更改列标题?有没有办法将特定的数据行传递给另一个子例程(类似于 RowDataBound?)。说 Col(1) 还是 Col(2)??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-31
  • 2012-10-07
  • 1970-01-01
  • 2018-11-16
相关资源
最近更新 更多