Professional Documents
Culture Documents
NET
By Sunil Mallya
Microsoft Student Champ
VI SEM,CS
PESIT
Mail:
sunil.mallya@yahoo.com
Agenda
Why a new data access technology ?
ADO.NET Overview.
ADO.NET object model
.NET data providers
Datasets
Why not ADO ???
ADO was not built to work with
XML data, though some versions
of ADO have added XML
features.
Contents of multiple Recordset
objects cannot be combined.
Does not provide support to
submit changes via stored
procedures.
ADO.NET
dataTable = dataSet.Tables[0];
listBox1.Items.Clear();
}
Show in List Box
private void FillForm()
{
txtPhonenum.Text = Phonenum;
txtSubscriber.Text = Subscriber;
}
private void listBox1_SelectedIndexChanged
{
DataRow sourceRow =
dataTable.Rows[listBox1.SelectedIndex];
LoadBuffers(sourceRow);
FillForm();
BuildCommands()
// Use the select command's connection again
OleDbConnection connection =
(OleDbConnection)dataAdapter.SelectCommand.Connection;
dataAdapter.InsertCommand.CommandText =
"insert into PhoneNumbers " +
"(Phonenum, Subscriber) " +
"values " +
"(?, ?)";
dataAdapter.InsertCommand.Parameters.Add("Phonenum",
OleDbType.Char, 0, "Phonenum");
Declare a reusable update
command with parameters
dataAdapter.UpdateCommand =
connection.CreateCommand();
dataAdapter.UpdateCommand.CommandText =
"update PhoneNumbers " +
"set Subscriber = ? " +
"where Phonenum = ? ";
dataAdapter.UpdateCommand.Parameters.Add("S
ubscriber", OleDbType.Char, 0, "Subscriber");
dataAdapter.UpdateCommand.Parameters.Add("P
honenum", OleDbType.Char, 0, "Phonenum");
Declare a reusable delete
command with parameters
dataAdapter.DeleteCommand =
connection.CreateCommand();
dataAdapter.DeleteCommand.Comm
andText =
"delete from
PhoneNumbers where Phonenum = ?
";
dataAdapter.DeleteCommand.Parame
ADD ENTRY INTO DATABASE
newRow["Phonenum"] = txtPhonenum.Text.Trim();
newRow["Subscriber"] = txtSubscriber.Text.Trim();
// update the database
try
{
dataSet.Tables["PhoneNumbers"].Rows.Add(newRow);
dataAdapter.Update(dataSet, "PhoneNumbers");
dataSet.AcceptChanges();
// inform the user
MessageBox.Show("Updated.");
Application.DoEvents();
// refresh the listbox
Fill_lb();
}
Add cont…
catch (OleDbException ex)
{
dataSet.RejectChanges();
MessageBox.Show(ex.Message);
MessageBox.Show(ex.ErrorCode.ToSt
ring());
}
DELETE RECORDS
// fetch the selected row in the listbox
DataRow selectedRow =
dataTable.Rows[listBox1.SelectedIndex];
Application.DoEvents();
// Look at each table in the dataSet
foreach (DataTable currTable in dsChanges.Tables)
{
// Find the rows with errors if any table has errors
if (currTable.HasErrors)
{
// fetch the error rows
DataRow[] errorRows = currTable.GetErrors();
// No errors -- all okay
if (okayFlag)
{
// apply updates to the database
dataAdapter.Update(dsChanges, "PhoneNumbers");
Sunil Mallya
Mail to :
sunil.mallya@yahoo.com