Click here to Skip to main content
15,901,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0 down vote
favorite
I'm building desktop application using c# , I put the connection string in the app.config file like this

C#
<connectionStrings>
       <add name="ComputerManagement"
       connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source=...;Initial Catalog=Computersh;Integrated Security=True"/>
     </connectionStrings>


How I can call the connection string in the forms ? or is it better to make a class for connection string and how I can do that ?
Posted

You can call the connection from app.config with below code

1.)add namespace
using System.Configuration

2.)Add reference to your project for System.Configuration

3.)You will get connection string in your form with below code
string conntr = ConfigurationManager.ConnectionStrings["ComputerManagement"].ConnectionString;



Happy coding :)
 
Share this answer
 
Comments
Lotus90 19-Feb-13 7:54am    
I did that ; but when I add it to my form and want to write an insertion query inside a button ;
It doesn't connect to the database ?
[no name] 19-Feb-13 8:01am    
can you post ur connection code here...
Lotus90 19-Feb-13 10:46am    
public string GetConnectionString()
{

return System.Configuration.ConfigurationManager.ConnectionStrings["ComputerManagement"].ConnectionString;





}
private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection(GetConnectionString());
OleDbCommand command = new OleDbCommand();

command.CommandType = CommandType.Text;


conn.Open();
command = conn.CreateCommand();
command.CommandText = "INSERT INTO Clients(C_name,C_phone ,C_mob ,C_add , C_email ,C_account) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
command.ExecuteNonQuery();


conn.Close();
}
catch (Exception) { throw; }
I am sure you didn't search the web before posting the question here. Try this - using connection string in app.config[^]
 
Share this answer
 
Comments
Lotus90 19-Feb-13 7:54am    
I search the web and I don't get what I want :)
Orcun Iyigun 19-Feb-13 9:29am    
Try harder!! because you have the answers here. I am sure it is something you are doing wrong
Lotus90 19-Feb-13 10:48am    
Yea . I'm sure too , still not detect it !

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