Click here to Skip to main content
15,887,244 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
HI,

am developing my windows application using C#.net..here i want to connect my sql server 2008 database with my project...

My Problem is how to set sql connection string dynamically in my project...

Any one help me out of this........
Posted

Well by saving your connection string in a web.config (or app.config) file.

Add something similar:

XML
<configuration>
  <connectionStrings>
  <clear />
    <add name="Target" connectionString="server=MYSERVER; Database=MYDB; Integrated Security=SSPI;" />
  </connectionStrings>
</configuration>



Then retrieve it by using ConfigurationManager:

ConfigurationManager.ConnectionStrings["Target"].ConnectionString


EDIT:
You also need to reference the System.Configuration library in order to make it work.

A simple

textBox1.Text = ConfigurationManager.ConnectionStrings["Target"].ConnectionString;


Will show your connection string ;)


Hope it helps

Cheers!
 
Share this answer
 
v2
Comments
Nathansathya 22-Sep-11 2:09am    
ConfigurationManager cant exists in Windows application........
Mario Majčica 22-Sep-11 4:22am    
That's a new one for me! As far as I know, you just need to reference System.Configuration library and it will work like a charm. I will however check it this evening!
Mario Majčica 22-Sep-11 5:27am    
I just tried and it is working perfectly. Let me know your mail if you want the working example!

Cheers!
You can build your connection string from server name string and give that to your SqlConnection object.

C#
string constr = "Server=" + servername + ";database=" + dbname + ";integrated security=sspi";
 
Share this answer
 
Comments
Nathansathya 22-Sep-11 2:10am    
cant get clearly friend..can yu explain in detail
Mehdi Gholam 22-Sep-11 2:16am    
just replace the servername and dbname with the values you want.
Create a class for Connection and set connection string in that..
Then replace the servername and dbname when you want.
 
Share this answer
 
create class and place in it method return connection string.
static public string Cconnection()
{
get{return "data source=......."}
}
 
Share this answer
 
Comments
Rahul Kumar Ghosh 12-Apr-13 16:40pm    
I am having same problem but a bit different.
I have made App.config file provided all the values of the connection string and a separate class named "SQLCONNECTION.cs" too and provided same values for it also, but in both the cases when I need to change my password or server name i have to do it in coding.
But i want something that would made that class (SQLCONNECTION.cs) or App.config file take user inputs (Server Name, User Name, Password) from a WindowsForm and save it till the user want to change it again.
Any Solution.. ?
rahulghosh.0101@gmail.com

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