Click here to Skip to main content
15,898,999 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the best way to connect to sql server database.

need a professional way not for beginner

please give an example

regards
Posted
Updated 21-Jul-12 3:15am
v2

 
Share this answer
 
C#
SqlConnection conn=null;

try
{
  String strConnection = String.Empty;
  strConnection = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString();

   using (conn = new SqlConnection(strConnection))
   {
       //code here
   }
}
catch (Exception ex)
{
}
finally
{
  conn.Close();
  conn = null;
}


Thanks,
 
Share this answer
 
v3
Comments
nandjaring 21-Jul-12 9:40am    
can i use the connection any where in my other forms
Abdul Quader Mamun 21-Jul-12 9:43am    
Yes, Remember you have to a connection string in app.config file.
Any specific reason for repost when I already replied you here: SQL server connection[^]

Please avoid reposts.
 
Share this answer
 

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