SqlDataSource.Insert メソッド

定義

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

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

返品

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

例外

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

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

Important

この例には、潜在的なセキュリティ上の脅威であるユーザー入力を受け入れるテキスト ボックスが含まれており、値は検証なしでパラメーターに挿入されます。これは潜在的なセキュリティ上の脅威でもあります。 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 オブジェクトは、DbCommand テキストと関連するInsertCommandプロパティを使用してInsertParameters オブジェクトをビルドし、基になるデータベースに対してDbCommand オブジェクトを実行します。

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

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

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

Important

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

適用対象

こちらもご覧ください