Click here to Skip to main content
11,709,536 members (52,330 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ADO.NET
Hi,

how to convert System.Data.OleDb.OleDbDataReader to System.Data.Common.DbDataReader
dynamically in C#

public T GetProjectCodes<t> (string Args) 
        {
           string strProjects = "";
           
            string QryAllProject ="select * from tbl_Project_Details";
 
            if (Args == "S")
            {
                strProjects = MyTest.GetRecordString(gcnPROJECT, QryAllProject, "*", "\n");
                return (T)Convert.ChangeType(strProjects, typeof(T));
            }
            else
            {
                DbDataReader Dr = null;
                Dr = MyTest.GetDataReader(gcnPROJECT, QryAllProject);// this function returns Dbdatareader 
                
                //return (T)Convert.ChangeType(Dr, typeof(T));
                //return (T)Convert.ChangeType(Dr, typeof(Dr));
 
                return (T)Convert.ChangeType(Dr, typeof(T));               
 
            }           
            
         // return (T) strProjects;
        }
 
  public static DbDataReader GetDataReader(DbConnection myConnection, string strQry)
        {
            DbDataReader ObjmyReader0 = null;
            DbDataReader ObjmyReader = null;
 

            string RetVal = string.Empty;
            DbCommand myCommand = null;
             myCommand = (DbCommand)myConnection.CreateCommand();
            // myCommand.CommandText = "SELECT * FROM Dept";
            myCommand.CommandText = strQry;
            if (myConnection.State == ConnectionState.Closed || myConnection.State == ConnectionState.Broken) {
                myConnection.Open();
            }
 
            try
            {
                if (ObjmyReader != null)
                {
                    if (ObjmyReader.IsClosed == false) 
                    {
                        ObjmyReader.Close();
                    }
                }
 
                ObjmyReader = (DbDataReader)myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
              
            }
            catch (Exception rdExc)
            {
                if ((ObjmyReader != null) && (ObjmyReader.IsClosed == false))
                {
                    ObjmyReader.Close();
                    ObjmyReader = (DbDataReader)myCommand.ExecuteReader();
                }
            }
            finally
            {
                myCommand.Dispose();
                ObjmyReader0 = ObjmyReader;
               
               // ObjmyReader.Close();
            }
 
            return ObjmyReader0;
            //return (DbDataReader)Convert.ChangeType(ObjmyReader0, typeof(DbDataReader));
            ObjmyReader0.Dispose();
        }
Please check and guide me...
thanks
vilas
Posted 10-Jan-13 23:30pm
Mogya1.7K
Edited 10-Jan-13 23:35pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why would you want to do that? The OleDbDataReader inherits from DbDataReader class (see MSDN[^]) , hence you may return it directly, without any 'conversion'.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 OriginalGriff 250
2 Maciej Los 225
3 Peter Leow 219
4 Mika Wendelius 160
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,037
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100