OleDbconnection syntax

J

JH

Is this syntax correct? I have problems connecting to the database
Dim myConnection As OleDbConnection

Dim directoryName As String

directoryName =
Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)

myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source='" & directoryName & "'\data\test.mdb")
 
W

William Ryan eMVP

For future reference, http://www.connectionstrings.com/ may be of help but
i'd check the Path variable. First.

Debug.Assert(File.Exists(directoryName & "\data\test.mdb")

As a rule of thumb I'd verify the existence of the file before trying to
reference it (take out the DebugAssert and use If File.Exists(directoryName
& "\data\test.mdb") Then
'Connect here.
End If

I'd also wrap the Open statement and/or And DataAdapter.Methods that involve
connections (Update/Fill) in try catch and catch OleDbException in this case
(SqlException for sqlserver, Oracle for oracle etc) since many things can go
wrong with connections other than the string.

If the assertion above fails it means it isn't seeing the db file and I
think that's the problem.

Try New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\'" & directoryName & "'\data\test.mdb"

Depending on permissions you may want to add the Account info User
Id=admin;Password=; at the end after the mdb ,
New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\'" & directoryName & "'\data\test.mdb;User Id= admin;Password=;"

You'll see the other forms at connection strings like I mentioned but
depending on where it's located, it may be that path reference. And
although my suggestions about the assertions (for debug purposes) , try
catches etc aren't specifically related, it may Tell you there's a problem
beforehand and if the file doesn't exist, you'll know immediately with the
If and using Assertions. Also, if you use the If to check the existence,
you'll probably save yourself at least one exception so it will have side
benefits as well .

HTH,

Bill
--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top