SqlDataSource Constructors

Definition

Initializes a new instance of the SqlDataSource class.

Overloads

SqlDataSource()

Initializes a new instance of the SqlDataSource class.

SqlDataSource(String, String)

Initializes a new instance of the SqlDataSource class with the specified connection string and Select command.

SqlDataSource(String, String, String)

Initializes a new instance of the SqlDataSource class with the specified connection string and Select command.

SqlDataSource()

Initializes a new instance of the SqlDataSource class.

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

See also

Applies to

SqlDataSource(String, String)

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)

Parameters

connectionString
String

The connection string used to connect to the underlying database.

selectCommand
String

The SQL query used to retrieve data from the underlying database. If the SQL query is a parameterized SQL string, you might need to add Parameter objects to the SelectParameters collection.

Examples

The following code example demonstrates how to create a SqlDataSource control using the SqlDataSource constructor. 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. The example uses a connection string that is stored in the Web.config file.

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

Remarks

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. If the SQL string is a parameterized query or command, the placeholder of the parameter also depends on the ADO.NET provider being used. 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'. However, if the provider is set to the System.Data.Odbc or the System.Data.OleDb, the placeholder of the parameter is '?'. For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

The SelectCommand value can be an SQL string or the name of a stored procedure, if the data source supports stored procedures.

See also

Applies to

SqlDataSource(String, String, String)

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)

Parameters

providerName
String

The name of the data provider that the SqlDataSource uses. If no provider is set, the SqlDataSource uses the ADO.NET provider for Microsoft SQL Server, by default.

connectionString
String

The connection string used to connect to the underlying database.

selectCommand
String

The SQL query used to retrieve data from the underlying database. If the SQL query is a parameterized SQL string, you might need to add Parameter objects to the SelectParameters collection.

Remarks

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. If the SQL string is a parameterized query or command, the placeholder of the parameter also depends on the ADO.NET provider being used. 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'. However, if the provider is set to the System.Data.Odbc or the System.Data.OleDb, the placeholder of the parameter is '?'. For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

The SelectCommand property can be an SQL string or the name of a stored procedure, if the data source supports stored procedures.

See also

Applies to