Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 21-Sep-11 19:14pm
Rate this: bad
good
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:
 
<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 Wink | ;)
 

Hope it helps
 
Cheers!
  Permalink  
v2
Comments
Nathansathya at 22-Sep-11 2:09am
   
ConfigurationManager cant exists in Windows application........
Mario Majcica at 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 at 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!
Rate this: bad
good
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";
  Permalink  
Comments
Nathansathya at 22-Sep-11 2:10am
   
cant get clearly friend..can yu explain in detail
Mehdi Gholam at 22-Sep-11 2:16am
   
just replace the servername and dbname with the values you want.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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=......."}
}
  Permalink  
Comments
Rahul Kumar Ghosh at 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)

  Print Answers RSS
0 OriginalGriff 225
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ChauhanAjay 80
4 ProgramFOX 80
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 24 Sep 2011
Copyright © CodeProject, 1999-2014
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