【问题标题】:ADODB RecordSet to String() in VB.NETVB.NET 中的 ADODB RecordSet 到 String()
【发布时间】:2019-08-05 14:48:39
【问题描述】:

我发现这篇文章对我的项目有帮助:

ADODB RecordSet to string variable VBA

我想把它改成 VB.NET 而不是 VBA,但它不起作用。

我的目标是使用 SQL 创建一个 String()。为了让您了解硬编码后字符串的外观,我们是这样做的:

Public Sub New()
InitializeComponent()
strValue = New String() {"10051", "65658", "25689" etc... }
End Sub



**'My actual code is as follows:**

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) 
Handles MyBase.Load

Dim Connection1 As New ADODB.Connection
Dim Connection1 As New ADODB.Connection
Dim RecordSet1 As New ADODB.Recordset
Dim SqlQuery1 As String, ConnectionString1 As String
Dim strValue As String()

SqlQuery1 = "Select ItemCode From OITM"
ConnectionString1 = "Driver=SQL Server;Server=Myserver; Database=MyDbase; 
User Id = sa; Password= 12345"
Connection1.Open(ConnectionString1)
RecordSet1 = New ADODB.Recordset
RecordSet1.Open(SqlQuery1, Connection1)
strValue = TryCast(RecordSet1.Fields("ItemCode").Value, String())

Do While RecordSet1.EOF = False
strValue = TryCast(RecordSet1.Fields("ItemCode").Value, String())

MsgBox(strValue)
RecordSet1.MoveNext()
Loop

我希望 MsbBox 在每次循环时显示 ItemCode,并构建整个(非常长的)字符串。但它显示为空白。

我读到 EOF 在 VB .NET 中可能不起作用。

我必须做什么?

