Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all

I want to change connection string according to path of database to be dynamic not to write it by my self:
C#
SqlConnection conn = new SqlConnection("Data Source=MS-TOSHIBA;Initial Catalog=dalily system;Integrated Security=True");
Posted
Updated 16-Jun-12 3:48am
v2
Comments
OriginalGriff 16-Jun-12 10:26am    
Yes? And?
Your problem is what?
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

Hi you can use the following approach

C#
String ConnString = string.Empty;
         switch (DB)
         {
             case "ABC":
                 ConnString = "Data Source=MS-TOSHIBA;Initial Catalog=dalily system;Integrated Security=True";
                 break;

             case "DEF":
                 ConnString = "Data Source=MS-TOSHIBA2;Initial Catalog=dalily system;Integrated Security=True";
                 break;

             case "XYZ":
                 ConnString = "Data Source= "+DB+";Initial Catalog=dalily system;Integrated Security=True";
                 break;

             default:
                 ConnString = "Data Source=MS-MYHOME;Initial Catalog=dalily system;Integrated Security=True";
                 break;

         }
         SqlConnection conn = new SqlConnection(ConnString);


Can you directly use hardcode connection string as it is done in Case 1 & 2 or you can create it at run time as mentioned in case XYZ

Hope it helps.... cheerz
 
Share this answer
 
 
Share this answer
 
Comments
mrx100 16-Jun-12 16:48pm    
http://msdn.microsoft.com/en-us/library/ms178411.aspx[^]
this is for asp.net Iam using sql server 2005 & c#

i want to make connection string like that code

string strPath = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + (System.AppDomain.CurrentDomain.BaseDirectory) + "c#project.accdb".ToString();
OleDbConnection con;
con = new OleDbConnection(strPath);


but with sql server 2005
Zoltán Zörgő 16-Jun-12 18:09pm    
This sample is for access 2010 files, you wrote you need it for sql server. In sql server you don't need the database file path, you have to use the database name. If the database is not attached, you need to attach it first.
mrx100 17-Jun-12 14:10pm    
i want to give program to friend , run on his pc without change the connection string thats all
bbirajdar 17-Jun-12 15:22pm    
In that case , your friend should tell you the name of the database instance to you in advance...Otherwise you need to write the code to find the name of database on the machine and then dynamically construct the connection string

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