连接ACCESS数据库

Anyway, to the heart of the matter. Firstly, I have to make the assumption that your students didn't mess with security and so the databases are still part of the default workgroup. The location of the default workgroup should be:

C:/Documents And Settings/<User Name>/Application Data/Microsoft/Access/System.mdw

I need to make this assumption because for the permission setting to work (I have found, might not be true for everyone but it's something I've seen in my experience) you need to add the workgroup file (a .mdw file) to the connection string otherwise an exception stating that the workgroup file cannot be opened will be shown.

Now, I'm not sure whether you will actually need to loop through every DB for this - it may be the case that modifying the workgroup once will apply the permission changes to all databases that are part of that workgroup (once again, a reason for the assumption I made previously).

Anyway, just try and execute the code once and see if you can access MSysObjects for all your databases. If you find that you can't access MSysObjects for a given database, then that would be an indication that you need to loop through each database. Anyway:

No Looping (Maybe)
================

Dim conn As System.Data.OleDb.OleDbConnection
Dim cmdToExecute As System.Data.OleDb.OleDbCommand

'NB. Replace the data source with the one for your Access DB and replace
'the Jet OLEDB:System Database with the path to your System.mdw

conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/XXXXX/My Documents/db3.mdb;" & _

            "Jet OLEDB:System Database=C:/Documents and Settings/XXXXXX/Application Data//Microsoft/Access/System.MDW")

cmdToExecute = New System.Data.OleDb.OleDbCommand()
cmdToExecute.Connection = conn
cmdToExecute.CommandType = CommandType.Text
cmdToExecute.CommandText = "GRANT SELECT ON TABLE MSysObjects TO PUBLIC"

conn.Open()
Try
    cmdToExecute.ExecuteNonQuery()
Catch ex As Exception
    MsgBox(ex.Message)

End Try

conn.Close()

With Looping
==========
Just execute the code I showed before....All you need to do is modify which .mdb file you'll be looking at. I'm sure there's code around to let you iterate through a directory and access all the .mdb files.

Connecting In The Future
===================
When you connect to the database in the future, be sure to use the connection string I showed you in the previous code (i.e. keep the Jet OLEDB:System Databases parameter in there) so it looks like:

conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/XXXXX/My Documents/db3.mdb;" & _

            "Jet OLEDB:System Database=C:/Documents and Settings/XXXXXX/Application Data//Microsoft/Access/System3.MDW")

I think that's about it. Hope that helps a bit, but sorry if it doesn't

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值