在ado.net中使用ado recordset

来源:互联网 发布:用友oa软件 编辑:程序博客网 时间:2024/05/01 12:57
在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怎么样试一试看看!

<BASE onmouseover="window.status='正文--在ado.net中使用adorecordset';return true">
原创粉丝点击