Basic ADO.NET operations

Let's see how we can perform the common database tasks like Insert, Update and Delete with ADO.NET. We'll be using the ADO.NET Data Adapter to perform the following tasks. Let's see how it is done.

The Database table has two fields, ID and Name.


Insert a new row

// the Database string
string sDBstr = "Provider=Microsoft.Jet.OLEDB.4.0;
DataSource=c:test.mdb";

//the sql statement
string sSQL = "SELECT * from Name"; 

//the connection object
OleDbConnection oCn = new OleDbConnection(sDBstr); < /FONT >

//open the connection
oCn.Open(); 

//create the data adapter and execute the query
OleDbDataAdapter oDA = new OleDbDataAdapter(sSQL,oCn); < /FONT >

//create the DataSet
DataSet oDs = new DataSet(); < /FONT >

//Fill the dataset with the data adapter
oDA.Fill(oDs,"Name"); 

//create the Data Row
DataRow oDR = oDs.Tables["Name"].NewRow(); < /FONT >

//Populate the datarow with values
oDR["Name"] = "Yateen" 

oDR["Id"] = "10"; 

//Add the datarow to the dataset
oDs.Tables["Name"].Rows.Add(oDR); 

//Use the Command Bulder object to generate Insert Command dynamically
OleDbCommandBuilder oCB = new OleDbCommandBuilder(oDA); < /FONT >

//Update the DB with values from Dataset with the Data Adapter
oDA.Update(oDs,"Name"); 

//Clean Up
oDA.Dispose(); 

oDs.Dispose();

oCB.Dispose();

oCn.Dispose();

For the Update and the Delete operations to work, the database table you are working with requires a Primary Key to be present.


Update a row

// the Database string
string sDBstr = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:test.mdb";

//the sql statement
string sSQL = "SELECT * from Name";

//the connection object
OleDbConnection oCn = new OleDbConnection(sDBstr); 

//open the connection
oCn.Open(); 

//create the data adapter and execute the query
OleDbDataAdapter oDA = new OleDbDataAdapter(sSQL,oCn); < /FONT >

//create the DataSet
DataSet oDs = new DataSet(); < /FONT > 

//Fill the dataset with the data adapter
oDA.Fill(oDs,"Name");

//create the Data Row assigning it to the row you want to update
DataRow oDR = oDs.Tables["Name"] .Rows[3];

//Populate the datarow with values
oDR["Name"] = "Yateen"

//Use the Command Bulder object to generate Update Command dynamically
OleDbCommandBuilder oCB = new OleDbCommandBuilder(oDA); < /FONT >

//Update the DB with values from Dataset with the Data Adapter
oDA.Update(oDs,"Name"); 

//Clean Up
oDA.Dispose();

oDs.Dispose(); 

oCB.Dispose(); 

oCn.Dispose(); 


Delete a row

// the Database string
string sDBstr = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:test.mdb"; 

//the sql statement
string sSQL = "SELECT * from Name";

//the connection object
OleDbConnection oCn = new OleDbConnection(sDBstr); < /FONT >

//open the connection
oCn.Open(); 

//create the data adapter and execute the query
OleDbDataAdapter oDA = new OleDbDataAdapter(sSQL,oCn); < /FONT >

//create the DataSet
DataSet oDs = new DataSet(); <. /FONT >

//Fill the dataset with the data adapter
oDA.Fill(oDs,"Name"); 

//Delete the row u want to delete
oDs.Tables["Name"].Rows[3].Delete();

//Use the Command Bulder object to generate Delete Command dynamically
OleDbCommandBuilder oCB = new OleDbCommandBuilder(oDA); < /FONT > 

//Update the DB with values from Dataset with the Data Adapter
oDA.Update(oDs,"Name"); 

//Clean Up
oDA.Dispose(); 

oDs.Dispose(); 

oCB.Dispose(); 

oCn.Dispose(); 

 

Copyright 2002 - 2003 XOCOMP, llc
Contact: md3@protocolla.com or md3@xocomp.net after March 2003

All materials and notes are the intellectual property of XOCOMP with expressed permission for use by PROTOCOL.