Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ADO.NET (part 1)
Objectives
Microsoft 2
Part 1
Microsoft 3
Relational databases
DB
• Most of today's databases are relational:
– database contains 1 or more tables
– table contains 1 or more records
– record contains 1 or more fields
– field contains data, or NULL
Microsoft 4
Example
Microsoft 5
Database management systems
DB Data
request
Engine
Microsoft 6
Common types of DBMS
Microsoft 7
Local DBMS
DB Data
Engine
client process
• Example?
– MS Access
– underlying DB engine is JET ("Joint Engine Technology")
Microsoft 8
Server DBMS
network? DB Data
Engine
server
client
– Examples?
• MS SQL Server, Oracle, DB2, MySQL
Microsoft 9
Structured Query Language
Microsoft 10
Select queries
• Key concept:
– what you get back is another (temporary) table!
Microsoft 11
Example
Microsoft 12
Example #2
Microsoft 13
Computed fields
Microsoft 14
Joins
Microsoft 15
Nested queries
Microsoft 16
Part 2
Microsoft 17
Architecture
.NET
Programmer
ADO.NET
Data Provider
DB
Microsoft 18
Existing data providers
Microsoft 19
ADO.NET object model
provider-
System.Data neutral
System.Data.Common
SQL Server
System.Data.SqlClient
other
DBs
System.Data.OleDb
Microsoft 20
Overview of database access
• Three steps:
1. open connection to database
2. execute SQL to update DB / retrieve records
3. close connection
Microsoft 21
Step 1: open connection
using System.Data;
using System.Data.OleDb;
string sConnection;
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=Sales.mdb";
connection
OleDbConnection dbConn;
dbConn = new OleDbConnection(sConnection);
dbConn.Open();
MessageBox.Show( dbConn.State.ToString() );
Microsoft 22
Building connection strings
Microsoft 23
Step 2: retrieve records
OleDbDataReader dbReader;
dbReader = dbCmd.ExecuteReader();
Microsoft 24
Step 3: close connection
dbReader.Close();
dbConn.Close();
Microsoft 25
Guaranteed close?
try {
dbConn = new OleDbConnection(sConnection);
dbConn.Open();
.
.
.
}
catch(Exception ex) {
System.Diagnostics.Debug.WriteLine("Call stack: " + ex.StackTrace);
System.Diagnostics.Debug.WriteLine("DB Error: " + ex.Message);
throw ex;
}
finally {
if (dbReader != null) dbReader.Close();
if ((dbConn != null) && (dbConn.State != ConnectionState.Closed))
dbConn.Close();
}
Microsoft 26
Part 3
• Modifying a database…
Microsoft 27
Action queries
Update Customers
Set CreditLimit = 40000000000.0, Balance = 0.0
Where LastName = 'Gates' and FirstName = 'Bill';
Microsoft 28
Example #1
• Example:
– delete selected customer…
c = (Customer) this.listBox1.SelectedItem;
sql = String.Format("Delete From Customers Where CID={0};", c.ID);
OleDbCommand dbCmd;
dbCmd = new OleDbCommand();
dbCmd.CommandText = sql;
dbCmd.Connection = dbConn;
dbConn.Open();
result = dbCmd.ExecuteNonQuery(); // perform deletion
dbConn.Close();
if (result != 1)
throw new Exception("Query ran, but failed to delete customer?");
c = (Customer) this.listBox1.SelectedItem;
.
. // execute as before…
.
Microsoft 30
Summary
Microsoft 31