0% found this document useful (0 votes)
29 views

Lec Note On Chapter 5 - Database Programming

Uploaded by

beshahashenafi32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lec Note On Chapter 5 - Database Programming

Uploaded by

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

ADO.

Net Database Programming


in C#
Overview
• .Net platform defined - a number of
namespaces to interact with local/ remote
database.
– Called ADO.Net
• Support – numerous data provider
– Allow to interact with specific DBMS ( SQL server,
Access, Oracle, MySql,…) .
• ADO.Net is successor of ADO(classic)
ADO.Net
• Designed – considering to work both- disconnected
and connection world.
• Support – both connected and disconnected world
• Deeply support for XML
– Data source serialized to XML (standard data rep. format)
• XML – transport between layer using standard http
• ADO (classic) – designed for tightly coupled c/s
system.
• Support multiple data provider – no need of
intermediate layer
The Core Objects of an ADO.NET Data Provider
The Core Objects continued…
Microsoft ADO.NET Data Providers
Data Providers
• MS SQL Server 7.0+
• Oracle
• OLE DB (old SQL & Access- Jet 4.0)
• Open Database Connectivity (ODBC)-
earlier Visual Studio, Access Driver, ODBC
for Oracle
ADO.Net Architecture
Architecture Layers
App User App UI Application
WinForms, Swing

Data Objects …
Application ADO, JDBC

ODBC, OleDB, JDBC


DB API
OS DB Driver …
SQLserver, Jet

Internet, local

DB Admin DBMS UI DB Engine


SQLserver, Access


Database
Database
ADO.net

• OleDB, ODBC, SQLdb, … DB


• Steps to get data:
1. dbConnection: connect to DB
2. dbCommand: SQL query text
Alternative: DataReader,
3. dbAdapter: executes query retrieve data incrementally
4. DataSet: resulting data
• Steps to display data:
• Bind to UI control, e.g. DataGrid
• or Manual data processing
C# DB Example
• Get data:
Using System.Data.OleDb;// “Jet” = MS Access DB driver
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/test.mdb”);
cmd = new OleDbCommand("SELECT * FROM table1”, con); // SQL query
adpt = new OleDbDataAdapter(cmd);
data = new DataSet( );
adpt.Fill(data); // execute the query and put result in ‘data’

• Display data:
dataGrid1.DataSource = data.Tables[0]; // show the table in the grid control
MessageBox.Show(data.Tables[0].Rows[0][5].ToString( )); // or process manually, this is row 0
col 5
DataSet vs. DataReader
DataSet:
• Like 2D array: a[r][c] DataReader: (standard)
• Full dataset read from DB at • 1 row at a time
query execution time • No local memory storage
• Dataset cached locally in • Fetch each row from DB on
mememory. demand
• Can disconnect from DB • “cursor” = current row
• Implemented using • Must stay connected to DB
DataReader/ DataAdapter • DataReader
• - scalability
• + random data access • + each row
• + Limited scalability • - random access difficult
• - initial read very slow
Object Model
DataSet:
DataReader: (standard)
• Tables[n]
• Columns[c] (current row)
– Rows[r]
– Value, type, …
• columns[c]
– Value, type, … • Columns info [c]
– Columns info [c] – Name, type, …
• Name, type, … • Cursor:
– moveNext, MovePrev,
moveTo
– EOF
Cursors
• Forward only - (forward/back vs. random
access)
• Read only vs. writeable
• …
Connections
• Connection strings:
• Con.open( )
• Queries here
• Con.close( ) // connections consume
resources

• Adapter.Fill( ) does open/close automatically


Commands (Queries)
• Command Types:
• SQL Query:
» Relation: SELECT
» Scalar: SELECT that returns 1 row, 1 col
» Non-query: INSERT, UPDATE, DELETE
• Table name
• View name
• Rdr = Cmd.ExecuteReader( );
Query Parameters
• Want to substitute a parameter value into a query
• Dynamic SQL query construction:
cmd = new OleDbCommand(
"SELECT * FROM table WHERE myAttr = " + myValue, con);

• Parameterized query: (more robust, reusable)


cmd = new OleDbCommand(
"SELECT * FROM table WHERE myAttr = ?", con); // ? = parameter
cmd.Parameters.Add(“?”, myValue); // parameter value

• Or, put param query in DB as view / stored procedure:


cmd = new OleDbCommand("MyStoredQuery", con); // name of view
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("?", myValue); // parameter value
DataBound UI Controls
• Display a table:
• DataGrid
DataGrid control
• Display a column: -scroll, sort, edit, …
• DataList
• listBox
• ComboBox
• Display a cell value: (of current row)
• TextBox
• Can bind any property of any UI control to any DB
column
Manual data processing

foreach(DataRow r in dataSet1.Tables[0].Rows) // for each row
{
doSomethingWith(r[“columnName”]); //or:
foreach(Object v in r.ItemArray) // for each column
doSomethingWith(v);
}


Saving Data Changes
• Manual update/insert/delete queries:
cmd = new OleDbCommand(“UPDATE table SET myAttr=value WHERE id=idval”, con);
cmd.ExecuteNonQuery(); // query does not return data.

• Adapters and bound UI Controls:


• User can edit in DataGrid, etc.
• Writes DataSet changes to DB on Update( ) method
• Must have param update/insert/delete commands in
Adapter
cmd = new OleDbCommand("UPDATE table SET attr1=?, attr2=? WHERE id=?", con);
cmd.Parameters.Add(new OleDbParameter(“?”, …, “attr1”, DataRowVersion.Current ));
cmd.Parameters.Add(new OleDbParameter(“?”, …, “attr2”, DataRowVersion.Current ));
cmd.Parameters.Add(new OleDbParameter(“?”, …, “id”, DataRowVersion.Original ));
adpt.UpdateCommand = cmd;

adpt.Update(data); // analogous to adpt.Fill(data);

You might also like