Click here to Skip to main content
12,622,652 members (28,701 online)
Rate this:
 
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 11-Jan-13 0:30am
Mogya1.8K
Updated 11-Jan-13 0:35am
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2016
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