Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 用表中的项填充数组_Vb.net - Fatal编程技术网

Vb.net 用表中的项填充数组

Vb.net 用表中的项填充数组,vb.net,Vb.net,我在VB.NET中有一个小要求,那就是用从表中检索的值填充数组。也就是说,我有一个名为“user\u roles”的表,它有一个名为“role\u id”和“role\u name”的列。在表单的加载事件中,我希望执行一个过程,并将表“user\u roles”中的所有项(role\u id)填充到一个数组中 谁能帮我一下这个要求吗 问候,, George我认为最好使用数组而不是数组。如果我错了,请纠正我 如果您已经在codebehind中填充了表,那么可以通过遍历所有行来添加rolw_id D

我在VB.NET中有一个小要求,那就是用从表中检索的值填充数组。也就是说,我有一个名为“user\u roles”的表,它有一个名为“role\u id”和“role\u name”的列。在表单的加载事件中,我希望执行一个过程,并将表“user\u roles”中的所有项(role\u id)填充到一个数组中

谁能帮我一下这个要求吗

问候,,
George

我认为最好使用数组而不是数组。如果我错了,请纠正我

如果您已经在codebehind中填充了表,那么可以通过遍历所有行来添加rolw_id

Dim allRoleIDs As New List(Of Int32)
For Each row As DataRow In user_roles.Rows
    allRoleIDs.Add(CInt(row("role_id)")))
Next
考虑到性能原因,在这里使用Datareader更好

当您使用强类型数据集并希望避免在填充数据表以将ID添加到列表中后出现额外的往返时,您必须扩展自动生成的数据集DataAdapter类(即称为user_rolesTableAdapter)

不要为此使用Dataset的designer.vb类,因为它将在每次数据集更改时被覆盖。使用其codebehind类(不带designer.vb),首先从自动生成的TableAdapter(例如DatasetNameTableAdapter,其中DatasetName是数据集的名称)添加相同的命名空间。然后添加以下类(替换部分类的正确命令、列索引、类名):

命名空间DatasetNameTableAdapters
部分公共类用户角色适配器
公共函数getListOfUserRolesID()作为System.Collections.Generic.List(属于System.Int32)
作为新System.Collections.Generic.list的Dim列表(属于System.Int32)
Dim命令作为System.Data.SqlClient.SqlCommand=Me.CommandCollection(0)
将以前的连接状态设置为System.Data.ConnectionState=command.Connection.State
If((command.Connection.State和System.Data.ConnectionState.Open)_
System.Data.ConnectionState.Open)然后
command.Connection.Open()
如果结束
尝试
将读卡器用作System.Data.SqlClient.SqlDataReader=command.ExecuteReader
边读边读
list.Add(reader.GetInt32(0))
结束时
终端使用
最后
如果(previousConnectionState=System.Data.ConnectionState.Closed),则
command.Connection.Close()命令
如果结束
结束尝试
返回列表
端函数
末级
结束命名空间
现在,您可以直接从Dataadapter获取用户角色ID作为通用列表,而无需迭代两次(第一次是在填充datatable时,第二次是在将ID添加到列表时)


当然,您也可以在Page.Load中使用此Datareader方法,而不使用数据集。

问题:您是如何获得信息的?您是将其拉入数据集还是将其拉入数据读取器?亲爱的,能否给我一个关于如何使用数据读取器的示例。我没有试过,但我有一个相同的要求。非常感谢蒂姆和泛型是一个非常好的方法。我正在使用VB.NET2005,我可以在其中使用泛型吗?@George,是的,你可以在VB.NET2005中使用泛型。它们是在.NET2.0中引入的,这正是VB.NET2005的目标。好建议。我正要建议使用一个块,但这些块直到3.5版本才在VB中可用。
Namespace DatasetNameTableAdapters

    Partial Public Class user_rolesTableAdapter

        Public Function getListOfUserRolesID() As System.Collections.Generic.List(Of System.Int32)
            Dim list As New System.Collections.Generic.List(Of System.Int32)
            Dim command As System.Data.SqlClient.SqlCommand = Me.CommandCollection(0)

            Dim previousConnectionState As System.Data.ConnectionState = command.Connection.State
            If ((command.Connection.State And System.Data.ConnectionState.Open) _
                        <> System.Data.ConnectionState.Open) Then
                command.Connection.Open()
            End If
            Try
                Using reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader
                    While reader.Read
                        list.Add(reader.GetInt32(0))
                    End While
                End Using
            Finally
                If (previousConnectionState = System.Data.ConnectionState.Closed) Then
                    command.Connection.Close()
                End If
            End Try

            Return list
        End Function
    End Class

End NameSpace