VB.Net学习笔记,使用ADO.Net对象访问数据库,将结果写入ListView

80酷酷网    80kuku.com

  ado|笔记|对象|访问|数据|数据库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 Button1 As System.Windows.Forms.Button
Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
Friend WithEvents OleDbCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents ListView1 As System.Windows.Forms.ListView

Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection()
Me.OleDbCommand1 = New System.Data.OleDb.OleDbCommand()
Me.ListView1 = New System.Windows.Forms.ListView()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(296, 256)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "读取(&R)"
'
'ListView1
'
Me.ListView1.Location = New System.Drawing.Point(8, 8)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(384, 224)
Me.ListView1.TabIndex = 2
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(408, 293)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListView1, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

'以上都不用看,是Vs.Net生成的,下面是我写的代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DataReader As System.Data.OleDb.OleDbDataReader '定义DataReader对象
Dim i, j, FieldCount As Integer
Dim TempItem As ListViewItem '定义临时ListViewItem对象
OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\Inetpub\wwwroot\MYDB.MDB" '设置连接字符串
OleDbConnection1.Open()
OleDbCommand1.CommandText = "Select * from mytable" '设置命令字符串
OleDbCommand1.Connection = OleDbConnection1
DataReader = OleDbCommand1.ExecuteReader '执行SQL语句,返回给DataReader对象
FieldCount = DataReader.FieldCount
ListView1.FullRowSelect = True
ListView1.CheckBoxes = True
For i = 0 To FieldCount - 1 '枚举DataReader中字段名,添加Columns
ListView1.View = View.Details
ListView1.Columns.Add(DataReader.GetName(i), 80, HorizontalAlignment.Left)
Next
While DataReader.Read() '获取记录信息,写入ListView
TempItem = ListView1.Items.Add(DataReader.GetValue(0))
For j = 1 To FieldCount - 1
TempItem.SubItems.Add(DataReader.GetValue(j) & "")
Next
End While
DataReader.Close()
OleDbConnection1.Close()
End Sub
End Class



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: