Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What is the difference between
C#
public SqlConnection getConnection()
   {SqlConnection conn;
       string str = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
       conn = new SqlConnection(str);
       conn.Open();
       return conn;
   }


C#
public SqlConnection getConnection()
    {
        string str = "Data source=Gisserver;initial catalog=Database;Uid=sa;pwd=sa";
        conn = new SqlConnection(str);
        conn.Open();
        return conn;
    }


I use second one then connection established . Why not executed first properly.
Posted

The difference between the 1st and 2nd code snippet is:

In the first case, you are storing the connection string in a config file.
The advantage being, it is not hard coded in your application and so it can be easily configured. What I mean is you may have a situation where you need to change your connection string, with this option you can easily change the server name, database, or authentication information without editing individual Web pages. Additionally, you can secure the connection string using encryption
In second case, you are hard coding the connection string which is not a good practice for the reason I explained above.

The other responders have already told you why the first case is not working. :)
 
Share this answer
 
v2
I don't think there is a problem in your code for first one you should check if you add connection string properly in you web config file.
This is the only one problem i think in it.
 
Share this answer
 
in case of the first one i.e
ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString
u have to put your connection string in web.config like
XML
<appSettings>
  <add key="MyConnection" value="Data Source=RITESH\SQLEXPRESS;Initial Catalog=Test1; Integrated Security=true"/>
</appSettings>


and its secure way to keep connection string in web config.
there should not be any issue og not getting connection this way.

==================================================================
Arun : I don't think that this is correct.What you specified is ConfigurationManager.AppSettings["MyConnection"] not ConfigurationManager.ConnectionStrings["MyConnection"].
==================================================================
 
Share this answer
 
v2
The main advantage of it is portability and decoupled design.

Let's check the other side, if we hard code the connection string then what is disadvantage of it.

1. First and foremost it is hard-coded. So if I want to reuse, it is not possible. I must hard code again and again whenever I want to reuse the same connection string.

2. The second and most important disadvantage of it is once we build the project and we need to change the connection string (either database server name change or database name change) then we need to change everywhere wherever we hard coded our connection string.
[While in second first case we need to change only at one place (in config file) and it will take new connection string]

Hope you understand!
----
Edit (by Ankur): In the last line you must be talking about the first case. :)
 
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