switching ConnectionString at runtime

T

TeeCo

Hi folks. I'm trying to change the location of the Access mdb file I
connect to using OleDb and am having trouble. I'm using Visual C# 2005
and the default values I use for the ConnectionString are those
generated by the Server Explorer. I can connect just fine using the
default values.

But when I comment out strDefaultConnectionFile and uncomment the
dbName variable I get:
'OleDbException: Could not use "; file already in use.' During testing
I select the exact same path and file that are used in the default, and
I have confirmed this by copying both 'runtime' strings to a text file
and comparing them.

So the two strings are identical. But the 2nd fails. Of course, I
close the debugger when switching between strings and have confirmed
that there are no locks on the database. And access permissions are
the same for the mdb in both cases. I am baffled!

Code fragments are below. Any help would be appreciated.

//Three strings defined earlier in the code...
strDefaultConnectionPre =
@"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source=";

strDefaultConnectionFile = @"C:\Program Files\HID\Config
Card Production Utility\ConfigCard.mdb";

strDefaultConnectionPost = @";Mode=ReadWrite|Share Deny
None;Extended Properties="""";Jet OLEDB:System database="""";Jet
OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet
OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global
Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New
Database Password="""";Jet OLEDB:Create System Database=False;Jet
OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on
Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet
OLEDB:SFP=False";

//Code fragment of a method called to define the data
source at runtime...

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((testStream = openFileDialog1.OpenFile()) == null)
{
testStream.Close();
return false;
}
//Install the new path and test it.
dbName = openFileDialog1.FileName;
oleDbConnection1.ConnectionString =
strDefaultConnectionPre +
// @dbName +


strDefaultConnectionFile +

strDefaultConnectionPost;
if (!TestConnection())
{
return false;
}
}
else
return false;
 
T

TeeCo

In case any one is interested, I found the error.

You will note I tested the result of the users file selection by trying
to open the selected file. I neglected to close the file stream on the
success branch. That resulted in the file being open when I later
tried to connect to the database. Once I closed the filestream
everything worked fine. Thanks for the help...
 

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