Nội dung Text: Creating a Connection Object Using Visual Studio .NET
Creating a Connection Object Using Visual Studio .NET
To create a SqlConnection object using Visual Studio .NET, you drag a SqlConnection
object from the Data tab of the Toolbox to your form. You'll recall that a SqlConnection
object allows you to connect to a SQL Server database. You can also drag an
OleDbConnection object from the Toolbox to your form to connect to a database that
supports OLE DB.
Figure 7.1 shows a form with a SqlConnection object. This object is assigned the default
name of sqlConnection1.
Figure 7.1: Creating a SqlConnection object with Visual Studio .NET
Once you've created a SqlConnection object, that object appears in the "tray" below the
form. The tray is used to store nonvisual components like SqlConnection objects. Other
objects that appear in the tray are SqlCommand objects. These objects are considered
nonvisual because you don't see them when you run your form. You can of course still
work with them visually when designing your form.
To the right of the form, you'll notice the Properties window, which you use to set the
properties for your SqlConnection object. To set the ConnectionString property that
specifies the details of the database connection, you can either type in the string directly
or click the drop-down list and build the ConnectionString visually. To do this, you select
New Connection from the drop-down list, which displays the Data Link Properties dialog
box. This dialog box contains four tabs, the first of which is the Provider tab, which
allows you to select the type of provider you want to connect to, as shown in Figure 7.2.
Figure 7.2: Selecting the provider
Click the Next button to continue to the Connection tab (you can also click the
Connection tab directly), where you enter the details for your database connection, as
shown in Figure 7.3.
Figure 7.3: Entering the connection details
Warning For security reasons, do not enable the Allow Saving Password check box. If
you did, your password would be stored in the actual code, and anyone could
get your password from the code. Leave Allow Saving Password in its default
non-enabled state; that way, the user will be prompted to enter the password.
For testing purposes, however, leaving your password in is sometimes
acceptable-just remember not to release your password in production code.
Once you've entered your connection details, you can press the Test Connection button to
ensure your details are correct.
At this point, you've entered all the mandatory details, and you can choose to save your
details by clicking OK, or you can click Advanced to enter additional details such as the
connection timeout, as shown in Figure 7.4.
Figure 7.4: Entering the advanced connection details
You can also click the All tab to view and edit all the values for the connection, as shown
in Figure 7.5. To edit a value, you click Edit Value.
Figure 7.5: Viewing all the connection details
Click the OK button to save your connection details.
On my computer, the ConnectionString property for my SqlConnection object that
connects to the SQL Server Northwind database is set to
data source=localhost;initial catalog=Northwind;persist security info=False;
user id=sa;pwd=sa;workstation id=JMPRICE-DT1;packet size=4096
Note The persist security info Boolean value controls whether security-sensitive
information such as your password is returned in a connection that has been
previously opened. You'll typically want to leave this in the default setting of False.
Coding an Event in VS .NET
You can add code for an event in VS .NET. For example, let's say you wanted to add
code for the State-Change event of the sqlConnection1 object created earlier. To do this,
you first select sqlConnection1 in the tray, then you click the Events (lighting) button in
the Properties window. Figure 7.6 shows the sqlConnection1 object's events.
Figure 7.6: sqlConnection1 object's events
You then double-click the name of the event in the Properties window you want to code.
In this example, you double-click the StateChange event. VS .NET then displays the code
and creates a skeleton of the event handler method for you, as shown in Figure 7.7. The
cursor shows where you add your code.
Figure 7.7: The beginning StateChange event handler method
All you have to do is add your code to the event handler method. For example, you can
set your method to
private void sqlConnection1_StateChange(
object sender, System.Data.StateChangeEventArgs e)
"State has changed from "+
e.OriginalState + "to "+
Figure 7.8 shows the completed event handler method.
Figure 7.8: The completed StateChange event handler method
Once you've created a SqlConnection object, you can then use it with other ADO.NET
objects, such as a SqlCommand object. You'll see how to do that with VS .NET in