SqlDataSourceView.Update(IDictionary, IDictionary, IDictionary) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用 UpdateCommand SQL 字符串、UpdateParameters 集合中的所有参数,以及指定的 keys、values 和 oldValues 集合中的值执行更新操作。
public:
int Update(System::Collections::IDictionary ^ keys, System::Collections::IDictionary ^ values, System::Collections::IDictionary ^ oldValues);
public int Update(System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues);
override this.Update : System.Collections.IDictionary * System.Collections.IDictionary * System.Collections.IDictionary -> int
Public Function Update (keys As IDictionary, values As IDictionary, oldValues As IDictionary) As Integer
参数
- keys
- IDictionary
与 IDictionary 属性一起使用以执行更新数据库操作的主键的 UpdateCommand。 如果不存在与查询关联的关键字,或者如果 UpdateCommand 不是参数化 SQL 查询,则传递 null。
- values
- IDictionary
与 IDictionary 属性一起使用以执行更新数据库操作的值的 UpdateCommand。 如果不存在与查询关联的参数,或者如果 UpdateCommand 不是参数化 SQL 查询,则传递 null。
- oldValues
- IDictionary
IDictionary,表示数据库中的原始值。 如果不存在与查询关联的参数,或者如果 UpdateCommand 不是参数化 SQL 查询,则传递 null。
返回
一个值,该值表示基础数据库中被更新的行数。
例外
SqlDataSource 无法与基础数据源建立连接。
CanUpdate 属性为 false。
示例
下面的代码示例演示如何使用 控件 SqlDataSource 显示控件中的数据 DropDownList ,并在单击“ 提交 ”按钮时更新数据。 属性 UpdateCommand 使用参数化 SQL 语句设置,并将两 ControlParameter 个参数添加到集合中 UpdateParameters 。 单击“ 提交 ”按钮时,将 OnClick 处理 事件以显式调用 Update 方法。
<%@Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void On_Click(Object source, EventArgs e) {
try {
SqlDataSource1.Update();
}
catch (Exception except) {
// Handle the Exception.
}
Label2.Text="The record was updated successfully!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID, LastName, Address FROM Employees"
UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID">
<UpdateParameters>
<asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
<asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
<asp:DropDownList
id="DropDownList1"
runat="server"
DataTextField="LastName"
DataValueField="EmployeeID"
DataSourceID="SqlDataSource1">
</asp:DropDownList>
<br />
<asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
AssociatedControlID="TextBox1" />
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />
<br /><asp:Label id="Label2" runat="server" Text="" />
</form>
</body>
</html>
<%@Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub On_Click(ByVal source As Object, ByVal e As EventArgs)
Try
SqlDataSource1.Update()
Catch except As Exception
' Handle the Exception.
End Try
Label2.Text="The record was updated successfully!"
End Sub 'On_Click
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID, LastName, Address FROM Employees"
UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID">
<UpdateParameters>
<asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
<asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
<asp:DropDownList
id="DropDownList1"
runat="server"
DataTextField="LastName"
DataValueField="EmployeeID"
DataSourceID="SqlDataSource1">
</asp:DropDownList>
<br />
<asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
AssociatedControlID="TextBox1" />
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />
<br /><asp:Label id="Label2" runat="server" Text="" />
</form>
</body>
</html>
注解
方法 Update 调用 ExecuteUpdate 方法,并 keys传递 、 values和 oldValues 参数。 有关详细信息,请参阅 SqlDataSource.UpdateCommand。