SqlDataSource SqlDataSource SqlDataSource SqlDataSource Constructors

定义

重载

SqlDataSource() SqlDataSource() SqlDataSource()

初始化 SqlDataSource 类的新实例。Initializes a new instance of the SqlDataSource class.

SqlDataSource(String, String) SqlDataSource(String, String) SqlDataSource(String, String) SqlDataSource(String, String)

使用指定的连接字符串和 Select 命令初始化 SqlDataSource 类的新实例。Initializes a new instance of the SqlDataSource class with the specified connection string and Select command.

SqlDataSource(String, String, String) SqlDataSource(String, String, String) SqlDataSource(String, String, String) SqlDataSource(String, String, String)

使用指定的连接字符串和 Select 命令初始化 SqlDataSource 类的新实例。Initializes a new instance of the SqlDataSource class with the specified connection string and Select command.

SqlDataSource(String, String) SqlDataSource(String, String) SqlDataSource(String, String) SqlDataSource(String, String)

使用指定的连接字符串和 Select 命令初始化 SqlDataSource 类的新实例。Initializes a new instance of the SqlDataSource class with the specified connection string and Select command.

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 String String String

用于与基础数据库建立连接的连接字符串。The connection string used to connect to the underlying database.

selectCommand
String String String String

用于从基础数据库中检索数据的 SQL 查询。The SQL query used to retrieve data from the underlying database. 如果该 SQL 查询是参数化的 SQL 字符串,可能需要将 Parameter 对象添加到 SelectParameters 集合中。If the SQL query is a parameterized SQL string, you might need to add Parameter objects to the SelectParameters collection.

示例

下面的代码示例演示如何SqlDataSource SqlDataSource使用构造函数创建控件。The following code example demonstrates how to create a SqlDataSource control using the SqlDataSource constructor. 该示例是异常SqlDataSource的, 因为控件不在 Web 窗体页上使用, 而是在中间层对象的实现中作为业务对象与数据库进行交互的简单方式。The example is unusual in that the SqlDataSource control is being used not on a Web Forms page, but in the implementation of a middle-tier object as a simple way for a business object to interact with a database. 该示例使用存储在 web.config 文件中的连接字符串。The example uses a connection string that is stored in the Web.config file.

此代码示例是为ObjectDataSource类提供的更大示例的一部分。This code example is part of a larger example provided for the ObjectDataSource class.

// 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由属性标识。Because different database products use different varieties of SQL, the syntax for selectCommand depends on the current ADO.NET provider being used, which is identified by the ProviderName property. 如果 SQL 字符串是一个参数化查询或命令,则参数的占位符还取决于所使用的 ADO.NET 提供程序。If the SQL string is a parameterized query or command, the placeholder of the parameter also depends on the ADO.NET provider being used. 例如, 如果提供程序是System.Data.SqlClient, 它是SqlDataSource类的默认提供程序, 则参数的占位符为'@parameterName'For example, if the provider is the System.Data.SqlClient, which is the default provider for the SqlDataSource class, the placeholder of the parameter is '@parameterName'. 但是, 如果提供程序设置为System.Data.Odbc System.Data.OleDb或, 则参数的占位符为'?'However, if the provider is set to the System.Data.Odbc or the System.Data.OleDb, the placeholder of the parameter is '?'. 有关参数化 SQL 查询和命令的详细信息, 请参阅将参数与 SqlDataSource 控件一起使用For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

如果SelectCommand数据源支持存储过程, 则值可以是 SQL 字符串或存储过程的名称。The SelectCommand value can be an SQL string or the name of a stored procedure, if the data source supports stored procedures.

另请参阅

SqlDataSource(String, String, String) SqlDataSource(String, String, String) SqlDataSource(String, String, String) SqlDataSource(String, String, String)

使用指定的连接字符串和 Select 命令初始化 SqlDataSource 类的新实例。Initializes a new instance of the SqlDataSource class with the specified connection string and Select command.

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 String String String

SqlDataSource 使用的数据提供程序的名称。The name of the data provider that the SqlDataSource uses. 如果没有设置任何提供程序,则在默认情况下,SqlDataSource 使用 Microsoft SQL Server 的 ADO.NET 提供程序。If no provider is set, the SqlDataSource uses the ADO.NET provider for Microsoft SQL Server, by default.

connectionString
String String String String

用于与基础数据库建立连接的连接字符串。The connection string used to connect to the underlying database.

selectCommand
String String String String

用于从基础数据库中检索数据的 SQL 查询。The SQL query used to retrieve data from the underlying database. 如果该 SQL 查询是参数化的 SQL 字符串,可能需要将 Parameter 对象添加到 SelectParameters 集合中。If the SQL query is a parameterized SQL string, you might need to add Parameter objects to the SelectParameters collection.

注解

由于不同的数据库产品使用不同种类的 SQL, 因此的selectCommand语法依赖于当前使用的 ADO.NET 提供程序, 该提供程序providerName由参数标识。Because different database products use different varieties of SQL, the syntax of selectCommand depends on the current ADO.NET provider being used, which is identified by the providerName parameter. 如果 SQL 字符串是一个参数化查询或命令,则参数的占位符还取决于所使用的 ADO.NET 提供程序。If the SQL string is a parameterized query or command, the placeholder of the parameter also depends on the ADO.NET provider being used. 例如, 如果提供程序是System.Data.SqlClient, 它是SqlDataSource类的默认提供程序, 则参数的占位符为'@parameterName'For example, if the provider is System.Data.SqlClient, which is the default provider for the SqlDataSource class, the placeholder of the parameter is '@parameterName'. 但是, 如果提供程序设置为System.Data.Odbc System.Data.OleDb或, 则参数的占位符为'?'However, if the provider is set to the System.Data.Odbc or the System.Data.OleDb, the placeholder of the parameter is '?'. 有关参数化 SQL 查询和命令的详细信息, 请参阅将参数与 SqlDataSource 控件一起使用For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

如果SelectCommand数据源支持存储过程, 则属性可以是 SQL 字符串或存储过程的名称。The SelectCommand property can be an SQL string or the name of a stored procedure, if the data source supports stored procedures.

另请参阅

适用于