【问题讨论】:

    标签: vb.net ado.net recordset


    【解决方案1】:

    我看到了一些问题:

    首先,您将从记录集中获得的值转换为字符串数组。为什么?你不应该这样做,尝试像这样直接设置值:

    strValue = RecordSet1.Fields("ItemCode").Value  
    

    其次,您没有连接字符串。使用“+”或“&”连接您从每一行读取的内容,如下所示:

    Do While RecordSet1.EOF = False
        strValue = strValue + RecordSet1.Fields("ItemCode").Value
    
       MsgBox(strValue)
       RecordSet1.MoveNext()
    Loop
    

    但是您需要一个数组来满足您的需求。所以你可以使用一个列表,然后使用 toArray 方法:

    Dim strValues As String()
    Dim strList As List(Of String) = New List(Of String)()
    
    Do While RecordSet1.EOF = False
        strList.Add(RecordSet1.Fields("ItemCode").Value)
        RecordSet1.MoveNext()
    Loop
    
    strValues = strList.ToArray()
    

    现在 strValues 是从 SQL 数据库读取的字符串数组。

    【讨论】:

    • of Autocomplete.......................在他的例子中,他使用一个硬编码的数组,如下所示:......................................Public Sub New() InitializeComponent()........ collections = New String() {"William James", "Robin Hood", "David Copperfield", "Albert Einstein", "Me", "You", "理查德·D·费曼”、“大卫·贝克汉姆”、“费米”、“某个地方的某个人”}....
    • 问题不是你为什么要使用数组(这没关系),我的问题是:你为什么要把值转换为 string():strValue = TryCast(RecordSet1.字段(“项目代码”)。值,字符串())?你不应该这样做。尝试将其设置为不进行简单转换: strValue = RecordSet1.Fields("ItemCode").Value
    • ctd...由于后面的代码使用的是字符串数组,所以我必须生成一个类似的数组(使用SQL),否则修改后的程序将无法运行。所以,本质上我的问题是:如何从 SQL 中生成类似于示例中的硬编码数组的字符串数组?
    • 您是否尝试过使用分隔符和拆分功能?正如我在答案中向您展示的那样?还是列表?
    • 对不起。我打字很慢,5分钟后,我无法编辑我的线程。我希望你能够明白。请询问更多信息。下次,我会输入Word并复制到这个论坛:)
    【解决方案2】:

    我希望 MsbBox 在每次循环时显示 ItemCode,并构建整个(非常长的)字符串

    这不是它的工作原理。每次循环,您都有一项。代码中没有任何内容会累积每次迭代的值。尝试将其转换为数组,即使它可以工作(它不工作)仍然只会在每次迭代时只为您提供一个 新数组实例,其中只有一项。再说一次,即使这么多也不会发生。您不能只将单个项目转换为数组。

    不仅如此,.Net 还使用真实数组,而不是您现在在其他语言中看到的类似数组的集合。 .Net 也有这些集合,但它不会试图假装它们是数组。真实数组的关键属性之一是固定大小。首次分配数组时,您必须知道数组中的元素数。甚至ReDim Preserve 也依赖于此,有效地分配一个全新的数组,并在每次使用它时复制所有元素。这意味着期望将项目转换为数组变量将连接到数组真的很奇怪。听起来您可能需要 List(Of String) 而不是数组。

    如果您的程序的其他部分需要一个数组,您需要将它们更改为使用List(Of String)。基本数组在 .Net 中不再经常使用,大多被通用 List(Of T) 类型所取代。并且只要有可能,让您的方法参数请求IEnumerable(Of T) 而不是T()List(Of T)。当您使用IEnumerable(Of T) 时,数组和列表都可以作为参数传递。

    另一个问题是使用带有MsgBox() 的数组。消息框不够聪明,无法知道如何处理数组。您最终会退回到 System.Object.ToString() 重载,它只返回类型名称。因此,即使其他一切都有效(它不会),您最希望看到的就是文本 System.Array

    最后,不要在 VB.Net 中使用 RecordSet

    旧的 ADO 库仅在向前移植代码时为了向后兼容而存在。你不应该在新的开发中使用它。请改用较新的 ADO.Net API。

    试试这个:

    'At the very top of the file:
    Imports System.Data.SqlClient
    Imports System.Collections.Generic
    Imports System.Text
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) 
        Handles MyBase.Load
    
        'I know this is just a sample, but DON'T USE THE SA ACCOUNT!
        Dim ConnectionString As String = "Server=Myserver;Database=MyDbase;User Id=sa;Password=12345"    
        Dim Sql As String = "Select ItemCode From OITM"
    
        Dim result As New List(Of String) 'The "array" (really an array-like collection)
        Dim message As New StringBuilder() 'For building up the string
    
        Using connection As New SqlConnection(ConnectionString), _
              command As New SqlCommand(sql, connection)
    
            connection.Open()
            Using reader As SqlDataReader = command.ExecuteReader()
                Dim delimiter As String = ""
                While reader.Read()
                   Dim itemCode As String = reader("ItemCode").ToString()
                   result.Add(itemCode)
                   message.Append(delimiter).Append(itemCode)
                   MessageBox.Show(message.ToString())
                   delimiter = ","
                End While
            End Using
        End Using
    End Sub
    

    另一种选择是创建一个数据表:

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) 
        Handles MyBase.Load
    
        'I know this is just a sample, but DON'T USE THE SA ACCOUNT!
        Dim ConnectionString As String = "Server=Myserver;Database=MyDbase;User Id=sa;Password=12345"    
        Dim Sql As String = "Select ItemCode From OITM"
        Dim result As New DataSet()
    
        Using connection As New SqlConnection(ConnectionString), _
              command As New SqlCommand(sql, connection), _
              adapter As New SqlDataAdapter(command)
    
            adapter.Fill(result)
        End Using
    End Sub
    

    【讨论】:

    • 我刚刚尝试了您的第一个代码。它非常适合我。为什么建议不要使用 SA 帐户?
    猜你喜欢
    • 2016-08-13
    • 2016-10-10
    • 2016-01-14
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    相关资源
    最近更新 更多