Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hello guys,

I'm developing c# app . I want to connect to database on different computer through switch ,how can I do it?
Posted
Comments
Sandeep Mewara 26-Sep-12 3:26am    
connect to database on different computer through switch
Elaborate please!
Oshtri Deka 26-Sep-12 3:36am    
Need more details.
Member 8584763 26-Sep-12 4:10am    
i've c# app want to update and insert on database on different computer which has the database, and I connect to it though switch

Where is the problem? Replace "localhost" in your connection string with the name of the computer the database is running on.
Depending on the database engine (which one - why don't you tell us?) you may have to configure it to accept connections from different machines. The firewalls should be configured to allow the communication.
 
Share this answer
 
In the connectionStrings section in your web\app config you specify the different connection strings:-
<connectionstrings>
  <add name="ConnString1" connectionstring="Data Source=serverinstancename;Initial Catalog=DBName;User Id=username;Password=password;" />
  <add name="ConnString2" connectionstring="Data Source=serverinstancename;Initial Catalog=DBName;User Id=username;Password=password;" />
</connectionstrings>


then in your code you could have a method which returns an SQLConnection based on the criteria in your switch

public SqlConnection getConnection()
{
   string conn = "";
   switch(yourDependancy)
   {
   case a:
       conn = configurationManager.ConnectionStrings["ConnString1"].ConnectionString;
       break;
   case b:
       conn = configurationManager.ConnectionStrings["ConnString2"].ConnectionString;
       break;
   }
   return new SqlConnection(conn);
}


you will obviously need to check that values exist\nulls etc and maybe include a default case for your switch but this is one way of doing what I think you are after.
 
Share this answer
 
v2
Comments
Member 8584763 26-Sep-12 8:08am    
my app is desktop , will it be different
Paul E Davies 26-Sep-12 8:27am    
No - only where your connection string is held (web.config or in your case app.config or hard coded)
Member 8584763 26-Sep-12 9:02am    
This is my connection string:
internal static string x = @"Data Source=.\Ahmed;Initial Catalog=SuperMarket;Integrated Security=True";

in your function I'm using connection string not configuration manager
Paul E Davies 26-Sep-12 10:26am    
just have as many connection strings as you need and using the switch statement as above select the appropriate one. It doesn't matter where the connection string is held as far as the switch statement is concerned (although an internal hardcoded string is just about one the worst places to put it)
Member 8584763 26-Sep-12 12:05pm    
what do you suggest?
Where is the problem? Replace "localhost" in your connection string with the name of the computer the database is running on.
Depending on the database engine (which one - why don't you tell us?) you may have to configure it to accept connections from different machines. The firewalls should be configured to allow the communication.
 
Share this answer
 
Comments
Member 8584763 26-Sep-12 4:28am    
it's sql server.
Member 8584763 26-Sep-12 4:37am    
is it like remote access?

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