数据库信息读入到内存数组中

类别:.NET开发 点击:0 评论:0 推荐:

       通常情况下,如果将数据读入到简单的内存数组中,那么处理少量数据会更容易些。以下方法提供了ADO.NET对象从数据库加载信息到数组,而且该数组可任意自行扩展。

在默认窗体的头部输入如下代码:
    Imports System.Data.OleDb
在按钮的Click事件中:
   
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myConn As OleDbConnection
        Dim myCmd As OleDbCommand
        Dim myReader As OleDbDataReader
        Dim myConnStr As String
        Dim myArray(0) As String
        Dim i As Integer

        myConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=JLGL.mdb"
        myConn = New OleDbConnection(myConnStr)
        myCmd = New OleDbCommand()
        myCmd.CommandType = CommandType.Text
        myCmd.CommandText = "select * from JXB"
        myCmd.Connection = myConn
        myConn.Open()
        myReader = myCmd.ExecuteReader
        Do While myReader.Read
            myArray(i) = myReader!出厂编号
            TextBox1.Text = TextBox1.Text & myArray(i) & vbCrLf
            ReDim Preserve myArray(UBound(myArray) + 1)
            i += 1
        Loop
        Label1.Text = i   'i为记录条数

        myConn.Close()

    End Sub

本文地址:http://com.8s8s.com/it/it43622.htm