Configuring a website to use a hosted database
-
We need to tell our Web site where to look for its membership
database. By default, a connection string named 'LocalSqlServer'
is already defined and the web server will try and look in that
location. (This is defined in a file called machine.config which
resides on the computer/server we are using).
-
We need to tell the web server:
-
Not to look in that usual location, and
-
Where it should look instead.
-
In order to do that we need to modify the web.config file in our
application. The web.config file is an XML document that stores
configuration settings for our website.
-
Directly under the opening tag for the
<configuration>
section, add a new section named <connectionStrings>
, if it
does not already exist. Remember that we are working with XML, so
ensure you add a corresponding closing tag.
-
The next step is to remove the reference to the default database
(inherited from the machine.config file). Within the
connectionStrings section, add the following line of code:
<remove name="LocalSqlServer"/>
-
We now need to add a reference to our new database. The example connection string below (taken from the
MSDN connection strings example page
) provides the syntax required for a connection string connecting to an instance of SQL server (as you are). You will need to modify the appropriate elements to connect to your individual database.
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />
-
Change the name of the connection string to
LocalSqlServer
(to replace the reference to the one we removed above)
-
Set the server address, database name, username and password