|
This course
- Connecting to the Northwind and Blinky databases
last updated:
08/07/2006
|
In this tutorial, you connect using Visual Studio .NET to Microsoft's
sample Northwind database on the SQL server (the database is on wren.cis.upenn.edu,
which is in my office). You can also connect to the Blinky database
used in Project 5 based on the same information. There are fourteen steps...
-
Create a new C# Windows application project:
-
Go to the VS.NET menus View...Server Explorer to
open the Server Explorer window. Right-click over Data Connections
and choose Add Connection:
-
In the Choose Data Source popup, select Microsoft SQL
Server; uncheck "always use this selection" (unless you want that)
and then click Continue.
In the Add Connection popup, enter
wren.cis.upenn.edu
for the server name.
Click the radio button for "Use SQL Server Authentication"
Enter
stud10
for the user name and
hothair10
for the password.
At this point, you may
need to click on the Refresh button (at the top) and wait a few seconds. Then you'll be able
to click on the right part of the drop-down list for "Select or enter a database name:". Select
the Blinky database if working on Project 5 (although the Northwind database
is shown here). Click on OK and wait
until the cursor returns to normal. Then you should see the database connection
on the left as follows:
Optionally, right-click to rename the database connection to a name of your
own choosing.
Open the Toolbox (View...Toolbox) and drag a
DataGridView
control onto your windows form. Resize
and anchor it as you like:
When you drop the DataGridView onto the form, a popup menu may appear called "DataGridView Tasks". You
can also invoke this popup menu anytime by selecting the DataGridView control and then clicking on the
tiny arrow that appears in its upper right corner:
In the "DataGridView Tasks" popup menu, click the drop-down list titled "Choose Data Source",
and then at the bottom of the popup window, click on Add Project Data Source:
The Data Source Configuration Wizard should now run. Select Database on the first
screen, and on the second screen of the wizard, choose the data connection which
you created above (NorthWindDB in this example). Be sure to click on the radio
button to choose "Yes, include sensitive data in connection string":
In the third screen of the wizard, accept the name of the connection string which
is generated for you (NorthWindConnection String in this example):
In the next screen, open the Tables tab and select the
Blogs table
(NOTE: do not select the Users table with this login):
Click on Finish, and wait a few seconds while the Data Source Configuration wizard completes its
work. Then you should see the new data source in
the project files as follows:
Now go back and select the DataGridView in the form, and then click on its
detail arrow in the upper right to display the DataGridView Tasks menu.
The "Choose Data Source" dropdown will now have the tables where
you can expand them under Other Data Sources -> Project Data Sources ->
DataSet. Click on the
Blogs table:
Three elements should now appear in the area below the form, titled "DataSet",
"CustomersBindingSource", and "blogsTableAdapter". A one-line, empty grid will also appear in the DataGridView control.
In the following view, UNCLICK the first three Enables, and CLICK "Enable
Column Reordering":
Build the application. When it runs, the application should automatically
fetch all the data from the selected table into the grid--and all without writing
a line of code yourself.
|
NOTE: You may want to have a look at the code which was generated
by VS.NET in Form1.
|
In case you need it: Code for this tutorial
Visitors: