次の方法で共有


SqlDataSourceView.Update(IDictionary, IDictionary, IDictionary) メソッド

定義

UpdateCommand SQL 文字列、UpdateParameters コレクション内のすべてのパラメーター、および指定したkeysvalues、および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

UpdateCommand プロパティでデータベースの更新操作を実行するために使用する主キーのIDictionary。 クエリに関連付けられているキーがない場合、または UpdateCommand がパラメーター化された SQL クエリでない場合は、 null渡します。

values
IDictionary

UpdateCommand プロパティでデータベース更新操作を実行するために使用する値のIDictionary。 クエリに関連付けられているパラメーターがない場合、または UpdateCommand がパラメーター化された SQL クエリでない場合は、 null渡します。

oldValues
IDictionary

データベース内の元の値を表す IDictionary 。 クエリに関連付けられているパラメーターがない場合、または UpdateCommand がパラメーター化された SQL クエリでない場合は、 null渡します。

戻り値

基になるデータベースで更新された行の数を表す値。

例外

SqlDataSourceは、基になるデータ ソースとの接続を確立できません。

CanUpdate プロパティはfalse

次のコード例では、 SqlDataSource コントロールを使用して、 DropDownList コントロールにデータを表示し、[ 送信] ボタンをクリックしたときにデータを更新する方法を示します。 UpdateCommand プロパティはパラメーター化された SQL ステートメントで設定され、UpdateParameters コレクションに 2 つのControlParameter パラメーターが追加されます。 [送信] ボタンをクリックすると、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 メソッドを呼び出し、keysvalues、およびoldValuesパラメーターを渡します。 詳細については、SqlDataSource.UpdateCommandを参照してください。

適用対象

こちらもご覧ください