Data Components In Visual Studio .NET - Part Four

Understanding Data Connections

To connect to a data source, the first thing you need to learn about is a data connection. Each data provider has a connection class, and if you're using VS.NET, you can see these class objects as components in the Toolbox > Data tab. For example, the SqlConnection, OdbcConnection, and OleDbConnection class objects represent a connection for the Sql, Odbc and OleDb data providers, respectively. See the following,
  • SqlConnection creates and manages SQL server database connections.
  • OdbcConnection creates and manages connections to ODBC data sources.
  • OleDbConnection creates and manages connections to an OLE-DB data sources.
In VS.NET, you can create a connection component in many ways. You can use the IDE to add a connection object to a project, create it programmatically, or use data adapters that automatically create a connection object for you. In this article, we'll be concentrating on adding to a connection through VS.NET. The easiest way to add a connection to project in VS.NET is to drag a connection component (SqlConnection, OleDbConnection, or OdbcConnection) from the Toolbox's Data tab. This action adds a connection object to your project. After that, you can set connection's properties using the Properties windows. For this demonstration, I'll drop a SqlConnection from the toolbox onto the form. Below Figure shows the properties window displayed after creating the SqlConnection. Note that the default connection name is the class name with a unique number appended to it. Because this is the first Connection Object, the connection is SqlConnection1.

Data Components In Visual Studio .NET
Figure: The SqlConnection component's properties
As you can see from the properties window in figure, a connection's properties include Database, ConnectionTimeout, DataSource, PacketSize, WorkStationId, Name, and ConnectionString.
The connection properties depend on the data provider. Some properties may not be available for other data providers. For example, the WorkStationId property is available in Sql data providers but not in OleDb or ODBC data providers.

Understanding Connection Strings

The ConnectionString property is the main property of a connection. By clicking the drop-down list of the ConnectionString property, you can see all the available data connections. If you don't have a data connection, you can use its New Connection option (see figure below), which launches the Data Link Properties Wizard. Refer to the previous article "Using the Server Explorer" section.

Data Components In Visual Studio .NET
Figure: Connection string property options
After choosing the New Connection option and launching the Data Link Properties Wizard, you choose a server in the Connection tab. On my machine, the SQL server's name is MEHRAS\SQLSERVER2005, the user ID and password aren't entered because I'm using Windows NT Integrated Security. You need to enter your server name (or select from the drop-down list), and enter your user ID and password if you're not using Windows NT integrated security option (see figure 4-16).

Data Components In Visual Studio .NET
Figure: Data Link Properties wizard
The SQLConnection string looks like following: Data Source=MEHRAS\SQLSERVER2005;Initial Catalog=Puran;Integrated Security=True


Hope this article would have helped you in understanding Data Connections. See my other articles on the website on ADO.NET.