esacape sequence

A

abcd

case 1
----------

I have a variable called sConnectString whose value is

"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\\msxp102\shared\DBS\db3.mdb;User Id=;Password=;";

after this I call following statements

sConnectString = sConnectString.Replace("\\",\\\\);

OleDbConnection myConnection = new OleDbConnection(sConnectString );

myConnection.Open();

I get error file not found.


Case 2
-----------

Where as if I do follwoing thing it works

ss = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\\\\msxp102\\shared\\DBS\\db3.mdb;User Id=;Password=;";

OleDbConnection myConnection = new OleDbConnection(sConnectString );

myConnection.Open();

I dont want to hard code the connection string so case 1 must run....

any thoughts
 
A

abcd

my case 2 syntax is as below

Where as if I do follwoing thing it works

ss = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\\\\msxp102\\shared\\DBS\\db3.mdb;User Id=;Password=;";

OleDbConnection myConnection = new OleDbConnection(ss);

this works...I am curious why case 1 does not work
 
N

Nathan Sokalski

Based on the difference between your Case 1 and Case 2, it looks like the
reason is that when you use the Replace method, you are only fixing the
problem for the backslashes at the beginning of the Source. Instead of
replacing "\\" with \\\\, try replacing "\" with \\ using Replace("\",\\).
This will fix all the backslashes in your connection string. Good luck.
 
J

jasonkester

Yikes!

Try using the @"" style declaration for your strings to avoid some of
this. As in:

string sConnectString = @"Provider=Microsoft.Jet.OLEDB.­4.0;Data
Source=\\msxp102\shared\DBS\db­3.mdb;User Id=;Password=;";

That will preserve the backslashes, and not attempt to use them to
initiate escape sequences. As a worst case, at least your replacement
code would be a touch more readable:

sConnectString = sConnectString.Replace(@"\",@"­\\");


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

abcd

Really frustrating now.

I tried this

sConnectString = sConnectString.Replace(@"\",@"\\");

this fails..

Whereas if I hardcode the same string as

ss= "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=\\msxp102\shared\DBS\db3.mdb;User Id=;Password=;";

OleDbConnection myConnection = new OleDbConnection(ss);
myConnection.Open();

Connection opens..

I checked sConnectString and ss are exactly same. The only difference is
sConnectString is created dynamically by getting some variables whereas ss
is hardcoded..
 
J

jasonkester

You've lost me. Why exactly are you attempting to replace backslashes
with doubled backslashes?

If you always use @"string" syntax, you won't need to deal with
escaping your slashes. You can concatonate away to your heart's
delight, so long as every element of your string is declared as
@"my\path" rather than "my\path".

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

abcd

I have variable which value is that long connection string ....

How can I put @ sign for that variable
 
A

abcd

Jason pls ignore my earlier email. You are right. I just put @ in front of
my connection string variable and its working

OleDbConnection myConnection = new OleDbConnection(@sConnectString);
myConnection.Open();

thanks
 

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