Download presentation
Presentation is loading. Please wait.
1
CIS16 Application Programming with Visual Basic
Chapter Eleven SQL Server Databases
2
Database Terminology Computer database
An electronic file that contains an organized collection of related information Popular databases Microsoft Access Microsoft SQL Server Oracle Interface DBMS Computer program
3
Database Terminology Relational database
Stores information in tables composed of columns and rows, similar to the format used in a spreadsheet Information can be related in different way Table A group of records Records A group of related data (fields)
4
Database Terminology (cont'd.)
Primary key A field that uniquely identifies each record Parent and Child tables Tables related to each other through a common field Foreign key The field in the child table that matches the field in the parent table Example of a two-table relational database
5
Connecting an Application to a Database
Data accessed from the database
6
CREATING A SQL SERVER DATABASE USING VISUAL STUDIO
7
Step 1: Defining the Database
Click Project->Add New-> Service-based database & specify the name of the SQL SERVER database in the NAME textbox
8
Step 2:Verification In the Solution explorer, the MDF file will appear.
9
STEP 3: Server Explorer Right click the MDF file and choose open, to open the SERVER EXPLORER Window
10
Creating Database objects
Or..open the SERVER EXPLORER, expand DATA CONNECTIONS, and EXPAND THE DATABASE, to see its OBJECTS To create a table, right click TABLES in the server explorer and choose ADD NEW TABLE
11
Creating tables in Server Explorer
Table designer opens with a new empty table
12
Creating Table fields in Server Explorer
Two most important properties for field are: Name: identifies field Data Type: designates type of information that an be stored in the field Defining fields Choose a data type that minimizes the amount of storage needed for the data Indicate if field can store a null value (no entry) Indicate default values Indicate identity fields – automatically generated; assign seed or increment Indicate constraints – ex. Data in field must be > 100
13
SQL Server data types Bit Char, varchar, text
Boolean – value of 0 or 1, true or false Char, varchar, text Any combination of letters, symbols, numbers Date, time, datetime, smalldatetime Represents a date &/ time Decimal, numeric: numeric data integer and fractional portion Float, real decimal Bigint, int, smallint, tinyint numeric data with only the integer portion Money, smallmoney currency accurate to 4 decimal places
14
Creating tables in Server Explorer
Begin adding fields and field properties To set a compound key, modify the TSQL Be sure to give your table a good name before saving your SQL and before clicking the UPDATE button
15
Creating tables in Server Explorer
Click UPDATE BUTTON to PREVIEW THEN CLICK UPDATE DATABASE TO ADD TABLE
16
Creating tables in Server Explorer
THE OPERATIONS WINDOW WILL DISPLAY THE STATUS OF THE UPDATE
17
Creating tables in Server Explorer
When complete, refresh, and the Server Explorer displays the table Expanding the table will display its fields Selecting a field will display its properties in the properties pane Right click the table and select “OPEN TABLE DEFINITION” to redisplay the TABLE DESIGNER window
18
Relationships Associates rows of one table with rows of another
To create a foreign key relationship in Table Designer Open the FOREIGN KEY TABLE in Table Designer In the Foreign-key Relationships dialog box, right-click and choose Add new Relationship. Replace the placeholders in the script pane Click UPDATE to change the table and add the relationship
19
Adding Data to a table Right-Click the table and choose SHOW TABLE DATA Begin adding records If you try to add a record that violates the foreign key, an error will occur!
20
Connecting an application to a SQL SERVER database
21
Connecting an Application to a Database
Before a software application can access the data is a table, it must connect to the database Then, it must define the source of data Data source connection wizard Specify the data you want to access Computer makes a copy of that data and stores the copy in memory as a dataset Allows you to also test that the connection works
22
Data Source Wizard dsadsa
23
Selecting the database
Specify the database which the data source will use
24
Saving the connection information
Click to save all of the information
25
Table selection Next select the tables and fields which will be used in this data source
26
Finished Data source The data source has created the data set definition and saved it in a DataSet file. Test it by right-clicking the Data Set and choose Preview Data
27
App.config connection Connection is defined in hidden file, app.config
28
Testing the data set Right click data set in data source pane and choose Edit dataset with Designer
29
Testing the data set Right click the table name in the Date source pane and choose Preview Data
30
Testing the data set Click Preview button
31
BINDING TO THE DATA SOURCE
32
Binding the Objects in a Dataset (cont'd.)
Connecting a dataset to a control on the form Connected controls are called bound controls The TableAdapter object connects the database, retrieves the appropriate information, and stores it in the DataSet object A bound control can then access the data using the BindingSource object
33
Binding the Objects in a Dataset (cont'd.)
5 objects in component tray Dataset BindingSource TableAdapter TableAdapterManager Binding Navigator
34
Ways to bind data Automatically create bound controls by dragging the dataset onto the form If controls already exist, bind by dragging a field in the data set onto the existing control When controls are bound, the form_load procedure has code written to it automatically to connect to the dataset
35
Automatically create the bound control
36
EX 1:Automatically Create Bound Controls (method 1)
Drag a field or the table from a dataset to an empty area on the form
37
EX 1:Automatically Create Bound Controls (method 2)
Drag the Table Object to the form Creates a DataGridView control and a binding navigator control The DataGridView control displays the data in a format similar to a spreadsheet The BindingNavigator control is used to move from one record to the next in the dataset, as well as to add or delete a record and save any changes made to the dataset
38
EX 1:Automatically Create Bound Controls (method 3)
To display data in separate textboxes rather than DataGrid, click on Details and drag table by name onto form
39
Binding to an existing control
Microsoft Visual Basic 2012: Reloaded, Fifth Edition Binding to an existing control
40
Ex 2: Drag a data field onto an existing control
Fewer data objects in the system tray
41
Ex 2: Drag a data field onto an existing control (cont)
Drawbacks Navigation toolbar is not automatically created Dragging to existing control does not add a BindingNavigator object to the component tray Doesn’t add a BindingNavigator control to the form
42
Ex 2: Drag a data field onto an existing control(cont)
To create navigation toolbar manually add a BindingNavigator control and object to the application
43
Ex 2: Drag a data field onto an existing control (cont'd.)
Set the control’s DataSource property to the name of the BindingSource object in the Items Collection Editor
44
Ex 2: Drag a data field onto an existing control (cont'd.)
Alternative is to code the Next Record and Previous Record buttons to view the remaining records, using BindingSource component in the component tray
45
Ex 2: Drag a data field onto an existing control (cont'd.)
The BindingSource object uses an invisible record pointer to keep track of the current record in the dataset Pointer is stored in Position property, starting at 0 Can set position to move to record
46
Ex 2: Drag a data field onto an existing control (cont'd.)
Can use the BindingSource object’s Move methods
47
Ex 2: Drag a data field onto an existing control (cont'd.)
When user clicks next record button, procedure should move record pointer to next record
48
DataGridView control
49
DataGridView control Most popular, as can view lots of information at a glance Displays the data in a row and column format Each row represents a record, and each column represents a field The intersection of a row and a column in a DataGridView control is called a cell
50
DataGridView control Drag DataViewGrid control onto form
Open smart tag, choose data source If the project data sources isn’t displaying, choose add project data source, and complete the wizard
51
DataGridView control (cont'd.)
DataGridView control has a task list
52
DataGridView control Edit Columns used to improve appearance
Add or remove columns from DataGrid Resequence controls Set properties of bound control in Datagrid Cell style, width, alignment, etc. Can change the heading for the column
53
Binding navigator control
Microsoft Visual Basic 2012: Reloaded, Fifth Edition Binding navigator control
54
Personalizing a Binding Navigator Control
BindingNavigator control contains buttons that allow you to move to a different record in the dataset, add or delete a record, and save any changes made to the dataset Might want to include additional items on the BindingNavigator control another button, a text box, or a drop-down button Code is automatically written for binding navigator events
55
Customizing a Binding Navigator Control
Click the binding navigators task box to choose the item to add
56
Customizing a Binding Navigator Control (cont'd.)
DropDownButton added to the BindingNavigator control Double-click to write code for new navigation item
57
Connecting an application to aN ACCESS database
58
Connecting an Application to a Microsoft Access Database (cont'd.)
59
Connecting an Application to a Microsoft Access Database (cont'd.)
60
The Copy to Output Directory Property
Data Source configuration added database to project Referred to as a Local database At run time, database in bin/debug folder is used Will have 2 copies of the database Copy to Output directory property determines how changes are saved to database
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.