0% found this document useful (0 votes)
23 views47 pages

Lecture - 9 - VP (Working With DB)

The document outlines objectives for adding data sources to projects and utilizing ADO.NET for database manipulation, including the use of System.Data and System.Data.SqlClient namespaces. It explains basic database concepts such as tables, primary keys, and common programming errors related to database integrity. Additionally, it details the ADO.NET object model, disconnected data access, and provides step-by-step instructions for creating a Windows application that interacts with a database using data binding techniques.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views47 pages

Lecture - 9 - VP (Working With DB)

The document outlines objectives for adding data sources to projects and utilizing ADO.NET for database manipulation, including the use of System.Data and System.Data.SqlClient namespaces. It explains basic database concepts such as tables, primary keys, and common programming errors related to database integrity. Additionally, it details the ADO.NET object model, disconnected data access, and provides step-by-step instructions for creating a Windows application that interacts with a database using data binding techniques.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Dr.

Ahmed Alnasheri
Objectives
 To add data sources to projects.

 To use the IDE's drag-and-drop capabilities to display database tables in


applications.

 To use the classes of namespaces:

 System.Data, and

 System.Data.SqlClient to manipulate databases.

 To use ADO.NET's disconnected object model to store data from a


database in local memory.

2
Basic Database Concepts
 A database: is a collection of related data, organized into
entities called tables that are connected together by
relationships.

 A Table: is a part of the database that contains related


information about a certain entity (Students, Teachers,
Faculties, etc.).
 A table consists of fields that represent the needed information.
 A certain data type should be specified for each field (e.g.
StudentName "string or varchar", CreditHours "Int", etc.)

 Primary Key: A unique field, that shouldn't be repeated in any


record, and shouldn't be null. 3
Basic Database Concepts
Fig. 8.1 Retrieved Data from the Different DB

AuthorID ISBN AuthorID ISBN

1 0131869000 2 0131450913
1 0131525239 2 0131426443
1 0131483986 2 0131857576
1 0131857576 2 0131483986
1 0131426443 2 0131525239
1 0131450913 2 0131869000
1 0131828274 3 0131450913
2 0131828274 4 0131828274

4
Common Programming Error
• Not providing a value for every column in a primary key breaks
the Rule of Entity Integrity and causes the DBMS to report an
error (null).

• Providing the same value for the primary key in multiple rows
breaks the Rule of Entity Integrity and causes the DBMS to
report an error (duplicate).

• Providing a foreign-key value that does not appear as a primary-


key value in another table breaks the Rule of Referential
Integrity and causes the DBMS to report an error.

• In a SQL query, failure to qualify names for columns that have


the same name in two or more tables is an error. 5
ADO.NET Object Model
 ADO.NET
 Provides an API for accessing database systems programmatically
 Replace Microsoft's ActiveX Data Objects (ADO) technology
 System.Data is the root namespace for the ADO.NET API
 Class DataTable

 Represents a table of data

 Contains a collection of DataRows and DataColumns

 System.Data.DataSet

 Consists of a set of DataTables and the relationships among them

 Represents a cache of data

 System.Data.OleDb
 Contains classes that are designed to work with any data source

6
ADO.NET Object Model
 System.Data.SqlClient
 Contains classes that are optimized to work with Microsoft SQL Server
databases
 Class SqlConnection
 Represents a connection to a data source
 Keep tracks of the location of the data source and how it is accessed

 Class SqlCommand
 Represents a SQL command that a DBMS can execute on a database

7
ADO.NET Object Model
 ADO.NET’s Disconnected Model
 DataSets that is disconnected does not need a persistent
connection to work with data
 Connect to the data source to populate the DataSet, and
disconnects immediately afterwards
 Access and manipulate the data stored in the DataSet
 If necessary, reconnect to perform an update and then disconnect
 Class SqlDataAdapter
 Connects to a SQL data source
 Executes SQL statements to both populate and update the a DataSet
 Maintains a SqlConnection object that it opens and closes as needed to
perform these operations using SqlCommands

8
Programming with ADO.NET
 Extracting Information from a Database

 The IDE provides visual programming tools and wizards that


simplify accessing data in your projects
 These tools establish database connections and create the ADO.NET to

