Thursday, July 17, 2008

Connecting to Data Sources in IIS 7.0

IIS can store connection strings used by managed code applications to connect to local and remote data sources, which can include SQL Server databases and other types of databases. To view currently configured connection strings, in IIS Manager, navigate to the level of the configuration hierarchy you want to manage, and then access the Connection Strings page by double-clicking the Connection Strings feature. In the main pane, you'll see a list of the currently defined connection strings. Local entries are configured at the level you are working with. Inherited entries are configured at a higher level of the configuration hierarchy.


You can create a connection string for SQL Server by completing the following steps:

1. In IIS Manager, navigate to the level of the configuration hierarchy you want to manage, and then access the Connection Strings page by double-clicking the Connection Strings feature.

2. On the Connection Strings page, in the Actions pane, click Add. This displays the Add Connection String dialog box.

3. In the Name text box, type the name of the connection string, such as SqlServerCustDb. This name must be the same name that you reference in your application code to retrieve data that uses this connection string. You cannot change the name later without re-creating the connection string.

4. In the Server text box, type the name of the SQL server that hosts the database.

5. In the Database text box, type the name of the SQL server database.

6. Select one of the following Credentials options to specify the security credentials that are used to connect to the database:

• Use Windows Integrated Security Configures the connection string so that the application uses the current Windows identity established on the operating system thread to access the SQL Server database. Use this option to pass through authenticated Windows domain credentials to the database.
You can use integrated security only when SQL Server runs on the same computer as IIS or when you've configured delegation between computers. Additionally, all application users must be in the same domain so that their credentials are available to IIS.

• Specify Credentials Configures the connection string to use a specific SQL Server user name and password. Use this option when you do not want to pass through user credentials to the database for authentication. After you select Specify Credentials, click Set. In the Set Credentials dialog box, type the SQL Server user name to use for the connection. After you type and then confirm the password for this user, click OK.

7. Click OK to close the Add Connection String dialog box.


You can create a custom connection string for other types of database servers by completing the following steps:

1. In IIS Manager, navigate to the level of the configuration hierarchy you want to manage, and then access the Connection Strings page by double-clicking the Connection Strings feature.

2. On the Connection Strings page, in the Actions pane, click Add. This displays the Add Connection String dialog box, with the Custom option enabled.

3. In the Name text box, type the name of the connection string, such as LocalSqlServer. This name must be the same name that you reference in your application code to retrieve data that uses this connection string. You cannot change the name later without re-creating the connection string.

4. Select the Custom option, and then type the connection string. The connection string should by formatted as appropriate for the type of database to which you are connecting.
Your organization's application developer or database administrator should be able to provide the required connection string. The following example connects to a local SQL Express database, which is stored in the aspnetdb.mdf file:

Data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

5. Click OK to close the Add Connection String dialog box.


To edit an existing connection string, select the string that you want to modify, and then click Edit. In the Edit Connection String dialog box, modify the settings as necessary, and then click OK to save your changes. To remove a connection string that is no longer needed, select the connection string you want to remove, and then click Remove. When prompted to confirm the action, click Yes.

No comments: