Search code examples
c#.netado.netdatasetdataadapter

Looking for how to create a Generic CreateDataAdapter


to create a dataset such as

 System.Data.Common.DBConnection conn = getConn();
 DbCommand com = conn.CreateCommand();

 DbDataAdapter da = // get a datadapter from conn ??? There is NO CreateDataAdapter here ?
 da.SelectCommand = com;
 da.Fill(ds);

any help ?

UPDATE - I know there's no CreateDataAdapter() method there, looking for a workaround !


Solution

  • I'am not sure if understand correctly.. but maybe you want something like this:

     using (System.Data.Common.DBConnection conn = getConn())
     {
      DbCommand com = conn.CreateCommand();
      DbDataAdapter da = CreateAdapter(com);
      // .. properties of adapter ..
      da.Fill(ds);
     }
    

    EDIT (Not tested):

    you can create generic CreateAdapter method like this (will create adapter based on command type):

    private static DbDataAdapter CreateAdapter<T>(T a_command) where T: DbCommand
    {
         if (a_command is SqlCommand)
         {
            return new SqlDataAdapter();
         }
    
         // .. others adapters..
    
         return null;
    }