在ado.net中使用ado recordset

來源:互聯網
上載者:User
ado 在ado.net中我們很少甚至不用recordset今天我就向大家展示如何在ado.net中使用recordset
建立一個vb.net應用程式
在表單上添加一個datagrid控制項
然後,項目-->添加引用--〉com->
選擇Microsoft ActiveX Data Objects 2.7 Library
這樣在引用一欄我們就看到新加入一項ADODB
我們將其與其他命名空間一起倒入
Imports System.Data
Imports System.Data.OleDb
Imports ADODB
整個代碼如下(使用中文officeXp案例資料庫-因為是中文所以表明欄位名都是中文,你也可以利用其他的資料庫)
Imports System.Data
Imports System.Data.OleDb
Imports ADODB

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 表單設計器產生的程式碼 "

    Public Sub New()
        MyBase.New()

        '該調用是 Windows 表單設計器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 調用之後添加任何初始化

    End Sub

    '表單重寫處置以清理組件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 表單設計器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下過程是 Windows 表單設計器所必需的
    '可以使用 Windows 表單設計器修改此過程。
    '不要使用代碼編輯器修改它。
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(536, 320)
        Me.DataGrid1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(552, 333)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create a connection string
        Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\Microsoft Office\\Office10\\Samples\\Northwind.mdb"
        Dim sql As String = "SELECT 產品ID, 產品名稱, 單價 From 產品"
        ' Create a Connection object and open it
        Dim conn As Connection = New Connection()
        Dim connMode As Integer = ConnectModeEnum.adModeUnknown
        conn.CursorLocation = CursorLocationEnum.adUseServer
        conn.Open(ConnectionString, "", "", connMode)
        Dim recAffected As Object
        Dim cmdType As Integer = CommandTypeEnum.adCmdText
        Dim rs As _Recordset = conn.Execute(sql)

        ' Create dataset and data adpater objects
        Dim ds As DataSet = New DataSet("Recordset")
        Dim da As OleDbDataAdapter = New OleDbDataAdapter()

        ' Call data adapter's Fill method to fill data from ADO
        ' Recordset to the dataset

        da.Fill(ds, rs, "Customers")

        ' Now use dataset
        DataGrid1.DataSource = ds.DefaultViewManager

    End Sub
End Class
OK怎麼樣試一試看看!

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。