无法在C#中隐式转换类型'System.Data.SqlClient.SqlDataReader'为'System.Data.OracleClient.OracleDataReader'

时间:2013-12-24 18:12:53

标签: c#

我在C#中创建了一个类,我的代码显示错误。 错误是=> 无法将类型'System.Data.SqlClient.SqlDataReader'隐式转换为'System.Data.OracleClient.OracleDataReader'

我收到错误返回SqlHelper.ExecuteReader(CnSettings.cnString1,CommandType.Text,commandText);

public OracleDataReader getWeightmntInfoForCenter(string startdate, string enddate, string unitcode)
{
   string commandText = string.Concat(new string[]
   {
      "select m.unitcode unit_code, to_char(tran_date, 'dd-MON-yy') m_date,
      to_char(m_purchy) m_number, net AS n_weight, to_number(substr(g.ryot_code,5))
      m_grow, to_number(substr(g.ryot_code,1,4)) m_vill, g.mob g_phoneno, 
      m.s_purchy indent_number from sisl_purpurchase m,cn_ryot_mst g where 
      m.unitcode=g.unit_code and lpad(v_code,4,0)||lpad(f_code,5,0)=g.ryot_code And
      length(g.mob)=10 And m.tran_date between '", startdate, "' and '", enddate, 
      "' And nvl(m_sms_send, 0)=0 AND m.unitcode = '",unitcode,"' And rownum <= 10"
   });
   return SqlHelper.ExecuteReader(CnSettings.cnString1, CommandType.Text, commandText);
}

3 个答案:

答案 0 :(得分:2)

SqlHelper.ExecuteReader返回了错误的类型 - SqlDataReader;当您的getWeightmntInfoForCenter方法期望返回OracleDataReader

如果要返回SqlDataReader,可以更改此方法的签名:

public IDataReader getWeightmntInfoForCenter(...)

这指定此方法将返回实现IDataReader的类,但不会精确指定将具体的类型,这意味着您可以返回SqlDataReader an OracleDataReader(因为这两个类都实现了这个接口)。

答案 1 :(得分:0)

getWeightmntInfoForCenter方法签名返回typeOracleDataReader,当您实际返回SqlDataReader时,请查看:

return SqlHelper.ExecuteReader(...

将您的代码更改为:OracleCommand.ExecuteReader方法,以便正确返回OracleDataReader

这样做:

    public OracleDataReader GetWeightmntInfoForCenter(string startdate, string enddate, string unitcode)
    {
        string commandText = string.Concat(new[]
            {
                "select m.unitcode unit_code, to_char(tran_date, 'dd-MON-yy') m_date, to_char(m_purchy) m_number, net AS n_weight, to_number(substr(g.ryot_code,5)) m_grow, to_number(substr(g.ryot_code,1,4)) m_vill, g.mob g_phoneno,m.s_purchy indent_number from sisl_purpurchase m,cn_ryot_mst g where m.unitcode=g.unit_code and lpad(v_code,4,0)||lpad(f_code,5,0)=g.ryot_code And length(g.mob)=10 And m.tran_date between '"
                , startdate, "' and '", enddate, "' And nvl(m_sms_send, 0)=0 AND m.unitcode = '", unitcode,
                "' And rownum <= 10"
            });

        using (var connection = new OracleConnection(CnSettings.cnString1))
        {
            var command = new OracleCommand(commandText, connection);
            connection.Open();
            return command.ExecuteReader();
        }
    }

答案 2 :(得分:0)

SqlHelper.ExecuteReader()会返回SqlDataReader类型的对象,但您的方法的返回类型为OracleDataReader,这是不同的。

您可以更改方法签名以返回DbDataReader。 Oracle和Sql Server的特定数据读取器类都来自该通用数据库:

public DbDataReader getWeightmntInfoForCenter(...)
{
    ...
}

另一方面,如果您正在使用Oracle数据库,则无法使用SqlHelper.ExecuteReader(),因为它是SQL Server的特定实现。请改为查看OracleCommand.ExecuteReader()