Professional Documents
Culture Documents
Objectives
2
To:
Sort
LAB
WORKING WITH DATABASES I
Using SQL Server DBs + Visual Studio
Lab 4-1
Sample Rows
Objectives:
To display rows and columns from a table
Table SalesStaff, located in a database
Company.
Holds information collected about company sales
employees.
3/20/2016
Lab 4-1
5
Lab 4-1
7
Lab 4-1
8
3/20/2016
Lab 4-1
9
Lab 4-1
10
Lab 4-1
11
Lab 4-1
12
Step 10: Click the Test Connection button. Assuming that the Test Connection
succeeded message displays, click the OK button two times to return to the
wizard, and then click the Next button.
Step 11: You will see the following message asking you if you want to copy
the database file to the project directory.
By answering yes, you can easily move your program and its database to
another computer. When you hand in programming projects, having the
database stored with the project is a good idea. Click the Yes button to continue.
3/20/2016
Lab 4-1
13
Lab 4-1
14
Expand
the
entry
under Tables, place a
check next to SalesStaff,
and
change
the
DataSet
name
to
SalesStaffDataSet (see
figure).
Lab 4-1
15
3
2
16
3/20/2016
Lab 4-1
17
Lab 4-1
18
Step 16: Open the forms Code window, and inspect the
code Visual Studio inserted into the forms Load event
handler.
The
End Sub
Lab 4-1
Lab 4-1
Step 16 (cont):
This is how the grid gets its data:
20
Step
17:
Save
and
run
the
application.
You should
see all the
rows of the
SalesStaff
table,
as
shown in the
figure.
3/20/2016
Lab 4-1
Lab 4-1
21
22
Click the button to the left of one of the grid rows. The
entire row is selected (highlighted).
Press the Del key and watch the row disappear.
The row has been removed from the in-memory DataSet, but
not the database.
The
Lab 4-1
23
If you cant see them run the prog and look at the forms design view.
3/20/2016
25
Exercise Answer
Formatting GridView Columns
26
Exercise Answer
Format Property
use
27
3/20/2016
30
It
The PK
Query Builder
32
31
Contains TableAdapters
and DataTables
Use it to view/modify
queries
3/20/2016
Query Builder
Query Builder
33
34
Grid pane
add
select
SQL pane
directly
35
Results pane
show
3/20/2016
Note
37
1.
2.
38
To avoid this problem, set the grids Dock property to None, drag the
grid a bit lower on the form with the mouse, and resize it so it fits.
Then set the grids Anchor property to Top, Bottom, Left, Right.
Objective
To create several queries that change the way rows
from the SalesStaff table are displayed in a
DataGridView control.
Lab 4-2
Filtering Rows in the SalesStaff Table
If this happens, Visual Studio may have left behind some extra code
that had been added to the designer file when the query was
created.
To sort this out, select the Rebuild <projectName> command from
Visual Studios Build menu.
Lab 4-2
39
Lab 4-2
40
10
3/20/2016
Lab 4-2
41
Lab 4-2
42
The argument passed to the method is the SalesStaff table inside the
SalesStaffDataSet DataSet:
Private
Sub
Full_TimeToolStripButton_Click()
Handles Full_TimeToolStripButton.Click
Try
Me.SalesStaffTableAdapter.Full_Time(Me.SalesStaffDataSet.SalesStaff)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Lab 4-2
43
Lab 4-2
44
Step 10: Now you will add another button to the ToolStrip that
displays all table rows.
Run
The
11
3/20/2016
Note
45
1.
Handles
End Sub
Private Sub All_RowsToolStripButton_Click(sender As Object, e As EventArgs)
All_RowsToolStripButton.Click
Try
Me.SalesStaffTableAdapter.All_Rows(Me.SalesStaffDataSet.SalesStaff)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
Handles
End Sub
End Class
12