I'm fetching data from a PostgreSQL 9.0 database, using the latest
psqlODBC-driver. I add the database as a data source in Visual Studio, and auto-generate a
DataSet
of some tables. I get the '
Fill
' and '
Get
' methods by default, but I'd like to have methods with multiple filtering parameters. This is where the problem starts when not using a Microsoft SQL. Luckily, this turned out to be no problem when I first found out that in
ODBC, the parameters are positional, and not named.
So the tip is: Write the new methods in a partial class of your auto-generated table adapter. Write your SQL with '?' where you want an input parameter. Then add the parameters to your ODBC-command according to the order of the inserted '?'.
public int FillByTimeFrame(MyDataSet.MyDataTable dataTable,
DateTime startTime, DateTime endTime)
{
var cmd = new System.Data.Odbc.OdbcCommand();
cmd.Connection = Connection;
cmd.CommandText = "SELECT * FROM \"public\".\"my_view\"" +
"WHERE (\"timestamp\" > ?) AND (\"timestamp\" <= ?)";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@startTime", startTime);
cmd.Parameters.AddWithValue("@endTime", endTime);
this.Adapter.SelectCommand = cmd;
if (this.ClearBeforeFill)
dataTable.Clear();
int nbrRowsAffected = this.Adapter.Fill(dataTable);
return nbrRowsAffected;
}