0

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

这是我的代码:

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

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

4

1 回答 1

0

尝试更换这个

cmd = db.GetSqlStringCommand(query);

有了这个:

cmd = con.CreateCommand();
cmd.CommandText = query;
于 2015-03-23T14:09:28.477 回答