Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public Class1()
   {
     con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
   }

plz explain this code
how to use this code.
Posted
Comments
fjdiewornncalwe 26-Jan-12 13:13pm    
Looks like an exam or interview question, so I'm going to have to defer answering on this one.

1 solution

Break it down:
C#
public Class1()
   {
   }
Declares a constructor for a class created by an amateur - anyone with more than 2 grammes of experience would have given it a name which reflects what it actually does rather than leave the VS default name.
C#
con = ...
Assigns a value to a variable, called "con" which from context is a class level variable, from the name it should be a private field.
C#
... = new SqlConnection();
Says that the variable being assigned to is of the class SQLConnection (because SqlConnection is a sealed class, so "con" cannot have been derived from it, and it is unlikely that you would use anything lower in the inheritance chain to store an SqlConnection)
C#
...ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString...
Gets the connection string for a database from the application default configuration.

Easy isn't it?

Next time, try that yourself, and if you don't understand a part of it, google!
 
Share this answer
 
Comments
Tech Code Freak 27-Jan-12 1:11am    
Nicely explained! 5up
Anuja Pawar Indore 27-Jan-12 2:39am    
My 5+

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