MSSQL DB connection string is used in ASP/.NET pages to retrieve data from the MSSQL databases. This article guides you to create your connection string.
This is a sample connection string which you can use with MSSQL 2008/2012, ASP.NET 3.5 and above versions.
The following connection string has to be created in web.config file
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=(server IP)\(database instance);Initial Catalog=(database name);User ID=(database username);Password=(database password)" providerName="System.Data.SqlClient" />
</connectionStrings>
Following properties have to be changed in connection string;
- Data Source : This should be the server name of MSSQL or server IP address like "localhost" or "X.X.X.X".
- Initial Catalog: Full name of the database that you have created (you can find it in Plesk panel)
- User Id : The username that you have created in your Plesk panel to access the DB (Please make sure the access has been granted for this user to access the DB)
- Password: Password of the MSSQL DB user created in Plesk panel.\
You can use ConfigurationSettings class to read the connection string in your code
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Note: You cannot the same connectionString on your local PC to connect MSSQL DB server. It won't work as the server side configurations are different and we do not allow remote access to the hosted databases.
Another option is to use the following string as the connection string
Data Source=localhost;Initial Catalog=Your_Database;Persist Security Info=True;User ID=Database_Username;Password=Database_Password
The "Data Source=" depends on the database version you chose for the database in Plesk panel. You may need to use .\MSSQLSERVER2008 or .\MSSQLSERVER2012 instead.