Yes it is possible, you just need to change the command of the dataconnection if you want to change the query with which you getting the data from DB.
Here is how you can change the query of a DB data connection in code
ADOAdapterObject oAdapter = (ADOAdapterObject) thisXDocument.DataAdapters["DatabaseConnectioName"];
//Here you can write any code that you want.
oAdapter.Command = string.Format("select * from table1 where date1 > date2 and date3 < date2");
oAdapter.Query();
Agni
InfoPath Dev India