views:

733

answers:

3

Hi Guys.I am trying to connect to a Sql Server in my apllication using the following code :

SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Data Source = (local);Initial Catalag = Inventory;
                                 Persist Security Info = false;
                                 Integrated Security = true;
                                 Packet Size = 4096;Connect TimeOut = 30";
myConnection.Open();

where Inventory is my database and it exists on my local machine.

"Error 40: Could not open a Connection to Sql Server" ---error comes while the above code executes.

A: 

If you do not specify the Network Library in the connection string a connection to the database will be established using TCP/IP, by default.

The TCP/IP network library is usually not enabled after installation of SQL Server. Use the SQL Server Configuration Manager to enable to protocol.

Alternatively, if you want to use an enabled protocol, perhaps Shared Memory, add Network Library=dbmslpcn to the connection string.

Ajw
+4  A: 

Its Initial Catalog and not Initial Catalag.

You may want to test with this changed.

Nahom Tijnam
A: 

Do you have more than 1 instance of SQL Server in your pc? Then Data Source = (local) is ambiguous.

If this is the case, then try out with Data Source = MyServername\MyInstance in the connection string.

Nahom Tijnam
I tested this case in my dev box having multiple sql server instances (sql 2000, sql 2005) and got the same error. The error was gone when I replaced (local) with <MyServername><MyInstance> in teh connection string.
Nahom Tijnam
@Down voter - Could you please comment why this was down voted?
Nahom Tijnam