SqlDataSource.Insert メソッド

定義

InsertCommand SQL 文字列と InsertParameters コレクション内にある任意のパラメーターを使用して、挿入操作を実行します。

public:
 int Insert();
public int Insert ();
member this.Insert : unit -> int
Public Function Insert () As Integer

戻り値

基になるデータベースに挿入された行数を表す値。

例外

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

次のコード例では、 コントロールと単純なWeb Forms ページをSqlDataSource使用してデータベースにデータを挿入する方法を示します。 データ テーブル内の現在のデータがコントロールに DropDownList 表示されます。 新しいレコードを追加するには、コントロールに値を TextBox 入力し、[ 挿入 ] ボタンをクリックします。 [挿入] ボタンをクリックすると、指定した値がデータベースに挿入されDropDownList、 が更新されます。

重要

この例には、ユーザー入力を受け入れるテキスト ボックスが含まれています。これは潜在的なセキュリティ上の脅威であり、値は検証なしでパラメーターに挿入されます。これは潜在的なセキュリティ上の脅威でもあります。 イベントを Inserting 使用して、クエリを実行する前にパラメーター値を検証します。 詳細については、「スクリプトによる攻略の概要」を参照してください。

Note

この例では、データ アクセスに宣言構文を使用する方法を示します。 マークアップではなくコードを使用してデータにアクセスする方法については、「 Visual Studio でのデータへのアクセス」を参照してください。

<%@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 InsertShipper (object source, EventArgs e) {
  SqlDataSource1.Insert();
}
</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:dropdownlist
        id="DropDownList1"
        runat="server"
        datasourceid="SqlDataSource1"
        datatextfield="CompanyName"
        datavaluefield="ShipperID" />

<!-- Security Note: The SqlDataSource uses a FormParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the FormParameter, handle the Inserting event. -->

      <asp:sqldatasource
        id="SqlDataSource1"
        runat="server"
        connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
        selectcommand="SELECT CompanyName,ShipperID FROM Shippers"
        insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)">
          <insertparameters>
            <asp:formparameter name="CoName" formfield="CompanyNameBox" />
            <asp:formparameter name="Phone"  formfield="PhoneBox" />
          </insertparameters>
      </asp:sqldatasource>

      <br /><asp:textbox
           id="CompanyNameBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator1"
        runat="server"
        ControlToValidate="CompanyNameBox"
        Display="Static"
        ErrorMessage="Please enter a company name." />

      <br /><asp:textbox
           id="PhoneBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator2"
        runat="server"
        ControlToValidate="PhoneBox"
        Display="Static"
        ErrorMessage="Please enter a phone number." />

      <br /><asp:button
           id="Button1"
           runat="server"
           text="Insert New Shipper"
           onclick="InsertShipper" />

    </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">
Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs)
  SqlDataSource1.Insert()
End Sub ' InsertShipper
</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:dropdownlist
        id="DropDownList1"
        runat="server"
        datasourceid="SqlDataSource1"
        datatextfield="CompanyName"
        datavaluefield="ShipperID" />

<!-- Security Note: The SqlDataSource uses a FormParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the FormParameter, handle the Inserting event. -->

      <asp:sqldatasource
        id="SqlDataSource1"
        runat="server"
        connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
        selectcommand="SELECT CompanyName,ShipperID FROM Shippers"
        insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)">
          <insertparameters>
            <asp:formparameter name="CoName" formfield="CompanyNameBox" />
            <asp:formparameter name="Phone"  formfield="PhoneBox" />
          </insertparameters>
      </asp:sqldatasource>

      <br /><asp:textbox
           id="CompanyNameBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator1"
        runat="server"
        ControlToValidate="CompanyNameBox"
        Display="Static"
        ErrorMessage="Please enter a company name." />

      <br /><asp:textbox
           id="PhoneBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator2"
        runat="server"
        ControlToValidate="PhoneBox"
        Display="Static"
        ErrorMessage="Please enter a phone number." />

      <br /><asp:button
           id="Button1"
           runat="server"
           text="Insert New Shipper"
           onclick="InsertShipper" />

    </form>
  </body>
</html>

注釈

挿入操作が実行される前に、 メソッドが OnInserting 呼び出されてイベントが発生します Inserting 。 このイベントを処理して、パラメーターの値を調べ、操作の前に前処理を Insert 実行できます。 挿入操作を実行するために、オブジェクトはSqlDataSourceViewテキストと関連InsertParametersするプロパティをInsertCommand使用してオブジェクトをビルドDbCommandし、基になるデータベースにDbCommand対してオブジェクトを実行します。

操作が完了すると、 OnInserted メソッドが呼び出されてイベントが発生します Inserted 。 このイベントを処理して、戻り値とエラー コードを調べ、後処理を実行できます。

メソッドは Insert 、 メソッドにプログラムでアクセスするために Insert 提供されます。 コントロールが SqlDataSource データ バインド コントロールに関連付けられている場合、データ バインド コントロールは自動的に メソッドを Insert 呼び出します。

メソッドは Insert 、 コントロールに Insert 関連付けられている オブジェクトの SqlDataSourceView メソッドにデリゲートします SqlDataSource

重要

値は検証なしでパラメーターに挿入されます。これは、潜在的なセキュリティ上の脅威です。 イベントを Filtering 使用して、クエリを実行する前にパラメーター値を検証します。 詳細については、「スクリプトによる攻略の概要」を参照してください。

適用対象

こちらもご覧ください