Hi All,
I often tend to fetch data from datasource using DataReader , these are usually a single data or single record of data cases. Within same method I reach to several datasource tables, each using datareader. This is awkward and cumbersome. Is it possible to create a class to which we send "select a,b,c from table1" , "where c=d", "group by a", "order by a,b desc" like;
private void toolStripButton1_Click(object sender, EventArgs e)
{
SqlImmediate(sSqlCommand, ifWhere , ifGroupby , ifOrderby, ifBinding)
}
public void SqlImmediate( string selectFrom, string sWhere , string sGroupby , string sOrderby, string listBindingstring )
{
// 1. Instantiate the connection
OleDbConnection cn = null;
cn = new OleDbConnection(strConn);
cn.Open();
// 2. Pass the connection to a command object
string selectCommand=selectFrom+sWhere+sGroupby+sOrderby
OleDbCommand myAccessCommand = new OleDbCommand(selectCommand, cn);
// 3. Call Execute reader to get query results
try
{
OleDbDataReader r = myAccessCommand.ExecuteReader();
if (r.Read())
{
// here somehow split listBindingstring into elements
string sm = r.GetString(2).ToString();
//as many as the number of elements parsed from listBindingstring
// here I like to be able to get all the fields binded to my textboxes/variables to be used anyplace in the form
}
}
I hope I explained what I mean. This reading with datareader many times for different datasource tables within the same procedure is really a pain on the neck, maybe I'm doing something very naive. Am I on the right track to attack this issue or is there much simpler approaches. I welcome any idea.
Thanks in advance
snky