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 顯示資料,並在按一下 [ 提交 ] 按鈕時更新資料。 會 UpdateCommand 使用參數化的 SQL 語句來設定 ,並將兩 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 參數名稱。

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

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

重要

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

適用於

另請參閱