SqlDataSourceView.UpdateParameters 屬性

定義

取得包含 UpdateCommand 屬性所用參數的參數集合。

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 方法。

<%@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 值的刪除和更新案例 ConflictDetection 中,以及將 一組 oldValues 傳遞給對應的資料方法。 在此情況下,格式字串會套用至集合中的每個 oldValues 參數名稱。

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

適用於

另請參閱