語言

SqlDataSource 建構函式

定義

初始化 SqlDataSource 類別的新執行個體。

多載

名稱 Description
SqlDataSource()

初始化 SqlDataSource 類別的新執行個體。

SqlDataSource(String, String)

初始化一個新的類別實例 SqlDataSource ,使用指定的連接字串和 Select 指令。

SqlDataSource(String, String, String)

初始化一個新的類別實例 SqlDataSource ,使用指定的連接字串和 Select 指令。

SqlDataSource()

初始化 SqlDataSource 類別的新執行個體。

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

另請參閱

適用於

SqlDataSource(String, String)

初始化一個新的類別實例 SqlDataSource ,使用指定的連接字串和 Select 指令。

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.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)

初始化一個新的類別實例 SqlDataSource ,使用指定的連接字串和 Select 指令。

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 字串,或是儲存程序的名稱,前提是資料來源支援儲存程序。

另請參閱

適用於