Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Buddies:

I have a sql db with its application. I bring this program and the associated sql database to another computer. How can I get the "server" and "data source" of new computer for string connection in sql connection? I should mention that I do not have any info. about the guest computer and assume that the guest computer does not have any sql managemnt system, so my application should obtain the "server" and "data source" to work properly.


Yours
Posted
Updated 23-May-12 10:40am
v4

Visit Connection strings for SQL Server 2008[^]

Also one nice way to resolve server name and instance is to open Sql Server Management Studio, connect to the database and then in the properties box, you should see the server name and instance.

Addition:

Example of current connection string to a local Sql Server database:
Server=(local)\MyInstance;Database=TestDatabase;Trusted_Connection=True;


Example of a connection string that clients use to connect to a single Sql Server database:
Server=ServerXYZ\TheInstanceName;Database=ProductionDatabase;Trusted_Connection=True;
 
Share this answer
 
v2
Comments
Mmohmmad 23-May-12 17:04pm    
Dear Mika:

I should mention that I do not have any info. about the guest computer and assume that the guest computer does not have any sql managemnt system, so my application should obtain the "server" and "data source" to work properly.
Wendelius 23-May-12 17:13pm    
That's quite alright.

The connection string is just a string that you define with your SqlConnection. You can store the string inside the application or put it in a configuration file and read it from the etc.

The necessary client libraries to connect to remote Sql Server are included in the .Net framework installation so you doon't have to worry about those.

The reason I mentioned Management Studio was that sometimes people don't know the name or the IP address or the instance name of the Sql Server on the server. That's why it's sometimes handy to check the name from management studio, but the installation of management studio isn't necessary on the client at all. Actually it shouldn't be installed unless the user really needs it.

So to sum it: You just need to put up a correct connection string and use that for the SqlConnection and it'll work perfectly.
Mmohmmad 23-May-12 17:23pm    
Dear Mika:

Thanks for your answer. however, in my previous question (http://www.codeproject.com/Questions/390531/SQL-server-Programing) you said that if I change the computer I should change the string connection in order to establish correct connection. I can not save connection string inside the application because the nature of my program is in the way that it should be run on the other computers so the string connection will be changed.
Wendelius 23-May-12 17:33pm    
What I mean is that if you change the setup so that you now connect to your own local database you would have one kind of connection string. When you want to connect to another, remote Sql Server you need to change the connection string. However, all the clients that connect to the same Sql Server database on the same remote server have the same connection string. Well, there are some situations when this isn't correct, but usually it is.

So when you have built your connection string, all the clients should be able to use the same.

It's a good practice not to store the connection string, but you don't need to change the connection string unless you want to connect to a different server. :)

I updated the answer with examples.
Espen Harlinn 23-May-12 18:23pm    
Well done :-D
For Web Applications :
SQL
Data Source = .\.; integrated Security= true; Database=yourdatabasename;


For Windows Application :
SQL
Data Source=.\\.; integrated Security= true; Database=yourdatabasename;


Now in place of Server or Datasource first . works understood Default SQL Server and Second . stand Default Instance.
 
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