在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怎么樣試一試看看!