Fixing Instance Failure when connecting to SQL Server 2005 Express
Sponsored By
I was getting Instance Failure when connecting to my SQL Server Express 2005 Database in my C# ASP.NET application. Very weird.
<connectionStrings> <add name="NorthwindConnString"
connectionString="Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient"/> </connectionStrings>
Trial and error just taught me that the problem was "Data Source=.\\SQLEXPRESS;" - It's the double \\. That's an escape sequence in C#. Everything worked when I switched the connection string to .\SQLEXPRESS.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
August 31, 2007 6:34
That was probably the cause of the problem, but I'm not sure it's related to \\ being an escape sequence in C#. If it were being taken as C# code, wouldn't ".\\SQLEXPRESS" translate to ".\SQLEXPRESS" and ".\SQLEXPRESS" translate to ".SQLEXPRESS"? I don't think C# escape sequences apply to strings in a configuration file.
Jose, I'm starting to think you're right, but interesting it works with \\ under VB...Hm...what is going on?
The config file is an XML file which has nothing to do with C#. Thus in XML the attribute connection string will still be .\\SQLEXPRESS and is invalid.
What happens is : the string is read in c# and is escaped so \\ becomes \\\\. And I know that the hard way :((.
Took 2 hours once to find the stupid thing.
Took 2 hours once to find the stupid thing.
Brett: Ah, so you're saying it was always wrong?
Sirrocco: See, that makes more sense to me.
Sirrocco: See, that makes more sense to me.
Yes, in a .config file or XML file it is always wrong and as Sirrocco said the .\\SQLEXPRESS would be read in as .\\\\SQLEXPRESS. However, if you had hard coded the connection string into C# code, in order for C# to interpret it, you would have had to do some thing like:
string connectionString = "Data Sorce=.\\SQLExpress";
What I am basically getting to, the config file (aka XML) is read in using an XML parser where \ is not a escape character thus will be treated litterally. i.e. \ = \.
In the C# source code (note I said source code) above, the C# parser will treat \ as an escape character, thus \\ is read in as \.
To answer your question, yes for a config file it is always wrong as it is XML. However for C# source code it is correct.
string connectionString = "Data Sorce=.\\SQLExpress";
What I am basically getting to, the config file (aka XML) is read in using an XML parser where \ is not a escape character thus will be treated litterally. i.e. \ = \.
In the C# source code (note I said source code) above, the C# parser will treat \ as an escape character, thus \\ is read in as \.
To answer your question, yes for a config file it is always wrong as it is XML. However for C# source code it is correct.
It alos seems like the ADO.NET API could give you a better exception message than "instance failure".
Thank you! You saved me from a bad headache. :) I had copy-pasted the code from a C# area of my program and had forgotten that the double backslash was for the benefit of C#'s string escapes and that XML didn't need it. Thank you!!!
Utah Custom Software Consulting
Utah Custom Software Consulting
Comments are closed.