次の方法で共有


SqlDataSource コンストラクター

定義

SqlDataSource クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
SqlDataSource()

SqlDataSource クラスの新しいインスタンスを初期化します。

SqlDataSource(String, String)

指定した接続文字列と Select コマンドを使用して、 SqlDataSource クラスの新しいインスタンスを初期化します。

SqlDataSource(String, String, String)

指定した接続文字列と Select コマンドを使用して、 SqlDataSource クラスの新しいインスタンスを初期化します。

SqlDataSource()

SqlDataSource クラスの新しいインスタンスを初期化します。

public:
 SqlDataSource();
public SqlDataSource();
Public Sub New ()

こちらもご覧ください

適用対象

SqlDataSource(String, String)

指定した接続文字列と Select コマンドを使用して、 SqlDataSource クラスの新しいインスタンスを初期化します。

public:
 SqlDataSource(System::String ^ connectionString, System::String ^ selectCommand);
public SqlDataSource(string connectionString, string selectCommand);
new System.Web.UI.WebControls.SqlDataSource : string * string -> System.Web.UI.WebControls.SqlDataSource
Public Sub New (connectionString As String, selectCommand As String)

パラメーター

connectionString
String

基になるデータベースへの接続に使用される接続文字列。

selectCommand
String

基になるデータベースからデータを取得するために使用される SQL クエリ。 SQL クエリがパラメーター化された SQL 文字列の場合は、 Parameter オブジェクトを SelectParameters コレクションに追加することが必要になる場合があります。

次のコード例では、SqlDataSource コンストラクターを使用してSqlDataSource コントロールを作成する方法を示します。 この例は、 SqlDataSource コントロールが Web フォーム ページではなく、ビジネス オブジェクトがデータベースと対話するための簡単な方法として中間層オブジェクトの実装で使用されているという点では珍しい点です。 この例では、Web.config ファイルに格納されている接続文字列を使用します。

このコード例は、 ObjectDataSource クラスに提供されるより大きな例の一部です。

// Returns a collection of NorthwindEmployee objects.
public static ICollection GetAllEmployees () {
  ArrayList al = new ArrayList();

  ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"];

  SqlDataSource sds
    = new SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees");

  try {

    IEnumerable IDs = sds.Select(DataSourceSelectArguments.Empty);

    // Iterate through the Enumeration and create a
    // NorthwindEmployee object for each ID.
    foreach (DataRowView row in IDs) {
      string id = row["EmployeeID"].ToString();
      NorthwindEmployee nwe = new NorthwindEmployee(id);
      // Add the NorthwindEmployee object to the collection.
      al.Add(nwe);
    }
  }
  finally {
    // If anything strange happens, clean up.
    sds.Dispose();
  }

  return al;
}
' Returns a collection of NorthwindEmployee objects.
Public Shared Function GetAllEmployees() As ICollection
   Dim al As New ArrayList()

   Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
   Dim sds As New SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees")
   Try
      Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty)

      ' Iterate through the Enumeration and create a
      ' NorthwindEmployee object for each ID.
      For Each row As DataRowView In IDs
         Dim id As String = row("EmployeeID").ToString()
         Dim nwe As New NorthwindEmployee(id)
         ' Add the NorthwindEmployee object to the collection.
         al.Add(nwe)
      Next
   Finally
      ' If anything strange happens, clean up.
      sds.Dispose()
   End Try

   Return al
End Function 'GetAllEmployees

注釈

データベース製品によって使用される SQL の種類が異なるため、 selectCommand の構文は、現在使用されている ADO.NET プロバイダーによって異なります。これは、 ProviderName プロパティによって識別されます。 SQL 文字列がパラメーター化されたクエリまたはコマンドの場合、パラメーターのプレースホルダーも、使用されている ADO.NET プロバイダーによって異なります。 たとえば、プロバイダーが System.Data.SqlClient ( SqlDataSource クラスの既定のプロバイダー) の場合、パラメーターのプレースホルダーは '@parameterName'。 ただし、プロバイダーが System.Data.Odbc または System.Data.OleDbに設定されている場合、パラメーターのプレースホルダーは '?'。 パラメーター化された SQL クエリとコマンドの詳細については、「 SqlDataSource コントロールでのパラメーターの使用」を参照してください。

データ ソースでストアド プロシージャがサポートされている場合、 SelectCommand 値には SQL 文字列またはストアド プロシージャの名前を指定できます。

こちらもご覧ください

適用対象

SqlDataSource(String, String, String)

指定した接続文字列と Select コマンドを使用して、 SqlDataSource クラスの新しいインスタンスを初期化します。

public:
 SqlDataSource(System::String ^ providerName, System::String ^ connectionString, System::String ^ selectCommand);
public SqlDataSource(string providerName, string connectionString, string selectCommand);
new System.Web.UI.WebControls.SqlDataSource : string * string * string -> System.Web.UI.WebControls.SqlDataSource
Public Sub New (providerName As String, connectionString As String, selectCommand As String)

パラメーター

providerName
String

SqlDataSourceが使用するデータ プロバイダーの名前。 プロバイダーが設定されていない場合、 SqlDataSource は既定で Microsoft SQL Server 用の ADO.NET プロバイダーを使用します。

connectionString
String

基になるデータベースへの接続に使用される接続文字列。

selectCommand
String

基になるデータベースからデータを取得するために使用される SQL クエリ。 SQL クエリがパラメーター化された SQL 文字列の場合は、 Parameter オブジェクトを SelectParameters コレクションに追加することが必要になる場合があります。

注釈

データベース製品によって使用される SQL の種類が異なるため、 selectCommand の構文は、現在使用されている ADO.NET プロバイダーによって異なります。これは、 providerName パラメーターによって識別されます。 SQL 文字列がパラメーター化されたクエリまたはコマンドの場合、パラメーターのプレースホルダーも、使用されている ADO.NET プロバイダーによって異なります。 たとえば、プロバイダーが System.Data.SqlClient ( SqlDataSource クラスの既定のプロバイダー) の場合、パラメーターのプレースホルダーは '@parameterName'。 ただし、プロバイダーが System.Data.Odbc または System.Data.OleDbに設定されている場合、パラメーターのプレースホルダーは '?'。 パラメーター化された SQL クエリとコマンドの詳細については、「 SqlDataSource コントロールでのパラメーターの使用」を参照してください。

データ ソースでストアド プロシージャがサポートされている場合は、 SelectCommand プロパティに SQL 文字列またはストアド プロシージャの名前を指定できます。

こちらもご覧ください

適用対象