ParameterCollection.Add メソッド

定義

Parameter オブジェクトをコレクションに追加します。

オーバーロード

Add(Parameter)

指定した Parameter オブジェクトをコレクションの末尾に追加します。

Add(String, String)

名前と既定値を指定して Parameter オブジェクトを作成し、コレクションの末尾に追加します。

Add(String, DbType, String)

名前、データベース型、および既定値を指定して Parameter オブジェクトを作成し、コレクションの末尾に追加します。

Add(String, TypeCode, String)

名前、Parameter、および既定値を指定して TypeCode オブジェクトを作成し、コレクションの末尾に追加します。

Add(Parameter)

指定した Parameter オブジェクトをコレクションの末尾に追加します。

public:
 int Add(System::Web::UI::WebControls::Parameter ^ parameter);
public int Add (System.Web.UI.WebControls.Parameter parameter);
member this.Add : System.Web.UI.WebControls.Parameter -> int
Public Function Add (parameter As Parameter) As Integer

パラメーター

parameter
Parameter

コレクションの末尾に追加する Parameter

戻り値

追加された項目のインデックス値。

次のコード例では、コントロールと オブジェクトをAccessDataSource使用して、FormParameterコントロール内の Microsoft Access データベースからの情報を表示する方法をGridView示します。 オブジェクトは FormParameter 、 メソッドを SelectParameters 使用してコレクションに Add(Parameter) 追加されます。

重要

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。 既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。 詳細については、「スクリプトによる攻略の概要」を参照してください。

<%@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">

void Page_Load(Object sender, EventArgs e){

  // You can add a FormParameter to the AccessDataSource control's
  // SelectParameters collection programmatically.
  AccessDataSource1.SelectParameters.Clear();

  // Security Note: The AccessDataSource uses a FormParameter,
  // Security Note: which does not perform validation of input from the client.
  // Security Note: To validate the value of the FormParameter,
  // Security Note: handle the Selecting event.

  FormParameter formParam = new FormParameter("lastname","LastNameBox");
  formParam.Type=TypeCode.String;
  AccessDataSource1.SelectParameters.Add(formParam);
}

</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:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datasourcemode="DataSet"
          datafile="Northwind.mdb"
          selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
                         FROM Orders WHERE EmployeeID =
                         (SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
      </asp:accessdatasource>

      <br />Enter the name "Davolio" or "King" in the text box and click the button.

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

      <br />
      <asp:button
        id="Button1"
        runat="server"
        text="Get Records" />

      <br />
      <asp:gridview
          id="GridView1"
          runat="server"
          allowsorting="True"
          datasourceid="AccessDataSource1">
      </asp:gridview>

    </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 Page_Load(sender As Object, e As EventArgs)

  ' You can add a FormParameter to the AccessDataSource control's
  ' SelectParameters collection programmatically.
  AccessDataSource1.SelectParameters.Clear()

  ' Security Note: The AccessDataSource uses a FormParameter,
  ' Security Note: which does not perform validation of input from the client.
  ' Security Note: To validate the value of the FormParameter,
  ' Security Note: handle the Selecting event.

  Dim formParam As New FormParameter("lastname","LastNameBox")
  formParam.Type=TypeCode.String
  AccessDataSource1.SelectParameters.Add(formParam)
End Sub ' Page_Load

</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:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datasourcemode="DataSet"
          datafile="Northwind.mdb"
          selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
                         FROM Orders WHERE EmployeeID =
                         (SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
      </asp:accessdatasource>

      <br />Enter the name "Davolio" or "King" in the text box and click the button.

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

      <br />
      <asp:button
        id="Button1"
        runat="server"
        text="Get Records" />

      <br />
      <asp:gridview
          id="GridView1"
          runat="server"
          allowsorting="True"
          datasourceid="AccessDataSource1">
      </asp:gridview>

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

注釈

コレクションの Add(Parameter) 末尾にオブジェクトを Parameter 追加するには、 メソッドを使用します。 メソッドのこの実装では、 パラメーターで指定された オブジェクトをparam受け取Parameterり、コレクションに追加します。

こちらもご覧ください

適用対象

Add(String, String)

名前と既定値を指定して Parameter オブジェクトを作成し、コレクションの末尾に追加します。

public:
 int Add(System::String ^ name, System::String ^ value);
public int Add (string name, string value);
member this.Add : string * string -> int
Public Function Add (name As String, value As String) As Integer

パラメーター

name
String

パラメーターの名前。

value
String

パラメーターの既定値として使用される文字列。

戻り値

追加された項目のインデックス値。

次のコード例では、 メソッドをAdd(String, String)使用して、 パラメーターと value パラメーターを指定してコレクションにParameterCollection新しいParameterオブジェクトを追加する方法をname示します。 この例では、コントロールの Parameter 値にバインドされている Access データ ソース コントロールの Update コマンドに オブジェクトが TextBox 追加されます。

重要

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。 既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。 詳細については、「スクリプトによる攻略の概要」を参照してください。

<script runat="server">
private void UpdateRecords(Object source, EventArgs e)
{
  CheckBox cb;
  foreach(GridViewRow row in this.GridView1.Rows) {
    cb = (CheckBox) row.Cells[0].Controls[1];
    if(cb.Checked) {
      string oid = (string) row.Cells[1].Text;
      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString());
      MyAccessDataSource.UpdateParameters.Add("orderid", oid);
      MyAccessDataSource.Update();
      MyAccessDataSource.UpdateParameters.Clear();
    }
  }
}
</script>
<script runat="server">
Private Sub UpdateRecords(source As Object, e As EventArgs)

  Dim cb As CheckBox
  Dim row As GridViewRow

  For Each row In GridView1.Rows

    cb = CType(row.Cells(0).Controls(1), CheckBox)
    If cb.Checked Then

      Dim oid As String
      oid = CType(row.Cells(1).Text, String)

      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString())

      MyAccessDataSource.UpdateParameters.Add("orderid", oid)

      MyAccessDataSource.Update()
      MyAccessDataSource.UpdateParameters.Clear()
    End If
  Next
