连接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

你可能感兴趣的:(exception,String,File,database,System,Access)