2012/08/13

Add parameters in oledb and odbc CommandText

OleDb:
using (OleDbCommand cmd = new OleDbCommand())
{
  cmd.CommandText = "Select * from VIP where ID = ? And RegDate >= ?";
  cmd.Parameters.AddWithValue("id", id);
  cmd.Parameters.AddWithValue("regDate", regDate);
  using (OleDbDataReader reader = cmd.ExecuteReader())
  {
    // ..........
  }
}
Odbc:
using (OdbcCommand cmd = new OdbcCommand())
{
  cmd.CommandText = "Select * from VIP where ID = ? And RegDate >= ?";
  cmd.Parameters.Add("id", OdbcType.Int).Value = id;
  cmd.Parameters.Add("regDate", OdbcType.DateTime).Value = regDate;
  using (OdbcDataReader reader = cmd.ExecuteReader())
  {
    // ..........
  }
}

1. The name of parameter doesn't matter, but the order of parameter does.
2. I didn't include full codes here so don't forget to fill in other components like OleDbConnection or OdbcConnection.

No comments:

Post a Comment