使用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;
   }
 }

你能帮我解决这个问题吗。

尝试替换此

cmd = db.GetSqlStringCommand(query);

这个:

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

相关内容

  • 没有找到相关文章

最新更新