SqlDataSource.UpdateCommand 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 SqlDataSource 控件更新基础数据库中的数据所用的 SQL 字符串。
public:
property System::String ^ UpdateCommand { System::String ^ get(); void set(System::String ^ value); };
public string UpdateCommand { get; set; }
member this.UpdateCommand : string with get, set
Public Property UpdateCommand As String
属性值
SqlDataSource 更新数据所用的 SQL 字符串。
示例
本部分包含两个代码示例。 第一个代码示例演示如何设置 UpdateCommandGridView 控件的 SqlDataSource 属性,并使用 控件更新 Microsoft SQL Server 数据库中的数据。 第二个代码示例演示如何使用 GridView 控件更新 ODBC 数据库中的数据。
下面的代码示例演示如何设置 UpdateCommandGridView 控件的 SqlDataSource 属性,并使用 控件更新 SQL Server 数据库中的数据。 GridView自动填充UpdateParameters集合,从 BoundField 对象推断参数,并在选择可GridView编辑对象上的“更新”链接时调用 Update 方法。 此示例还包括一些后处理:更新记录后,将发送通知电子邮件。
<%@Page Language="C#" %>
<%@Import Namespace="System.Web.Mail" %>
<!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 OnDSUpdatedHandler(Object source, SqlDataSourceStatusEventArgs e) {
if (e.AffectedRows > 0) {
// Perform any additional processing,
// such as setting a status label after the operation.
Label1.Text = Request.LogonUserIdentity.Name +
" changed user information successfully!";
}
else {
Label1.Text = "No data updated!";
}
}
</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"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
UpdateCommand="Update Employees SET FirstName=@FirstName,LastName=@LastName,Title=@Title WHERE EmployeeID=@EmployeeID"
OnUpdated="OnDSUpdatedHandler">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="EmployeeID"
AutoGenerateEditButton="True"
DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:BoundField HeaderText="Title" DataField="Title" />
</columns>
</asp:GridView>
<asp:Label
id="Label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
<%@Page Language="VB" %>
<%@Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub OnDSUpdatedHandler(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
If e.AffectedRows > 0 Then
' Perform any additional processing,
' such as setting a status label after the operation.
Label1.Text = Request.LogonUserIdentity.Name & _
" changed user information successfully!"
Else
Label1.Text = "No data updated!"
End If
End Sub 'OnDSUpdatedHandler
</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"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
UpdateCommand="Update Employees SET FirstName=@FirstName,LastName=@LastName,Title=@Title WHERE EmployeeID=@EmployeeID"
OnUpdated="OnDSUpdatedHandler">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="EmployeeID"
AutoGenerateEditButton="True"
DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:BoundField HeaderText="Title" DataField="Title" />
</columns>
</asp:GridView>
<asp:Label
id="Label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
下面的代码示例在功能上与前面的代码示例相同,演示如何使用 GridView 控件更新 ODBC 数据库中的数据。 设置为 ProviderName ODBC 的 ADO.NET 提供程序, System.Data.Odbc属性 ConnectionString 设置为 ODBC 数据源名称 (DSN) 。
<%@Page Language="C#" %>
<%@Import Namespace="System.Web.Mail" %>
<!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 OnDSUpdatedHandler(Object source, SqlDataSourceStatusEventArgs e) {
if (e.AffectedRows > 0) {
// Perform any additional processing, such as sending an email notification.
Label1.Text = Request.LogonUserIdentity.Name +
" changed user information successfully!";
}
else {
Label1.Text = "No data updated!";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<!-- This example uses a Northwind database that is hosted by an ODBC-compliant
database. To run this sample, create an ODBC DSN to any database that hosts
the Northwind database, including Microsoft SQL Server or Microsoft Access,
change the name of the DSN in the ConnectionString, and view the page.
-->
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ProviderName="System.Data.Odbc"
DataSourceMode="DataSet"
ConnectionString="dsn=myodbc3dsn;"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
UpdateCommand="Update Employees SET FirstName=?,LastName=?,Title=? WHERE EmployeeID=?"
OnUpdated="OnDSUpdatedHandler">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="EmployeeID"
AutoGenerateEditButton="True"
DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:BoundField HeaderText="Title" DataField="Title" />
</columns>
</asp:GridView>
<asp:Label
id="Label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
<%@Page Language="VB" %>
<%@Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub OnDSUpdatedHandler(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
If e.AffectedRows > 0 Then
' Perform any additional processing, such as setting a status label.
Label1.Text = Request.LogonUserIdentity.Name & _
" changed user information successfully!"
Else
Label1.Text = "No data updated!"
End If
End Sub 'OnDSUpdatedHandler
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<!-- This example uses a Northwind database that is hosted by an ODBC-compliant
database. To run this sample, create an ODBC DSN to any database that hosts
the Northwind database, including Microsoft SQL Server or Microsoft Access,
change the name of the DSN in the ConnectionString, and view the page.
-->
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ProviderName="System.Data.Odbc"
DataSourceMode="DataSet"
ConnectionString="dsn=myodbc3dsn;"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
UpdateCommand="Update Employees SET FirstName=?,LastName=?,Title=? WHERE EmployeeID=?"
OnUpdated="OnDSUpdatedHandler">
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="EmployeeID"
AutoGenerateEditButton="True"
DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:BoundField HeaderText="Title" DataField="Title" />
</columns>
</asp:GridView>
<asp:Label
id="Label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
注解
UpdateCommand表示 SQL 查询或存储过程的名称,由 Update 方法使用。
由于不同的数据库产品使用不同类型的 SQL,因此 SQL 字符串的语法取决于当前正在使用的 ADO.NET 提供程序,该提供程序由 ProviderName 属性标识。 如果 SQL 字符串是一个参数化查询或命令,则参数的占位符还取决于所使用的 ADO.NET 提供程序。 例如,如果提供程序是 System.Data.SqlClient,这是 类的默认提供程序 SqlDataSource ,则 参数的占位符为 '@parameterName'
。 但是,如果提供程序设置为 System.Data.Odbc 或 System.Data.OleDb,则参数的占位符为 '?'
。 有关参数化 SQL 查询和命令的详细信息,请参阅 在 SqlDataSource 控件中使用参数。
如果数据源支持存储过程,则 UpdateCommand 属性可以是 SQL 字符串或存储过程的名称。
属性UpdateCommand委托给UpdateCommand与 SqlDataSource 控件关联的 对象的 属性SqlDataSourceView。
重要
出于安全目的, UpdateCommand 不存储属性为视图状态。 由于可以在客户端上解码视图状态的内容,因此在视图状态中存储有关数据库结构的敏感信息可能会导致信息泄露漏洞。