SqlDataSource.SqlDataSource(String, String) Constructor

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

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public:
SqlDataSource (
	String^ connectionString, 
	String^ selectCommand
)
public SqlDataSource (
	String connectionString, 
	String selectCommand
)
public function SqlDataSource (
	connectionString : String, 
	selectCommand : String
)
Not applicable.

Parameters

connectionString

The connection string used to connect to the underlying database.

selectCommand

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.

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 on parameterized SQL queries and commands, see Parameters with the SqlDataSource and AccessDataSource Controls.

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

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.

No code example is currently available or this language may not be supported.
// 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 Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show: