Wednesday, March 28, 2012

Problems with SQL Server connection string

I am slightly confused about SQL Server connections strings. When I used the following connection string:

string strConn = "server=decius\awa;database=xxxx;uid=xxxx;pwd=xxxx";

I get an error saying "Format of the initialization string does not conform to specification starting at index 13. "

I spent ages trying to figure out why this was and I eventually discovered that adding an "@." symbol before the string fixed it, i.e.

string strConn = @."server=decius\awa;database=xxxx;uid=xxxx;pwd=xxxx";

Most of the examples I have seen to not use the "@." symbol so I am just wondering what is does and when you need to use it?

Thanks,

Mark

The @. fixed your problem because it designates a literal string. Basically, when the processor sees @. before the string it knows to ignore all special characters within that string that would otherwise need to be used with an escape character. In C#, the '\' is an escape character that means 'do not read the following character as a special character. It would be used like so:

string path = "files\\filename.doc";

That will read to the processor as 'files\filename.doc";

As you've found out, you can also use the @. for the same result. These next 2 strings will have the same output:

string path = "files\\filename.doc";

string path = @."files\filename.doc";

...Hope that helps.

|||

Thanks Stew, that's great.

Mark

sql

No comments:

Post a Comment