• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 980
  • Last Modified:

web.config connection string

I want to verify if this is the right syntax in .net 2.0

            <add name="MyConn" connectionString="Data Source=local;Initial Catalog=URLs; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes" providerName="System.Data.SqlClient"/>

or should I  be doing it this way in .NET 2.0?

<add key="ConnectionInfo" value="server=(local);database=dbname;Integrated Security=SSPI" />

In my code-behind I'm trying to access my connection like this (C#):

        GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]

When accessing this using a new instance of my class, what's the difference and which way should I be referencing my connection?

GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);
GetListURLs geturls = new GetListURLs(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);

I'm confused coming from VS 2003 to VS 2005
1 Solution
By default VS 2005 will look to connectioStrings in your web.config for connections.  I use:
    <remove name="LocalSqlServer"/>
    <add name="Employee" connectionString="Data Source=localhost;Initial Catalog=Employee;Integrated Security=True" providerName="System.Data.SqlClient"/>

To retrieve:
System.Configuration.Configuration rootWebConfig =
            System.Configuration.ConnectionStringSettings connString = new ConnectionStringSettings();
            connString = rootWebConfig.ConnectionStrings.ConnectionStrings["Employee"];

I do not know that this is the best way but I took this right from Microsoft's help.

Hope this helps.
dba123Author Commented:
I've seen that artible but it's talking about encryption and nowhere do I see an example of the correct connection string syntax...nor how to call it correctly in the way that I'm trying to do with my class constructor
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

here is the another way to get the connection string.
string cnn = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;

Microsft is giving more info about ConnectionStrings, here is the link, may help you more
dba123Author Commented:
Finally,  I changed the connection string to use the correct SQL instance.  I didn't have it named as local in SQL 2005 Management Studio, I had it actually as pcname\LOCAL

that fixed it!

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now