Click here to Skip to main content
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.6K
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 547
1 Member 11120668 188
2 ChauhanAjay 180
3 Maciej Los 165
4 CHill60 135
0 Sergey Alexandrovich Kryukov 9,735
1 OriginalGriff 6,987
2 CPallini 5,835
3 George Jonsson 4,017
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
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