471,627 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

switching ConnectionString at runtime

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="""";U ser 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;

Feb 13 '06 #1
1 1793
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...

Feb 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jeremy Holt | last post: by
2 posts views Thread by AllenL | last post: by
7 posts views Thread by hung tran | last post: by
1 post views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.