Tip

Basic ADO.NET operations

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.