VB.NET 代码 对ACCESS的只读访问
时间: 2023-06-19 10:08:04 浏览: 77
以下是VB.NET代码示例,用于在ACCESS数据库中进行只读访问:
```
Imports System.Data.OleDb
Public Class Form1
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Mode=Read;"
Dim conn As New OleDbConnection(connString)
Dim cmd As New OleDbCommand("SELECT * FROM myTable", conn)
Try
conn.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader()
While reader.Read()
' Do something with the data '
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try
End Sub
End Class
```
请注意,此示例使用Microsoft.ACE.OLEDB.12.0提供程序,因此你需要安装Microsoft Access数据库引擎才能使用它。如果你使用的是较旧的Access版本,则需要更改提供程序的名称。此外,将“Mode”属性设置为“只读”可确保不会对数据库进行任何更改。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)