Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a c# application with sql server 2008 database in windows authentication mode. when i run it in my system it works well.
But when i share c# app in network, I can not connect to database and i get error( a network related or instance...).
this is my connection string:
C#
conn = new SqlConnection(@"Server=localhost;Database=OfficialAutomation;Integrated Security=true;");

What should i do?
Thanks in advanced.
Posted

Try replacing "localhost" with the IP address of the machine:
C#
conn = new SqlConnection(@"Server=XXX.XXX.XXX.XXX;Database=OfficialAutomation;Integrated Security=true;");

I assume that the user that is logged in can access the machine on the network?

Otherwise, you might want to switch to SQL Server authentication...
 
Share this answer
 
v5
Comments
MJ MOUSAVI 20-May-13 8:21am    
I change server name to ip address but i didn,t get any answer.
then i switch to sql server authentication and enter with sa.
but in other machine again i,v got error.
Johnny J. 20-May-13 8:32am    
Sounds like a network problem. Can you ping the address of the the DB server machine?
MJ MOUSAVI 20-May-13 8:53am    
yes, when i ping the address,i get answer.
Server=localhost says that the SQL instance is located on the current PC - which it isn't, it is located on your development machine (which is why it worked when you wrote it). There are two things you can do about this:
1) Change the connection string to refer to the absolute address of your developemnt machine. This is relatively trivial:
Set up a connection in VS with the Server Explorer pane:
1.1) Open Server Explorer.
1.2) Right click "Data connections" and select "Add connection"
1.3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
1.4) When the connection works, press "OK"
1.5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file. This will be absolute "pc/instance" rather than "localhost" related.
Or
2) Set up a second machine as an SQL server for production, and use the absolute address of that instance instead.

Personally, I would use the second - that way when you continue work on the application, you will not accidentally affect the production database. Image the mess you could make if you forget to add the WHERE clause to an UPDATE or DELETE statement!

BTW: You should also take the time to move your connection string from a hardcoded value as you show in the question to a configuration file (of some description) based setting, so that you can switch your PC between production and development SQL instances without changing the software - it makes it more reliable, and helps to ensure that when you upgrade their software, the users don't switch back to the development server and mess everything up! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900