Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
newbie question..

What is the best way to configure the connection string, in Web.config or in a public method?

im trying to create web project connected to a database [MSSQL SERVER 2008 R2]..

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace asp
{
    public class connection
    {
        public string constring()
        {
            string str = @"SERVER=MACHUCA-PC\SQLEXPRESS;DATABASE=testdb;TRUSTED_CONNECTION=True";

            return str;
        }
    }
}
Posted
Updated 13-Oct-17 20:57pm

1 solution

Web.config

If you hard code the string you have to recompile the code when it changes.

And it changes every time you do maintenance, or upgrades. Because if it doesn't, your code tests against the production database and everything you do risks disaster. If you forget the WHERE clause for an UPDATE command for example: and all customers get their delivery address changed. Or you test a sale price reduction and everybody online gets the product for free perhaps. These things happen in development: and provided you are working against a development DB which is not the production one, it's no biggie. Testing against production is a very bad move!

If you hard code strings, you have to change your (tested, and perfect) code before release - and you can't test those changes except in production, where every mistake costs downtime and customers.
 
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