Click here to Skip to main content
12,697,294 members (28,115 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C# SQL-Server , +

am developing my windows application using 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 21-Sep-11 20:14pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

Add something similar:

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

Then retrieve it by using ConfigurationManager:


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

Nathansathya 22-Sep-11 2:09am
ConfigurationManager cant exists in Windows application........
Mario Majcica 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 Majcica 22-Sep-11 5:27am
I just tried and it is working perfectly. Let me know your mail if you want the working example!

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

string constr = "Server=" + servername + ";database=" + dbname + ";integrated security=sspi";
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Create a class for Connection and set connection string in that..
Then replace the servername and dbname when you want.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

create class and place in it method return connection string.
static public string Cconnection()
get{return "data source=......."}
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.. ?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 24 Sep 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100