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 Form頁面上,而是在仲介層物件的實作中,做為商務物件與資料庫互動的簡單方式。 此範例會使用儲存在 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.OdbcSystem.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 預設會將 ADO.NET 提供者用於 Microsoft SQL Server。

connectionString
String

用來連接基礎資料庫的連接字串。

selectCommand
String

用來從基礎資料庫擷取資料的 SQL 查詢。 如果 SQL 查詢是參數型 SQL 字串,則您可能需要將 Parameter 物件加入 SelectParameters 集合。

備註

因為不同的資料庫產品使用不同的SQL,所以 的 selectCommand 語法取決於目前所使用的 ADO.NET 提供者,由 參數識別 providerName 。 如果 SQL 字串是參數型查詢或命令,參數的替代符號亦須視目前所使用的 ADO.NET 提供者而定。 例如,如果提供者是 System.Data.SqlClient ,這是類別的預設提供者 SqlDataSource ,則參數的預留位置為 '@parameterName' 。 不過,如果提供者設定為 System.Data.OdbcSystem.Data.OleDb ,則 參數的預留位置為 '?' 。 如需參數化SQL查詢和命令的詳細資訊,請參閱搭配 SqlDataSource 控制項使用參數

如果資料來源支援預存程式,屬性 SelectCommand 可以是SQL字串或預存程式的名稱。

另請參閱

適用於