共用方式為


SqlDataSource.UpdateParameters 屬性

定義

取得參數集合,包含與 UpdateCommand 控制項相關聯之 SqlDataSourceView 控制項 SqlDataSource 屬性所使用的參數。

public:
 property System::Web::UI::WebControls::ParameterCollection ^ UpdateParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection UpdateParameters { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.UpdateParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property UpdateParameters As ParameterCollection

屬性值

ParameterCollection,包含 UpdateCommand 屬性所使用的參數。

屬性

範例

下列程式代碼範例示範如何使用 SqlDataSource 控件在控件中 DropDownList 顯示數據,並在按兩下 [ 提交 ] 按鈕時更新數據。 使用參數化的 SQL 語句設定 , UpdateCommand 並將兩 ControlParameter 個參數新增至 UpdateParameters 集合。 按兩下 [ 提交 ] 按鈕時, OnClick 會處理事件以明確呼叫 Update 方法。

重要

此範例包含一個文本框,可接受使用者輸入,這是潛在的安全性威脅,而且值會插入參數中而不驗證,這也是潛在的安全性威脅。 在執行查詢之前, Inserting 請使用 事件來驗證參數值。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@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>

備註

UpdateCommand如果屬性包含參數化的 SQL 查詢,UpdateParameters集合會Parameter包含任何對應至 SQL 字串中參數佔位元的物件。

參數名稱可能會受到 OldValuesParameterFormatString 屬性的影響,特別是如果名稱識別主鍵,例如使用DataKeyNames數據系結控件的 屬性所指定的索引鍵,或是刪除和更新屬性設定為CompareAllValues值且將一組 oldValues 傳遞至對應數據方法的案例ConflictDetection中。 在此情況下,格式字串會套用至集合中的每個 oldValues 參數名稱。

集合中 UpdateParameters 參數的順序可能很重要,視 ADO.NET 提供者而定。 System.Data.OleDbSystem.Data.Odbc 提供者會根據參數出現在參數化 SQL 查詢中的順序,關聯集合中的參數。 提供者 System.Data.SqlClient 是控件的預設 ADO.NET 提供者 SqlDataSource ,會比對參數的名稱與 SQL 查詢中的佔位元別名,使集合中的參數產生關聯。 如需參數化 SQL 查詢和命令的詳細資訊,請參閱 搭配 SqlDataSource 控制項使用參數

屬性UpdateParametersUpdateParameters擷取與控件相關聯SqlDataSource之 物件所包含的SqlDataSourceView屬性。

重要

值會插入參數而不進行驗證,這是潛在的安全性威脅。 在執行查詢之前, Filtering 請使用 事件來驗證參數值。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

適用於

另請參閱