Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In web applications we use to write web.config file to access database connectivity like same way i want in windows application
Posted
Comments
Umapathi K 22-Aug-12 2:08am    
where is that format?
Sergey Alexandrovich Kryukov 22-Aug-12 2:12am    
Why do you think Windows application is different? ADO.NET is ADO.NET...
--SA
Umapathi K 22-Aug-12 2:16am    
i am new to platform

XML
<connectionstrings>
     <add name="MyDBConnectionString" providername="System.Data.SqlClient">
           connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
  </add></connectionstrings>



Then in c#
C#
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
 
Share this answer
 
Comments
ridoy 22-Aug-12 3:22am    
exactly..+5
You need to have app.config in the project, and rest of the part remains same as in web.config.
XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DBEntities" connectionString="metadata=res://*/TestModel.csdl|res://*/TestModel.ssdl|res://*/TestModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=127.0.0.1\Dev;Initial Catalog=TestDB;User ID=TestUser;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
 
Share this answer
 
Comments
Umapathi K 22-Aug-12 2:15am    
how to call in windows form from app.config?
pramod.hegde 22-Aug-12 2:38am    
Hi,
You need to use ConfigurationManager.ConnectionStrings. Before that, you need to refer System.Configuration dll if it is referred.
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBEntities"].ConnectionString);
In windows application we have app.config file,add following code to that xml file
HTML
<connectionstrings>
      <add name="MyConnectionString" providername="System.Data.SqlClient">
            connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
  </add></connectionstrings>  

To access it from form.cs write the same code as web applications .aspx.cs page as following
C#
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
 
Share this answer
 
in window application app.config is there u have to write connection string in app.config in following format

XML
<connectionstrings>
<add name="Registration_cxb.Properties.Settings.Registration_cxb_mdbConnectionString">
      connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Registration_cxb.mdb.accdb"
      providerName="System.Data.OleDb" />
</add>
</connectionstrings>
 
Share this answer
 
v3
Comments
Umapathi K 22-Aug-12 2:15am    
how to call in windows form from app.config?
1. Rightclick peoject -> properties -> resources (select string type and add connection)

2. if (using VB Language) then you can use Module for global methods & variable declarations. else in (C# & VB both) app.config file is also available for global declarations.

3. external text file and read connection from that file run time (because mostly installing application in other pc the connection string will be change due to sql login-name & password or authentication mode can be change)

Happy Coding!
:)
 
Share this answer
 
v2

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