SQL statements and stored procedures often include parameters that are evaluated at run time. An SQL statement written with parameters is referred to as a parameterized SQL statement.
When using the
Using Parameters
Like all data source controls, the SqlDataSource control accepts input parameters at run time and manages them in parameter collections. Each data operation has a related parameter collection. For select operations, you can use the
You can specify a name, type, direction, and default value for each parameter. Parameters that get values from a specific object, such as a control, session variable, or the user profile, require you to set additional properties. For example, a
The SqlDataSource control will also automatically create parameters based on values passed by a data-bound control (such as a
Specifying Parameters in Commands
When you use the SqlDataSource control, you can set the control's command properties to parameterized SQL statements or to the name of a stored procedure. If you specify a stored procedure for a command, you must specify that the command type of the command is
Parameter Names
The SqlDataSource control adds the value of the
If a data-bound control such as a GridView control is bound to the SqlDataSource control, during an update or delete operation the data-bound control passes both current and original record values to the SqlDataSource control. The current values are passed in the Values dictionary. The original values are passed in the Keys or OldValues dictionaries. The contents of these dictionaries are appended to the underlying
In the SqlDataSource control's SQL commands, you use a naming convention to match parameter placeholders to the old values passed into the command. You establish the format of the placeholder name by setting the SqlDataSource control's LastModifiedDate
. The current value for the field is passed in the Values dictionary and the original value for the field is passed in the OldValues dictionary. A parameter named @LastModifiedDate
is created to pass the current value and a parameter named @old_LastModifiedDate
is created to pass the original value. You can then include both parameters in an SQL statement to differentiate between the current and original values for the field, as shown in the following example:
В | Copy Code |
---|---|
UPDATE Table1 SET LastModifiedDate = @LastModifiedDate WHERE Key = @Key AND LastModifiedDate = @old_LastModifiedDate |
The ability to separate current and original values in a command is essential when performing optimistic concurrency checks or working with a data source where the primary key can be modified.
For more information on the dictionaries passed by a data-bound control, see How a Data Source Control Creates Parameters for Data-bound Fields.
Using Parameters with the SqlClient Provider
By default, the SqlDataSource control uses the
В | Copy Code |
---|---|
SELECT * FROM Employees WHERE LastName = @LastName AND FirstName = @FirstName |
With named parameters, the order in which the parameters is specified in the command's parameters collection is not important. However, you must ensure that the parameter names that you use in your SQL command correspond to the names of the parameters in the associated collection.
The following example shows how to use named parameters in an SQL command for a SqlDataSource control that uses the System.Data.SqlClient provider.
Visual BasicВ | Copy Code |
---|---|
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID" InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); SELECT @EmpID = SCOPE_IDENTITY()" UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName WHERE EmployeeID=@EmployeeID" DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" OnInserted="EmployeeDetailsSqlDataSource_OnInserted" RunAt="server"> <SelectParameters> <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> </asp:sqlDataSource> |
C#В | Copy Code |
---|---|
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID" InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); SELECT @EmpID = SCOPE_IDENTITY()" UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName WHERE EmployeeID=@EmployeeID" DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" OnInserted="EmployeeDetailsSqlDataSource_OnInserted" RunAt="server"> <SelectParameters> <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> </asp:sqlDataSource> |
Using Parameters with the OleDb and Odbc Providers
If you are connecting to an OLE DB or ODBC data source, you can configure the SqlDataSource control to use the
В | Copy Code |
---|---|
SELECT * FROM Employees WHERE LastName = ? AND FirstName = ? |
When you use the System.Data.OleDb and System.Data.Odbc providers with parameterized SQL statements, the order in which you specify the parameter placeholders must match the order of the parameters in the related parameter collection. You can control the order of parameters by explicitly specifying them in the collection for the related data operation, such as the UpdateParameters collection for the related
Note |
---|
By default, parameters based on bound fields from a data-bound control are added to the command from the parameter dictionaries in the following order: Values, Keys, OldValues. For delete operations, only the Keys dictionary is used. For insert operations, only the Values dictionary is used. For more information on the dictionaries passed by a data-bound control, see How a Data Source Control Creates Parameters for Data-bound Fields. |
The following example shows how to specify parameters for a SqlDataSource control that uses the System.Data.OleDb provider. Parameters are explicitly specified to ensure the order of the parameters in the collection matches the order of the placeholders in the SQL statements.
Visual BasicВ | Copy Code |
---|---|
<Fields> <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" InsertVisible="False" ReadOnly="true"/> <asp:BoundField DataField="FirstName" HeaderText="First Name"/> <asp:BoundField DataField="LastName" HeaderText="Last Name"/> <asp:TemplateField HeaderText="Birth Date"> <ItemTemplate> <asp:Label ID="BirthDateLabel" Runat="Server" Text='<%# Eval("BirthDate", "{0:d}") %>' /> </ItemTemplate> <InsertItemTemplate> <asp:Calendar ID="InsertBirthDateCalendar" Runat="Server" SelectedDate='<%# Bind("BirthDate") %>' /> </InsertItemTemplate> <EditItemTemplate> <asp:Calendar ID="EditBirthDateCalendar" Runat="Server" VisibleDate='<%# Eval("BirthDate") %>' SelectedDate='<%# Bind("BirthDate") %>' /> </EditItemTemplate> </asp:TemplateField> </Fields> |
C#В | Copy Code |
---|---|
<Fields> <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" InsertVisible="False" ReadOnly="true"/> <asp:BoundField DataField="FirstName" HeaderText="First Name"/> <asp:BoundField DataField="LastName" HeaderText="Last Name"/> <asp:TemplateField HeaderText="Birth Date"> <ItemTemplate> <asp:Label ID="BirthDateLabel" Runat="Server" Text='<%# Eval("BirthDate", "{0:d}") %>' /> </ItemTemplate> <InsertItemTemplate> <asp:Calendar ID="InsertBirthDateCalendar" Runat="Server" SelectedDate='<%# Bind("BirthDate") %>' /> </InsertItemTemplate> <EditItemTemplate> <asp:Calendar ID="EditBirthDateCalendar" Runat="Server" VisibleDate='<%# Eval("BirthDate") %>' SelectedDate='<%# Bind("BirthDate") %>' /> </EditItemTemplate> </asp:TemplateField> </Fields> |
See Also
Reference
Concepts
Selecting Data Using the SqlDataSource ControlModifying Data using the SqlDataSource Control