将原生的ADO 记录集绑定到MSGRID上
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Button1 = New System.Windows.Forms.Button
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.Button2 = New System.Windows.Forms.Button
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(16, 48)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(560, 280)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(368, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(208, 32)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Fill Grid"
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation id=HUOER;packet size=4096;user id=sa;data source=""LIZ-SERVER"";persist" & _
" security info=False;initial catalog=wen"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(144, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(200, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "使用原生ADO对象"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(584, 333)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private sqlCOnn As String = "user id=sa;data source=LIZ-SERVER;persist security info=False;initial catalog=wen"
Private NsqlCOnn As String = "Provider=SQLOLEDB.1;user id=sa;data source=LIZ-SERVER;persist security info=False;initial catalog=wen"
Public Function GetRs(ByVal Sql As String) As ADODB.Recordset
Dim conn As New ADODB.Connection
Dim comm As New ADODB.Command
conn.ConnectionString = NsqlCOnn
conn.Open()
comm.ActiveConnection = conn
comm.CommandText = Sql
Return comm.Execute
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection(sqlCOnn)
conn.Open()
MsgBox(conn.State)
Dim ad As New SqlCommand("select * from product", conn)
Dim rs As New SqlClient.SqlDataAdapter(ad)
Dim tab As New DataSet
rs.Fill(tab, "test")
Me.DataGrid1.DataSource = tab
End Sub
Private Sub SqlConnection1_InfoMessage(ByVal sender As System.Object, ByVal e As System.Data.SqlClient.SqlInfoMessageEventArgs) Handles SqlConnection1.InfoMessage
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rs_YPDZT As New ADODB.Recordset
rs_YPDZT = GetRs("select * from book")
Dim ds_YPDZT As New Data.DataSet
Dim da_YPDZT As New Data.OleDb.OleDbDataAdapter
da_YPDZT.Fill(ds_YPDZT, rs_YPDZT, "YPB")
Me.DataGrid1.DataSource = ds_YPDZT
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection(sqlCOnn)
conn.Open()
MsgBox(conn.State)
Dim ad As New SqlCommand("select * from product", conn)
Dim rs As New SqlClient.SqlDataAdapter(ad)
Dim tab As New DataSet
rs.Fill(tab, "test")
Me.DataGrid1.DataSource = tab
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rs_YPDZT As New ADODB.Recordset
rs_YPDZT = GetRs("select * from book")
Dim ds_YPDZT As New Data.DataSet
Dim da_YPDZT As New Data.OleDb.OleDbDataAdapter
da_YPDZT.Fill(ds_YPDZT, rs_YPDZT, "YPB")
Me.DataGrid1.DataSource = ds_YPDZT
End Sub
End Class