Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Connection string in web.config
Message
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
00823105
Message ID:
00823295
Views:
20
Yes, you definitively want to store it in a separate file. Web.Config is the most common place.

As Jayesh mentioned, you might want to wrap the call to the web config in a method, so that your business objects do something like DataServices.ConnectToDB() regardless of whether you store the connection string in Web.Config or in another file, or whether you encrypt the conn string or not.
Imports System.Data.SqlClient
Imports System.Data

Public Class DataServices

    Public Shared Function ConnectToDB() As SqlConnection
        Dim Conn As SqlConnection
        Dim ConnString As String

        ConnString = ConfigurationSettings.AppSettings("ConnectionString")
        If ConnString Is Nothing OrElse ConnString.Length = 0 Then
            ' use a default conn string is no one is found in config
ConnString = "server=(local);database=adefaultdbperhaps;trusted_connection=yes"
        End If

        Try
            Conn = New SqlConnection(ConnString)
            Conn.Open()
        Catch ex As Exception
            Conn = Nothing
        End Try

        Return Conn

    End Function

End Class
>I am not using Mere Mortals.NET (yet!), but I am using the business object in the Hentzenwerke book. Right now, I have the database connection string hard-coded in the BusinessObject base class. Is it a better idea to put this in the web.config file? That seems to be what I am reading so far as best practices. If so, then should I set the connection string from web.config in the BusinessObject constructor method?
>
>
>public BusinessObject()
>{
>   this.strConnectionString = ConfigurationSettings.AppSettings("constring");
>}
>
Hector Correa
Previous
Reply
Map
View

Click here to load this message in the networking platform