如何将从数据读取器检索到的列保存到数组中?

发布于 2024-10-23 00:50:35 字数 543 浏览 5 评论 0原文

我想检索表中大约 800 行的单列,并将这些值保存在数组中,以便稍后引用它们。我该怎么做呢?我尝试过:

    con = New OleDb.OleDbConnection("provider=SQLOLEDB;data source=PC;initial catalog=DB1;integrated security=SSPI")  

    cmd = New OleDbCommand("select col1 from table1", con)

    con.Open()

    r = cmd.ExecuteReader

    While r.Read

        prev_ob(i) = r.Item(0)(0)

        i = i + 1

    End While

    For i = 0 To UBound(prev_ob)

        Console.WriteLine(prev_ob(i))

    Next`

但没有成功。 col1 是 Int64 类型,数组 prev_ob() 也是如此。请让我知道如何将其保存到数组中。

I would like to retrieve a single column of about 800 rows in a table and save these values in an array so as to reference them later on. How can I do it? I tried:

    con = New OleDb.OleDbConnection("provider=SQLOLEDB;data source=PC;initial catalog=DB1;integrated security=SSPI")  

    cmd = New OleDbCommand("select col1 from table1", con)

    con.Open()

    r = cmd.ExecuteReader

    While r.Read

        prev_ob(i) = r.Item(0)(0)

        i = i + 1

    End While

    For i = 0 To UBound(prev_ob)

        Console.WriteLine(prev_ob(i))

    Next`

And it didn't work. The col1 is of type Int64 and so is the array prev_ob(). Please let me know how I can save it to an array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2024-10-30 00:50:35

您遇到的错误是什么?更多信息将帮助您更快获得答案。

试试这个:

Dim int64list As New List(Of Int64)

cmd = New OleDbCommand("select col1 from table1", con)

    con.Open()
    r = cmd.ExecuteReader

    While r.Read

        int64list.Add(Convert.ToInt64(r.Item(0))

    End While

For Each obj In int64list
     Console.WriteLine(obj)
Next obj

PS 我编写了一个可以在 C# 中运行的示例,并尝试转换为 VB.net。

What is the error your are getting? More information will help you in getting an answer sooner.

Try this:

Dim int64list As New List(Of Int64)

cmd = New OleDbCommand("select col1 from table1", con)

    con.Open()
    r = cmd.ExecuteReader

    While r.Read

        int64list.Add(Convert.ToInt64(r.Item(0))

    End While

For Each obj In int64list
     Console.WriteLine(obj)
Next obj

PS I wrote a sample that works in C# and tried translating to VB.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文