在ado.net中使用ado recordset
发表于:2007-06-30来源:作者:点击数:
标签:
在ado .net 中我们很少甚至不用recordset今天我就向大家展示如何在ado.net中使用recordset 新建一个 vb .net应用程序 在窗体上添加一个datagrid控件 然后,项目--添加引用--〉com- 选择Microsoft ActiveX Data Objects 2.7 Library 这样在引用一栏我们就看到
在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.De
buggerStepThrough()> 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怎么样试一试看看!
原文转自:http://www.ltesting.net