调用Excel数据库的实例
分类:VB.Net
乐博网lob.cn提示:调试环境为 vs2005 + windows2003 / windows2008 / xp / vista + .NET Framework 2.0
代码如下:
Option Strict On Imports System.Data.OleDb
Public Class LOB
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click ' 显示一个状态讯息对话框来表示我们目前要尝试连结至 SQL Server。 Dim frmStatusMessage As New frmStatus
frmStatusMessage.Show("乐博网提示:正在连接到Excel工作薄....")
Try ' 利用 OleDbConnectionStringBuilder 对象来构建连接字符串。 Dim connectStringBuilder As New OleDbConnectionStringBuilder() connectStringBuilder.DataSource = "d:\乐博网\资料.xls" 'connectStringBuilder.DataSource = "d:\乐博网\Book1.xls" connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0" connectStringBuilder.Add("Extended Properties", "Excel 8.0")
Using cn As New OleDbConnection(connectStringBuilder.ConnectionString) cn.Open()
Dim ds As New DataSet()
Dim cmdLiming As New OleDbCommand("SELECT * FROM [金额$]", cn)
Using drLiming As OleDbDataReader = cmdLiming.ExecuteReader()
ds.Load(drLiming, LoadOption.OverwriteChanges, New String() {"金额"})
' 将 BindingSource 组件绑定到 DataSet 当中的“金额”数据表。 Me.BindingSource1.DataSource = ds.Tables("金额")
' 将 DataGridView 控件绑定到 BindingSource 组件。 Me.DataGridView1.DataSource = Me.BindingSource1
End Using End Using Catch ex As Exception MessageBox.Show(ex.Message, "乐博网提示", MessageBoxButtons.OK, MessageBoxIcon.Stop) End Try frmStatusMessage.Close() End Sub
Private Sub LOB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub End Class
|