End Sub ' UpdateRecords
</script>

注釈

メソッドを Add(String, String) 使用して、既定値を持つオブジェクトを Parameter 作成し、コレクションの末尾に追加します。 メソッドのこの実装では、 パラメーターと パラメーターでnamevalue指定された名前と既定値をそれぞれ使用して オブジェクトを作成Parameterし、コレクションに追加します。

こちらもご覧ください

適用対象

Add(String, DbType, String)

名前、データベース型、および既定値を指定して Parameter オブジェクトを作成し、コレクションの末尾に追加します。

public:
 int Add(System::String ^ name, System::Data::DbType dbType, System::String ^ value);
public int Add (string name, System.Data.DbType dbType, string value);
member this.Add : string * System.Data.DbType * string -> int
Public Function Add (name As String, dbType As DbType, value As String) As Integer

パラメーター

name
String

パラメーターの名前。

dbType
DbType

パラメーターのデータベース型。

value
String

パラメーターの既定値です。

戻り値

追加された項目のインデックス値。

注釈

このメソッドはデータベース型用です。 CLR 型には Add(String, TypeCode, String) メソッドを使用します。

適用対象

Add(String, TypeCode, String)

名前、Parameter、および既定値を指定して TypeCode オブジェクトを作成し、コレクションの末尾に追加します。

public:
 int Add(System::String ^ name, TypeCode type, System::String ^ value);
public int Add (string name, TypeCode type, string value);
member this.Add : string * TypeCode * string -> int
Public Function Add (name As String, type As TypeCode, value As String) As Integer

パラメーター

name
String

パラメーターの名前。

type
TypeCode

パラメーターの型。

value
String

パラメーターの既定値です。

戻り値

追加された項目のインデックス値。

次のコード例では、 メソッドをAdd(String, TypeCode, String)使用して、、value、および type パラメーターをParameterCollection指定することで、新しいParameterオブジェクトをコレクションにname追加する方法を示します。 この例では、現在の Parameter システム時刻の値を提供する Access データ ソース管理の Update コマンドに オブジェクトが追加されます。 パラメーターは、 の と共にTypeCodeDateTime追加されます。

<script runat="server">
private void UpdateRecords(Object source, EventArgs e)
{
  CheckBox cb;
  foreach(GridViewRow row in this.GridView1.Rows) {
    cb = (CheckBox) row.Cells[0].Controls[1];
    if(cb.Checked) {
      string oid = (string) row.Cells[1].Text;
      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString());
      MyAccessDataSource.UpdateParameters.Add("orderid", oid);
      MyAccessDataSource.Update();
      MyAccessDataSource.UpdateParameters.Clear();
    }
  }
}
</script>
<script runat="server">
Private Sub UpdateRecords(source As Object, e As EventArgs)

  Dim cb As CheckBox
  Dim row As GridViewRow

  For Each row In GridView1.Rows

    cb = CType(row.Cells(0).Controls(1), CheckBox)
    If cb.Checked Then

      Dim oid As String
      oid = CType(row.Cells(1).Text, String)

      MyAccessDataSource.UpdateParameters.Add("date", TypeCode.DateTime, DateTime.Now.ToString())

      MyAccessDataSource.UpdateParameters.Add("orderid", oid)

      MyAccessDataSource.Update()
      MyAccessDataSource.UpdateParameters.Clear()
    End If
  Next
End Sub ' UpdateRecords
</script>

注釈

メソッドを Add(String, TypeCode, String) 使用して、厳密に型指定された Parameter オブジェクトを作成し、既定値を持つオブジェクトをコレクションの末尾に追加します。 メソッドのこの実装では、 パラメーターと パラメーターでnametypevalue指定された名前、型、値をそれぞれ使用して オブジェクトを作成Parameterし、コレクションに追加します。

こちらもご覧ください

適用対象