[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

web.config connection string

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

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

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

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

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"]);
or
GetListURLs geturls = new GetListURLs(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);


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

To retrieve:
System.Configuration.Configuration rootWebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Employee");
            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.
0
 
pradeepsudharsanCommented:
0
 
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SKumar_1981Commented:
0
 
irps20001Commented:
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
http://msdn2.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.connectionstrings.aspx
0
 
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!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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