Basic ADO.NET operations

Code and instruuction on the common database tasks of Insert, Update and Delete in ADO.NET.

Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share?

Submit it here!


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(); 

There you go. I needed to do this for a particular project I was working on, and I couldn't find a single working code on the Internet. My colleagues and I had to spend a few hours to do this. I hope this will save your time. Happy coding!


Source: DotNetExtreme.com
This was first published in November 2002

Dig deeper on ADO.NET development

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close