view and manipulate the data through GUI controls

9
Programming with ADO.NET
 Displaying a Database Table in a DataGridView
 DataGridView component
 Displays a data source in a GUI
 Displays data organized in rows and columns
 Connection string
 Specifies the path to a database file
 Determine how the database is accessed
 Adding a data source causes the IDE to generate a class derived
from System.Data.DataSet
 Designed specifically to store data from that data source
 BindingNavigator component
 Provides several ways for users to browse and manipulate data displayed
by another GUI control on a Form
10
Programming with ADO.NET
 Displaying a Database Table in a DataGridView
 Step 1: Create the Project
 Step 2: Add a Data Source to the Project
 Step 3: Choose the Data Source Type to Add to the Project
 Step 4: Add a New Database Connection
 Step 5: Choose the Books.mdf or Books.mdb Data Connection
 Step 6: Save the Connection String
 Step 7: Select the Database Objects to Include in Your DataSet
 Step 8: View the Data Source in the Data Sources Window
 Step 9: View the Database in the Solution Explorer
11
Create new windows application project
Data Sources window Data menu

12
Create new windows application project
 In choose data source type dialog box select Dataset then
click next

13
Create new windows application project
 In choose your data connection dialog box click on new
connection button

14
Create new windows application project
 Select any data source file option as a data source then click
on continue button

15
Create new windows application project
 In Add connection dialog box enter a name for the database
file ex: database1 then click on ok button

16
Create new windows application project

17
Create new windows application project
 A massage box will appear to tell you that this is a new
database would you like to create it? Click on yes button

18
Create new windows application project
 Now a new database is created with a specific connection
string that will be used to connect the application to this
database.

19
Create new windows application project
 Click next

20
Create new windows application project
 Click on NO button

21
Create new windows application project
 Click on next

22
Create new windows application project
 Click on previous button

23
Create new windows application project
 Click on finish button

24
Create new windows application project

25
Create new windows application project
 Open server explorer then click on database1.mdf then
right click on Tables and choose Add new Table option

26
Create new windows application project

27
Create new windows application project
Add new filed "name" to the table as following:

28
Create new windows application project
To save changes on the table click on update then Click on
update database button

29
Create new windows application project

30
Create new windows application project
To change table name to "student"

31
Create new windows application project
Update the database then close the table

32
Create new windows application project
On data source window right click on database1Dataset then choose Configure
data source with wizard then choose tables

33
Create new windows application project
Then choose table click finish

34
Create new windows application project

35
Create new windows application project
Drag and drop student table from data source window to the form

36
Create new windows application project
Run the program

37
Create new windows application project
Add new record then click save

38
Create new windows application project
Run the program

39
Create new windows application project
Remove the record then click save

40
How Data Binding Works

 Data Binding
 Technique in which GUI controls are connected to data
sources
 Changes made through the application to the underlying data source
will automatically be reflected in the data-bound control
 Modifying the data in the data-bound control and saving the changes
updates the underlying data source

41
How Data Binding Works
 A TableAdapter is responsible for interacting with a
database on disk
 Method Fill
 Retrieves information from the database and places it in the
DataSet

 BindingSource
 Identifies a data source that a program can bind to a control
 Serves as an intermediary between the data source and the
corresponding data-bound GUI control
 Method EndEdit
 Applies changes made to data through a GUI control to the data
source bound to that control

42
Example 2

43
Example 2
1- Create New Application WindowsFormsApplication9
2- Add Label, textbox, and Button
3- Add DataSource to yor project and Select CRM.mdb database.
4- Configere your DataSource and select the customer Table.
5- Drag The DataGridView to your Form.
5- View CRMDataSet.xsd as shown in this Figure.
6- Right Click on CustomersTableAdapter  Add  Query
 Use sql statements  select which return rows.
 Then modify the SQL statement as shown bellow.

44

Example 2
using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;

 namespace WindowsFormsApplication9
 {
 public partial class Form1 : Form
 {
 public Form1()
 {
 InitializeComponent();
 }

 private void button1_Click(object sender, EventArgs e)


 {
 this.customersTableAdapter.Fill(this.cRMDataSet.Customers, comboBox1.Text);
 }
 }
 } 45
Homework 8

46
The End
47

You might also like