Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source.
[Visual Basic] Public Shadows Property SelectCommand As SqlCommand [C#] public new SqlCommand SelectCommand {get; set;} [C++] public: __property SqlCommand* get_SelectCommand(); public: __property void set_SelectCommand(SqlCommand*); [JScript] public hide function get SelectCommand() : SqlCommand; public function set SelectCommand(SqlCommand);
A SqlCommand used during Fill to select records from the database for placement in the DataSet.
When SelectCommand is assigned to a previously created SqlCommand, the SqlCommand is not cloned. The SelectCommand maintains a reference to the previously created SqlCommand object.
If the SelectCommand does not return any rows, no tables are added to the DataSet, and no exception is raised.
[Visual Basic, C#] The following example creates a SqlDataAdapter and sets the SelectCommand and InsertCommand properties. It assumes you have already created a SqlConnection object.
[Visual Basic] Public Shared Function CreateCustomerAdapter(conn As SqlConnection) As SqlDataAdapter Dim da As SqlDataAdapter = New SqlDataAdapter() Dim cmd As SqlCommand ' Create the SelectCommand. cmd = New SqlCommand("SELECT * FROM Customers " & _ "WHERE Country = @Country AND City = @City", conn) cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15) cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15) da.SelectCommand = cmd ' Create the InsertCommand. cmd = New SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _ "VALUES (@CustomerID, @CompanyName)", conn) cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID") cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName") da.InsertCommand = cmd Return da End Function [C#] public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn) { SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd; // Create the SelectCommand. cmd = new SqlCommand("SELECT * FROM Customers " + "WHERE Country = @Country AND City = @City", conn); cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15); cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15); da.SelectCommand = cmd; // Create the InsertCommand. cmd = new SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " + "VALUES (@CustomerID, @CompanyName)", conn); cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); da.InsertCommand = cmd; return da; }
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET
SqlDataAdapter Class | SqlDataAdapter Members | System.Data.SqlClient Namespace | DeleteCommand | InsertCommand | UpdateCommand