You are on page 1of 20

Microsoft Visual Studio -2008 ADO.

NET Database Technology

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

DA0- Data Access Object RDO- Remote Data Object ADO- ActiveX Data Object
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

ADO.NET - Database Technology


This technology takes its name from ADO(ActiveX Data Objects) New ADO.Net is designed to overcome the limitation of ADO technology. It deals with accessing and manipulating databases. It comprises of many namespaces and classes to do so. ADO.Net provides accesses to data source such as Microsoft SQL Server, OLE-DB and XML etc. ADO technology supports only Connected approach. But new ADO.Net supports both a connected and disconnected approach.

I. Connected approach II. Disconnected approach

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

ADO.Net has two major components


1. The .Net Data providers 2. The DataSet
A .Net Data Provider is used for connecting to a database, executing commands, and retrieving results. Using the .Net Data Providers, we can either access database directly or use the disconnected approach, in disconnected approach we use DataSet class.

The .Net Data Providers - It has following basic objects.


1. A Connection Object

2. A Command Object 3. A DataReader Object 4. A DataAdapter Object


4

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

The .Net Data Provider objects


The Connection Object is used to connected to the data source. Data source can be any database file. The Connection object contains information like the provider name, server name, data source name, user name, password and so on. A Command Object is used to connect the Connection Object to a dataReader or DataAdaptor Object. The Command Object allows us to execute an SQL statement or a stored procedure in a data source. The DataReader Object is used to read the data in a first an efficient manner from the database. It is generally used to extract one or a few records or specific field values, or to execute simple SQL statements. A DataAdaptor Object is used to fill data from the database into the DataSet Object. The DataSet is used in the disconnected approach.
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

Comparison DataReader & DataSet


DataSet DataReader Advantages Limitations Its magnitude is closer to the It is singly database It is forward only, provides sequential It offers index based access access It allows update ,delete also It is read only Always works in disconnected mode Can read data from xml file too Always works in connected mode Limitations Not xml enabled Slow Advantages Each piece of data in the DataSet is Fast by default of type object. Each piece of data in the DataReader is of specific type .

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

Connection Object

Command Object

Data Adapter

Data Set Data Source

ADO.Net Architecture Data Reader Command Object

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

Table[0] Table[1] Table[2]

name

age

address

phone

DataSet Virtual Database It temporarily stores Data in a disconnected approach for Database Management.

DataSet
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

DbAccess class for Database Management(Ms SQL Server)


using System; using System.Data; using System.Data.SqlClient; namespace <FileName> { class DbAccess { static SqlConnection con = new SqlConnection(@ Connection String"); // Method use to execute SELECT command from the data source public static DataSet FetchData(string Query) { SqlDataAdapter da = new SqlDataAdapter(Query, con); DataSet ds = new DataSet(); da.Fill(ds); return ds; }
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

// Method use to execute Non-Query(INSERT / UPDATE / DELETE )records public static bool SaveData(string Query) { try { SqlCommand cmd = new SqlCommand(Query, con); con.Open(); cmd.ExecuteNonQuery(); return true; } catch (Exception ex) { return false; } finally { con.Close(); } }
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

10

// Method use to obtain a single value from the data source public static string FetchScalar(string Query) { SqlCommand cmd = new SqlCommand(Query, con); string s; try { con.Open(); s = Convert.ToString(cmd.ExecuteScalar()); return s; } catch (Exception ex) { return ; } finally { con.Close(); } } } //End of DbAccess class. } //End of the namespace.
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

11

OleDb Data Provider


It help us to connect to Ms Access Database. For using OleDb Data Provider 1. Change the namespace to - using System.Data.OleDb; 2. Change the DataProvider Object name with OleDbConnection, OleDbAdapter , OleDbCommand etc.

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

12

Windows Data Form

Message:

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

13

//Event to Add Record in a Table. protected void Button_Add_Click(object sender, EventArgs e) { bool r = DbAccess.SaveData("INSERT INTO <TableName> VALUES ( "+ name.Text + ',"+ age.Text + , +address.Text+ , +phone.Text+ ) "); if (r == true) Message.Text = "Data Successfully Added"; else Message.Text = "Data Can t be Added"; }

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

14

//Event to Update Record in a Table. protected void Button_Update_Click(object sender, EventArgs e) { bool r = DbAccess.SaveData("UPDATE <TableName> SET Name= " +name.Text + ',Age="+ age.Text + , Address = +address.Text+ WHERE PhoneNo="+phone.Text +" "); if (r) Message.Text = "Data Updated"; else Message.Text = Data Can,t be Updated"; }
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

15

//Event to Delete Record in a Table.

protected void Button_Delete_Click(object sender, EventArgs e) { bool r = DbAccess.SaveData("DELETE FROM <TableName> WHERE PhoneNo= " + phone.Text + "); if (r) Message.Text = "Data Deleted"; else Message.Text = Data can t be Deleted"; }

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

16

XML Extensive Markup Language


XML use tag like HTML, but it is user define, not pre define tag like HTML. It is use to store static Data which is not frequently change. It can be use with Mobile or other small application where Database is very small. ADO.Net provides facilities to use XML as Data source for an application developed under Visual Studio-2008. XML file is a plain text file can be created on any editor, having extension as .xml.

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

17

Sample XML File


<?xml version="1.0" encoding="utf-8" ?> <contacts> - <contact> <Name>Sunil Kr Chakrabarty</Name> <Address>Jadavpur, Kolkata - 700032</Address> <Telephone>24725127</Telephone> </contact> - <contact> <Name>Ashish Kr Chakrabarty</Name> <Address>Sonarpur, Kolkata - 700150</Address> <Telephone>9836033925</Telephone> </contact> - <contact> <Name>Arjun Kr Chakrabarty</Name> <Address>Pratapgarh, Kolkata - 700103</Address> <Telephone>64583500</Telephone> </contact> </contacts>
Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

18

Summary
The Disconnected data architecture in ADO.NET is more flexible, efficient than it is in ADO, some of the major issues are ADO.NET being built on XML architecture and its size. DataAdapter is the back bone of implementing the disconnected data architecture XML data can be read into DataSet at the same time Data in the DataSet can be written onto the file in the form of XML Document. DataSet can also be validated against xml schemas

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

19

Thank You For Listening Me

Developed By: Ashish Kr Chakrabarty, ARDENT COMPUTECH PVT LTD

20

You might also like