vb2010如何用DataGridView显示access数据库

vb2010如何用DataGridView显示access数据库,第1张

首先使用vb创建一个Windows

Form

Application,然后拖拽一个DataGridView在上面: 点击图右上角的小箭头,出现向导,选择Access数据源: 点击下一步: 点击下一步: 点击红色部分的按钮,d出下面对话框,选择红色部分: d出对话框,选择红色部分,单击OK: d出Access连接对话框,然后点击下图红色部分选择数据库路径(如果有密码请填入密码,在下面文本框内): 最后一路“下一步”或者“保存”,运行程序即可。

Imports System.Data.SqlClient

Imports System.Data

Imports System.Data.OleDb

Public Class Form1

Dim odCnn As OleDbConnection

Dim odApter As OleDbDataAdapter

Dim dataPath As String

Dim passWord As String

Dim cnnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0Data source="

Dim selectTabel As String

Dim sqlStr As String

Dim dataSet As DataSet

Dim dataTable As DataTable

Dim openFileDlg As OpenFileDialog

Dim dataGridView As DataGridView

Dim CB As OleDbCommandBuilder

Dim myTable As DataTable

Function dlg(ByVal index As Integer) As String

openFileDlg = New OpenFileDialog

With openFileDlg

.Title = "张志晨数据库程序V2012"

.Filter = "Aaccess07-10数据库|*.accdb|access97-03数据库|*.mdb| 所有类型|*.*"

.FilterIndex = index

.ShowDialog()

Return .FileName

End With

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label1.Text = dlg(1)

dataPath = Label1.Text

passWord = ""

cnnStr = cnnStr &dataPath &"JET OLEDB:Database Password=" &passWord

odCnn = New OleDbConnection(cnnStr)

odCnn.Open()

getTableName()

odApter = New OleDbDataAdapter

End Sub

Function getTableName()

' 获取数据表列表

'Type 有:"TABLE,VIEW,ACCESS TABLE,SYSTEM TABLE",

Dim dt As DataTable

Dim i As Integer

dt = odCnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _

New Object() {Nothing, Nothing, Nothing, "TABLE"})

'DataGridView2.DataSource = dt '.......显示表的所有信息

For i = 0 To dt.Rows.Count - 1 '记录数,也就是行数

ComboBox1.Items.Add(dt.Rows(i).Item("TABLE_NAME"))

'显示字段名为 TABLE_NAME 列的行值 就是用户创建的表的名

Next

ComboBox1.Text = ComboBox1.Items(0) '设置第一个表为列表框的值,也就是显示第一个表

MsgBox("数据库中共有" &dt.Rows.Count &"个表")

End Function

Function fillData()

Dim sql As String = "select * from " &selectTabel '建立查询 字符型

''''请注意这里:Dim sql As String = "select * from MyTanle When [年龄]>45 And [学历]=大本"

odApter = New OleDb.OleDbDataAdapter(sql, odCnn) '桥接器

dataSet = New DataSet '内存中的虚拟数据库实例

odApter.Fill(dataSet, "table") '上面的实例被填充

dataGridView.DataSource = dataSet.Tables("table")

dataGridView.AutoResizeColumns()

dataGridView.AutoResizeRows()

'myTable = New DataTable

'odApter.Fill(myTable)

'dataGridView.DataSource = myTable

'获取字段名,并填充列表

getField()

CB = New OleDb.OleDbCommandBuilder(odApter) '用于自动记录你对数据的更改

If CB Is Nothing Then

CB.DataAdapter = New OleDb.OleDbDataAdapter

odApter.DeleteCommand = CB.DataAdapter.DeleteCommand

odApter.InsertCommand = CB.DataAdapter.InsertCommand

odApter.UpdateCommand = CB.DataAdapter.UpdateCommand

End If

'MsgBox("共有记录条数:" &dataSet.Tables("table").Rows.Count, vbOKOnly, "打开成功")

End Function

Function getField()

For i As Integer = 0 To dataSet.Tables("table").Columns.Count - 1

ComboBox2.Items.Add(dataSet.Tables("table").Columns(i).Caption)

Next

End Function

'创建一个网格

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dataGridView = New DataGridView

Me.Controls.Add(dataGridView)

With dataGridView

.Top = 60

.Left = 0

.Width = Me.Width - 20

.Height = Me.Height - 40 - 60

.Visible = True

End With

End Sub

'打开指定的表

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

selectTabel = ComboBox1.Text

fillData()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

save_sub()

End Sub

'保存数据

Function save_sub()

Dim temptb As DataTable

temptb = dataGridView.DataSource.GetChanges() '获取更改信息

If temptb IsNot Nothing Then '如果有更改

odApter.Update(dataGridView.DataSource) '就保存更改,在实质上是把网格中的数据回发给数据库

MsgBox("您的更改已经回发到数据库!", , "通用数据库程序")

Else

MsgBox("您没有更改任何数据!", , "通用数据库程序")

Exit Function

End If

End Function

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim sr As New DataView

sr = dataSet.Tables("table").DefaultView

sr.RowFilter = ComboBox2.Text &"='" &TextBox1.Text &"'" '这是另一种自定义查询

ListBox1.DataSource = sr

End Sub

End Class


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9965741.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存