Tuesday, 14 May 2013

CONNECTION STRING FOR MS SQL DATABASE


CONNECTION STRING FOR MS SQL DATABASE


THIS ARTICLE SHOWS HOW TO CONNECT FROM YOUR WEB APPLICATION TO SQL SERVER DATABASE. IN A FEW STEPS YOU CAN LEARN HOW TO CONFIGURE DATABASE SERVER AND MAKE CHANGES IN YOUR APPLICATION CONFIGURATION FILE (WEB.CONFIG) TO GET ACCESS TO SQL DATABASE SERVER FROM YOUR WEB APPLICATION.

After you have created database in MS SQL Server 2005 you need to add user in whose name the connection will be established. To do this open object explorer, extend the “Security” folder and select “New Login” by right clicking on the “Logins” folder.
You will see a new window called “Login – New”. On the “General Page” fill in login name for application user, check “SQL Server authentication” mode, enter password for application user, uncheck “User must change password at next login”, and select default database to which your application user will be connecting.
Next select “User Mappings” Page and check the “Map” field next to the database that you use in your web application. At the bottom you will see role membership for selected database. Check  db_datareader for getting data (run the SELECT statement), db_datawriter to add, delete and change data in database, and db_owner to access database objects like stored procedures.
The last thing you need to do is change web.config in your web application. You need to add connectionString section in the global “configuration” section.
  1. <configuration>  
  2.    <connectionStrings>  
  3.       <add name="MyConnection" connectionString="Data Source=222.222.222.222;Initial Catalog=NetroTemplate_dev;  
  4.          User ID=ApplicationUserName;Password=ThisIsNotTheRealPassword" providerName="System.Data.SqlClient"/>  
  5.    </connectionStrings>  
  6. </configuration>  
In your application you can get the connection string using similar syntax:
  1. System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;  

No comments:

Post a Comment