Professional Documents
Culture Documents
NET Basics
Course Instructor :Saadia Shehryar
Overview
What is ADO.NET?
ADO. Net
ADO.NET is the technology that .NET applications use to
Data Providers
ADO.NET Data Providers are class libraries that allow a common
Data Provider
SQL Server
Namespace
System.Data.SqlClient
OLE DB
System.Data.OleDb
ODBC
System.Data.Odbc
Oracle
System.Data.OracleClient
ADO.NET Objects
.
Object
Object Description
Connection
Command
DataReader
DataAdapter
DataSet
data access..
When you query data with direct data access, you dont
keep a copy of the information in memory. Instead, you
work with it for a brief period of time while the database
connection is open, and then close the connection as soon as
possible.
This is different than disconnected data access, where you
keep a copy of the data in the DataSet object so you can
work with it after the database connection has been closed.
database name, user name, password, and other parameters that are
required for connecting to the data base.
make a database connection. They include the location, name of the database, and security
credentials.
Connection
String Parameter. Name
Description
Data Source
Initial Catalog
Database name.
Integrated Security
User ID
Password
string. For that reason, it usually makes the most sense to store a connection string in
a config file.
The <connectionStrings> section of the web.config file is a handy place to store
your connection strings. Heres an example:
<configuration> <connectionStrings>
<add name="Pubs" connectionString=
"Data Source=localhost;Initial Catalog=Pubs;Integrated
Security=SSPI"/>
</connectionStrings>
</configuration>
try{
conn.Open();
..
}
catch (SqlConnection ex){ ..}
finally {
conn.Close(); }
than the way you instantiate other ADO.NET objects.You must call
ExecuteReader on a command object, like this:
SqlDataReader rdr = cmd.ExecuteReader();
The ExecuteReader method of the SqlCommand object, cmd returns
a SqlDataReader instance.
Once you have the reader, you retrieve a single row at a time
SqlDataReader: ReadingData
The typical method of reading from the data stream returned by
the SqlDataReader is to iterate through each row with a while loop.
The DataReader
As soon as youve finished reading all the results you
method
ExecuteNonQuery
To execute operations where database is changed
Example: insert, update, delete
Int i = cmd.ExecuteNonQuery();
Updates the Database table and returns the number of rows affected
CategoryName = 'Other'";
// 1. Instantiate a new command
SqlCommand cmd = new SqlCommand(deleteString,
conn);
// 4. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
Updates the Database table and returns the number of rows
affected
Finally
// close the reader
if (rdr != null)
try
{
rdr.Close();
conn.Close();
} } } }
Updating Data
To execute an Update, an Insert, or a Delete statement, you need to
example. Instead of displaying the field values in a single, fixed label, you need to
show each detail in a separate text box. Figure shows the new page. It includes two
new buttons that allow you to update the record (Update) or delete it (Delete), and
two more that allow you to begin creating a new record (Create New) and then
insert it (Insert New).
Updating a Record
When the user clicks the Update button, the information in the text boxes is applied
Updating a Record
cmd.Parameters.AddWithValue("@address", txtAddress.Text);
cmd.Parameters.AddWithValue("@city", txtCity.Text);
cmd.Parameters.AddWithValue("@state", txtState.Text);
cmd.Parameters.AddWithValue("@zip", txtZip.Text);
cmd.Parameters.AddWithValue("@contract",
Convert.ToInt16(chkContract.Checked));
cmd.Parameters.AddWithValue("@au_id_original",
lstAuthor.SelectedItem.Value);
Adding a Record
To start adding a new record, click Create New to clear all the text boxes.
Technically this step isnt required, but it simplifies the users life:
protected void cmdNew_Click(Object sender, EventArgs e)
{
txtID.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
txtPhone.Text = "";
txtAddress.Text = "";
txtCity.Text = "";
txtState.Text = "";
txtZip.Text = "";
chkContract.Checked = false;
lblStatus.Text = "Click Insert New to add the completed record.";
}
Deleting a Record
When the user clicks the Delete button, the author information
Deleting a Record
cmd.Parameters.AddWithValue("@au_id ",
lstAuthor.SelectedItem.Value);
// Try to open the database and delete the record.
int deleted = 0;
Try {
con.Open();
deleted = cmd.ExecuteNonQuery();
}
catch (Exception err)
{ lblStatus.Text = "Error deleting author. ";
lblStatus.Text += err.Message; }
Finally {
con.Close(); }
You fill the DataSet in much the same way that you connect a DataReader.
newpage.aspx.
Response.Redirect(newpage.aspx");
When you use the Redirect() method, ASP.NET immediately
stops processing the page and sends a redirect message back to
the browser. Any code that occurs after the Redirect() call wont
be executed. When the browser receives the redirect message, it
sends a request for the new page.