首页 > 编程 > .NET > 正文

在ado.net中使用ado recordset

2024-07-10 13:05:27
字体:
来源:转载
供稿:网友
在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怎么样试一试看看!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表