Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Write a database connection string in web.config

This guide shows you how to tell your ASP.NET website how to connect to its database by using the web.config file.

Plesk1 min read7 steps1 screenshots

What is a connection string?

A connection string is a line of text that holds the address and login for a database. It is like a note that says where the filing cabinet is and which key opens it. The file web.config is a settings file for ASP.NET sites on Windows hosting. It sits in the main folder of your site.

A database is a storage place for your site's data.

What you need

  • The database server name. Often it is localhost. Your welcome email or Hostvento support can tell you.
  • The database name.
  • The database username and password. You can create these in Plesk under Databases.

Steps

  1. Back up your site. A typing mistake in web.config can stop the whole site.
  2. Open the file with the Plesk File Manager or an FTP program. It is in the httpdocs folder.
    Screenshot: Open the file with the Plesk File Manager or an FTP program. It is in the httpdocs folder.
  3. Find the <configuration> tag.
  4. Add a connectionStrings block inside it. Use this example for SQL Server:
<connectionStrings>
  <add name="MyDb"
       connectionString="Data Source=localhost;Initial Catalog=mydatabase;User ID=myuser;Password=mypassword;"
       providerName="System.Data.SqlClient" />
</connectionStrings>

This creates a connection named MyDb. Replace the server, database, user and password with your own values.

  1. Save the file.
  2. Reload your site to check it works.
  3. In your code, read the string by its name, MyDb.
Tip: Warning: web.config holds a password. Never share it or upload it to a public place. If your password has symbols like &, write it as &amp; in the file.

If you see an error, check for typos and missing quotes. Open a support ticket if you need help.

Quick recap

  • A connection string tells your site how to reach its database.
  • It goes inside <connectionStrings> in web.config.
  • You need server, database, user and password.
  • Back up before editing.
  • Keep the file private.