Re: OleDbconnection syntax

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: William Ryan eMVP (dotnetguru_at_comcast.nospam.net)
Date: 05/26/04


Date: Wed, 26 May 2004 12:33:38 -0400

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
"JH" <yhcontact@yahoo.com> wrote in message
news:upWtuVzQEHA.2876@TK2MSFTNGP09.phx.gbl...
> 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")
>
>


Relevant Pages

  • Re: local area connections
    ... while all kinds of things showed in the device manager ... only thing that has turned up any reference to ... connections that are not there. ... >> dir that the referrences to the numbers were in. ...
    (microsoft.public.windowsxp.network_web)
  • Re: SSIS An error occurred while attempting to perform a type cast
    ... 2005 Microsoft Corporation; All Rights Reserved; ... AssemblyName = "System.Data" ...
    (microsoft.public.sqlserver.dts)
  • Re: The Measurement of Contraction
    ... physical space at a given time in the observer's frame of reference .. ... you have no arguments only assertions: Yes it does, ... defining the theory and defending it with even more ambiguity ...
    (sci.physics.relativity)
  • Re: Sharing Knife: Lakewalker Society questions
    ... There's certainly no reference to even the ... existence of a Europe-equivalent in the books, ... Dag asserts that no sailors have visited other continents at least since ... the Malice Wars, though they are known to have existed then, and been ...
    (rec.arts.sf.written)
  • Re: Let us be sure that we know what the problem is!
    ... Unlike Einstein and his cult followers, ... presuppose the existence of an absolute frame of reference. ...
    (sci.physics.relativity)