Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error message for connection string

Status
Not open for further replies.

sirnose1

Programmer
Nov 1, 2005
133
US
I am trying to connect to an SQL server 2008 express database with C#. The program compiles with no problems, however when I try to run it, I get the exception: "Format of the initialization string does not conform to specification starting at index 51."

Here is the connection string:

string cnString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\myWork\appLithium\appLithium\\dbAppChem.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(cnString);

Format of the initialization string does not conform to specification starting at index 51.
 
chances are [tt]appLithium\appLithium[/tt] is the problem, should be [tt]appLithium\\appLithium[/tt].
however, creating the connection string in code is a very bad practice.
1. you end up repeating yourself anytime you connect to the database.
2. you cannot reconfigure the connection without a compile & deploy
3. if you repeat the connection string that much, you are not managing the db connections very well either.

to remedy this place the connection string in the ConnectionStrings node of the app.config file. you can then reference the connection string using
Code:
var settings = ConfigurationManager.ConnectionStrings["key"];
var connectionString = settings.ConnectionString;
this will eliminate the problem 1 and 2 above. there is still the issue of connection managment. see my faq below for some ideas. it's more difficult to determine where the boundaries of the unit of work lie in a desktop application, but once you define them you can better manage the connections.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top