本文介绍了在C#中将oledbdatareader转换为dbdatareader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何在C#中动态地将System.Data.OleDb.OleDbDataReader转换为System.Data.Common.DbDataReader





 public T GetProjectCodes< t> (字符串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); //此函数返回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();
}

尝试
{
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();
}
}
最后
{
myCommand.Dispose();
ObjmyReader0 = ObjmyReader;

// ObjmyReader.Close();
}

返回ObjmyReader0;
// return(DbDataReader)Convert.ChangeType(ObjmyReader0,typeof(DbDataReader));
ObjmyReader0.Dispose();
}



请检查并指导我...

谢谢

vilas

解决方案

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

解决方案


这篇关于在C#中将oledbdatareader转换为dbdatareader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:39