使用DbDataReader从数据库读取数据时出错

时间:2015-03-23 13:55:46

标签: c# asp.net oracle dbdatareader

我想从oracle数据库中读取数据并将该数据添加到List中。但是当我运行应用程序时,我得到了'无效的操作。连接已关闭。错误。

这是我的代码:

public class NewOtherCompanyMapper
{
    OtherCompany om;
    private Database db;
    private DbCommand cmd;
    private DbConnection con;

     public NewOtherCompanyMapper(OtherCompany om_temp)
    {
        om = om_temp;
        db = DatabaseFactory.CreateDatabase("NDA_generator");
    }

   public List<OtherCompany> getCompanyDetails()
    {
        List<OtherCompany> list = new List<OtherCompany>();
        OtherCompany oc = new OtherCompany();

        try
        {
            con = db.CreateConnection();
            con.Open();

            string query = "SELECT * FROM OtherCompanyData";
            cmd = db.GetSqlStringCommand(query);
            DbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                oc = new OtherCompany(reader["RegNumber"].ToString(), reader["ComName"].ToString(), reader["Country"].ToString(), reader["Address"].ToString(), reader["CoreBusi"].ToString());
                list.Add(oc);
            }

        }

        catch (Exception ex)
        {
            throw ex;
        }
       return list;
   }
 }

你能帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试替换此

cmd = db.GetSqlStringCommand(query);

用这个:

cmd = con.CreateCommand();
cmd.CommandText = query;