download

Mata kuliah : M0874 – Programming II
Tahun
: 2010
Database, SQL, and ADO .NET- Part 2
Session 12
Outline Materi
•Programming with ADO .NET: Extracting
Information from a Database
•Programming with ADO .NET: Modifying a
Database
Bina Nusantara University
3
The SqlCommand Object
•A SqlCommand object allows you to specify what type of interaction you
want to perform with a database.
•For example, you can do select, insert, modify, and delete commands on
rows of data in a database table.
•Similar to other C# objects, you instantiate a SqlCommand object via the
new instance declaration, as follows:
SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);
4
Querying Data
•Instantiate a new command with a query and
connection
SqlCommand cmd = new SqlCommand("select
CategoryName from Categories", conn);
•Call Execute reader to get query results
SqlDataReader rdr = cmd.ExecuteReader();
Bina Nusantara University
5
Inserting Data
•Prepare command string
string insertString = @"
insert into Categories
(CategoryName, Description)
values ('Miscellaneous', 'Whatever doesn''t fit elsewhere')";
•Instantiate a new command with a query and
connection
SqlCommand cmd = new SqlCommand(insertString, conn);
•Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
Bina Nusantara University
6
Updating Data
•Prepare command string
string updateString = @"
update Categories
set CategoryName = 'Other'
where CategoryName = 'Miscellaneous'";
•Instantiate a new command with command text only
SqlCommand cmd = new SqlCommand(updateString);
•Set the Connection property
cmd.Connection = conn;
•Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
7
Deleting Data
•Prepare command string
string deleteString = @"
delete from Categories
where CategoryName = 'Other'";
•Instantiate a new command
SqlCommand cmd = new SqlCommand();
•Set the CommandText property
cmd.CommandText = deleteString;
•Set the Connection property
cmd.Connection = conn;
•Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
Bina Nusantara University
8
Bina Nusantara University
9
Bina Nusantara University
10
Bina Nusantara University
11
Bina Nusantara University
12
Reading Data with the SqlDataReader
•SqlDataReader objects allow you to read data in a
fast forward-only manner.
•You obtain data by reading each row from the data
stream.
•Call the Close method of the SqlDataReader to
ensure there are not any resource leaks.
Bina Nusantara University
13
Reading Data with the SqlDataReader
14
Bina Nusantara University
15
Working with Disconnected Data –
The DataSet and SqlDataAdapter
•The SqlDataAdapter performs the following tasks
when filling a DataSet with data:
•Open connection
•Retrieve data into DataSet
•Close connection
•Performs the following actions when updating data
source with DataSet changes:
•Open connection
•Write changes from DataSet to data source
•Close connection
Bina Nusantara University
16
Bina Nusantara University
17
Bina Nusantara University
18
References
•http://www.csharpstation.com/Tutorials/AdoDotNet/Lesson01.aspx
Bina Nusantara University
19