SqlDataSource コンストラクタ ()
アセンブリ: System.Web (system.web.dll 内)
構文
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
SqlDataSource コンストラクタ (String, String)
アセンブリ: System.Web (system.web.dll 内)
構文
Dim connectionString As String Dim selectCommand As String Dim instance As New SqlDataSource(connectionString, selectCommand)
異なるデータベース製品では異なる種類の SQL が使用されるため、selectCommand の構文は、現在使用されている ADO.NET プロバイダによって異なります。このプロバイダは、ProviderName プロパティによって示されます。SQL 文字列がパラメータ化されたクエリまたはコマンドである場合、パラメータのプレースホルダは、使用されている ADO.NET プロバイダに依存します。たとえば、プロバイダが SqlDataSource クラスの既定のプロバイダである System.Data.SqlClient の場合、パラメータのプレースホルダは '@parameterName' です。ただし、プロバイダが System.Data.Odbc または System.Data.OleDb に設定されている場合、パラメータのプレースホルダは '?' となります。パラメータ化された SQL クエリとコマンドの詳細については、「SqlDataSource コントロールにおけるパラメータの使用」を参照してください。
データ ソースがストアド プロシージャをサポートしている場合、SelectCommand 値には、SQL 文字列またはストアド プロシージャの名前を指定できます。
SqlDataSource コンストラクタを使用して SqlDataSource コントロールを作成する方法を次のコード例に示します。この例は、Web フォーム ページ上にない SqlDataSource コントロールが使用されている点が例外的ですが、中間層オブジェクトの実装において、データベースと対話する簡単な方法としてビジネス オブジェクトが使用されています。この例では、Web.config ファイルに格納された接続文字列を使用します。
このコード例は、ObjectDataSource クラスのトピックで取り上げているコード例の一部分です。
' 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
// 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 static ICollection GetAllEmployees() throws NorthwindDataException, SqlException { ArrayList al = new ArrayList(); ConnectionStringSettings cts = ConfigurationManager.get_ConnectionStrings().get_Item("NorthwindConnection"); SqlDataSource sds = new SqlDataSource(cts.get_ConnectionString(), "SELECT EmployeeID FROM Employees"); try { IEnumerable ids = sds.Select(DataSourceSelectArguments.get_Empty()); // Iterate through the Enumeration and create a // NorthwindEmployee object for each id. IEnumerator enumerator = ids.GetEnumerator(); while (enumerator.MoveNext()) { // The IEnumerable contains DataRowView objects. DataRowView row = (DataRowView)enumerator.get_Current(); String id = row.get_Item("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; } //GetAllEmployees
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
SqlDataSource コンストラクタ
オーバーロードの一覧
名前 | 説明 |
---|---|
SqlDataSource () | SqlDataSource クラスの新しいインスタンスを初期化します。 |
SqlDataSource (String, String) | 接続文字列と SELECT コマンドを指定して、SqlDataSource クラスの新しいインスタンスを初期化します。 |
SqlDataSource (String, String, String) | 接続文字列と SELECT コマンドを指定して、SqlDataSource クラスの新しいインスタンスを初期化します。 |
SqlDataSource コンストラクタ (String, String, String)
アセンブリ: System.Web (system.web.dll 内)
構文
Public Sub New ( _ providerName As String, _ connectionString As String, _ selectCommand As String _ )
Dim providerName As String Dim connectionString As String Dim selectCommand As String Dim instance As New SqlDataSource(providerName, connectionString, selectCommand)
public function SqlDataSource ( providerName : String, connectionString : String, selectCommand : String )
異なるデータベース製品では異なる種類の SQL が使用されるため、selectCommand の構文は、現在使用されている ADO.NET プロバイダによって異なります。このプロバイダは、providerName パラメータによって示されます。SQL 文字列がパラメータ化されたクエリまたはコマンドである場合、パラメータのプレースホルダは、使用されている ADO.NET プロバイダに依存します。たとえば、プロバイダが SqlDataSource クラスの既定のプロバイダである System.Data.SqlClient の場合、パラメータのプレースホルダは '@parameterName' です。ただし、プロバイダが System.Data.Odbc または System.Data.OleDb に設定されている場合、パラメータのプレースホルダは '?' となります。パラメータ化された SQL クエリとコマンドの詳細については、「SqlDataSource コントロールにおけるパラメータの使用」を参照してください。
データ ソースがストアド プロシージャをサポートしている場合、SelectCommand プロパティには、SQL 文字列またはストアド プロシージャの名前を指定できます。
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- SqlDataSource コンストラクタ ()のページへのリンク