Data In Asp.Net 2.0 :: InsertIng,
in ASP.NET 2.0 section of the ASP.NET site at http://www.asp.net/learn/dataaccess/default.aspx.
Working with Data in ASP.NET 2.0 :: Inserting,
Updating, and Deleting Data with the SqlDataSource
Introduction
As discussed inAn Overview of Inserting, Updating, and Deleting, the GridView control provides builtin
updating and deleting capabilities, while the DetailsView and FormView controls include inserting support along
with editing and deleting functionality. These data modification capabilities can be plugged directly into a data
source control without a line of code needing to be written. An Overview of Inserting, Updating, and Deleting examined using the ObjectDataSource to facilitate inserting, updating, and deleting with the GridView,
DetailsView, and FormView controls. Alternatively, the SqlDataSource can be used in place of the
ObjectDataSource.
Recall that to support inserting, updating, and deleting, with the ObjectDataSource we needed to specify the object layer methods to invoke to perform the insert, update, or delete action. With the SqlDataSource, we need to provide
INSERT, UPDATE, and DELETE SQL statements (or stored procedures) to execute. As we’ll see in this tutorial, these
statements can be created manually or can be automatically generated by the SqlDataSource’s Configure Data
Source wizard.
Note:Since we’ve already discussed the inserting, editing, and deleting capabilities of the GridView, DetailsView,
and FormView controls, this tutorial will focus on configuring the SqlDataSource control to support these
operations. If you need to brush up on implementing these features within the GridView, DetailsView, and
FormView, return to the Editing, Inserting, and Deleting Data tutorials, starting with An Overview of Inserting,
Updating, and Deleting.
Step 1: Specifying INSERT, UPDATE, and DELETE Statements
As we’ve seen in the past two tutorials, to retrieve data from a SqlDataSource control we need to set two
properties:
1. ConnectionString, which specifies what database to send the query to, and
2. SelectCommand, which specifies the adhoc SQL statement or stored procedure name to execute to return the
results.
For SelectCommand values with parameters, the parameter values are specified via the SqlDataSource’s
SelectParameters collection and can include hardcoded values, common parameter source values (querystring
fields, session variables, Web control values, and so on), or can be programmatically assigned. When the
SqlDataSource control’s Select() method is invoked – either programmatically or automatically from a data Web
control –a connection to the database is established, the parameter values are assigned to the query, and the
command is shuttled off to the database. The results are then returned as either a DataSet or DataReader, depending
on the value of the control’s DataSourceMode property.
Along with selecting data, the SqlDataSource control can be used to insert, update, and delete data by supplying
INSERT, UPDATE, and DELETE SQL statements in much the same way. Simply assign the InsertCommand,
UpdateCommand, and DeleteCommand properties the INSERT, UPDATE, and DELETE SQL statements to execute. If
the statements have parameters (as they most always will), include them in the InsertParameters,
UpdateParameters, and DeleteParameters collections.
1 of 12
Once an InsertCommand, UpdateCommand, or DeleteCommand value has been specified, the “Enable Inserting”,
“Enable Editing”, or “Enable Deleting” option in the corresponding data Web control’s smart tag will become
available. To illustrate this, let’s take an example from the Querying.aspx page we created in the Querying Data
with the SqlDataSource Control tutorial and augment it to include delete capabilities. ...
Regístrate para leer el documento completo.