Thursday, July 12, 2007

SQL Server connection error[40] doesn't allow remote access

Today I was learning how to use SQL Databases and came upon a really stupid problem. I am trying to connect to a database on the computer im using, and this error pops up. Im not trying to connect to a remote computer, so why the hell is it giving me this error?

Heres some solutions I found while googling my way to success. Solution 1 allows a client to connect to the server computer. Solution 2 is used for the same reason. Solution 3 however solved my problem, when the error comes up using your computer as the server.

Mandatory code for testing
Using C# code under an event handling button type this:
First create a button and a label.
SqlConnection con = new SqlConnection(@"Server=localhost\SQLExpress;Initial Catalog=;Integration Security=true");
con.Open( );
label1.Text = "This connection worked";
con.Close( );

After each solution Start Debugging to test your connection. If your connection will actually work the label1 text will appear. Otherwise it will take about 10 seconds to tell you an error has occurred.

Solution 1: Enable things
1. Start->Run->Type "cliconfg.exe" -> Ok
2. Click TCP/IP -> Enabled
3. Click Named Pipes -> Enabled
4. Apply and leave
5. Debug program

Solution 2: Allow a port to be unblocked
1. Start->Run->Type "firewall.cpl"->Ok
2. Exceptions -> Add Port...
3. Add port "1433"
4. Check TCP -> Ok
5. Debug program

Solution 3: If you are getting this problem on a local host
1. Download
2. Its used to manage SQL databases
3. Create a database on that tool
4. Open Visual Studio 2005
5. View->Server Explorer
6. Click Connect to Database
7. Datasource = "Microsoft SQL Server (SqlClient)"
8. Server name= /SQLExpress
9. Check Windows Authentication
10. Select "Select or Enter a Database name"
11. On the list, your database you created with the posted tool will come up
12. Test Connection
13. Ok
14. Debug program

1 comment:

There was an error in this gadget