Creating a connection

Creating a connection

Since the database is an external resource, the first thing to do is create a connection using a connection string with instructions on the location of the database and the authentication method to use.  Instead of hard-coding the connection string into the code, a more manageable solution would be to put the connection string in the app.config file (web.config in a web application). The connection string consists of 3 main parts:

  • The server (data source)
  • The database (initial catalog)
  • The authentication mechanism (discussed below)

By setting integrated security to true, the connection string below uses Windows authentication which means that it’s using the application’s current Windows identity to connect to the database. This is the recommended method.

<connectionStrings>
	<add name="EmployeeDatabase" connectionString="data source=Server1;initial catalog=EmployeeDb;integrated security=true"/>
</connectionStrings>
You must sign up to see the rest of this content.