Export (0) Print
Expand All

IDbDataAdapter.SelectCommand Property

Gets or sets an SQL statement used to select records in the data source.

[Visual Basic]
Property SelectCommand As IDbCommand
[C#]
IDbCommand SelectCommand {get; set;}
[C++]
__property IDbCommand* get_SelectCommand();
__property void set_SelectCommand(IDbCommand*);
[JScript]
function get SelectCommand() : IDbCommand;function set SelectCommand(IDbCommand);

Property Value

An IDbCommand that is used during Update to select records from data source for placement in the data set.

Remarks

When SelectCommand is assigned to a previously created IDbCommand, the IDbCommand is not cloned. The SelectCommand maintains a reference to the previously created IDbCommand object.

If the SelectCommand does not return any rows, no tables are added to the DataSet, and no exception is raised.

Example

[Visual Basic, C#, C++] The following example creates an instance of the inherited OleDbDataAdapter class, and sets the SelectCommand and InsertCommand properties. It assumes you have already created an OleDbConnection object.

[Visual Basic] 
Public Shared Function CreateCustomerAdapter(conn As OleDbConnection) As OleDbDataAdapter 
  
  Dim da As OleDbDataAdapter = New OleDbDataAdapter()
  Dim cmd As OleDbCommand

  ' Create the SelectCommand.

  cmd = New OleDbCommand("SELECT * FROM Customers " & _
                       "WHERE Country = @Country AND City = @City", conn)

  cmd.Parameters.Add("@Country", OleDbType.VarChar, 15)
  cmd.Parameters.Add("@City", OleDbType.VarChar, 15)

  da.SelectCommand = cmd

  ' Create the InsertCommand.

  cmd = New OleDbCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
                       "VALUES (@CustomerID, @CompanyName)", conn)

  cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")
  cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName")

  da.InsertCommand = cmd

  Return da
End Function

[C#] 
public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection conn)
{
  OleDbDataAdapter da = new OleDbDataAdapter();
  OleDbCommand cmd;

  // Create the SelectCommand.

  cmd = new OleDbCommand("SELECT * FROM Customers " +
                       "WHERE Country = @Country AND City = @City", conn);

  cmd.Parameters.Add("@Country", OleDbType.VarChar, 15);
  cmd.Parameters.Add("@City", OleDbType.VarChar, 15);

  da.SelectCommand = cmd;

  // Create the InsertCommand.

  cmd = new OleDbCommand("INSERT INTO Customers (CustomerID, CompanyName) " +
                       "VALUES (@CustomerID, @CompanyName)", conn);

  cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID");
  cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName");

  da.InsertCommand = cmd;

  return da;
}

[C++] 
public:
static OleDbDataAdapter* CreateCustomerAdapter(OleDbConnection* conn)
{
  OleDbDataAdapter* da = new OleDbDataAdapter();
  OleDbCommand* cmd;

  // Create the SelectCommand.

  cmd = new OleDbCommand(S"SELECT * FROM Customers " 
                         S"WHERE Country = @Country AND City = @City", conn);

  cmd->Parameters->Add(S"@Country", OleDbType::VarChar, 15);
  cmd->Parameters->Add(S"@City", OleDbType::VarChar, 15);

  da->SelectCommand = cmd;

  // Create the InsertCommand.

  cmd = new OleDbCommand(S"INSERT INTO Customers (CustomerID, CompanyName) " 
                         S"VALUES (@CustomerID, @CompanyName)", conn);

  cmd->Parameters->Add(S"@CustomerID", OleDbType::Char, 5, S"CustomerID");
  cmd->Parameters->Add(S"@CompanyName", OleDbType::VarChar, 40, S"CompanyName");

  da->InsertCommand = cmd;

  return da;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

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

See Also

IDbDataAdapter Interface | IDbDataAdapter Members | System.Data Namespace

Show:
© 2014 Microsoft