从oracle DB获取数据

时间:2012-07-04 12:23:45

标签: c# .net sql oracle

我使用以下代码连接到我的oracle DB并执行查询。我在示例中使用的查询只是从表中提取一组行。但是,我不断收到“表或视图不存在”的错误消息。但是,我很确定该表存在于DB中。我哪里错了?

 public void UpdateDatabase()
        {
            System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection();
            conn.ConnectionString = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.144)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)));UID=mwm;PWD=mwm";
            conn.Open();
            OracleCommand command = conn.CreateCommand();
            command.CommandText = "Select * from Task"; 
            command.ExecuteNonQuery();
            command.Dispose();
        }

达到command.ExecuteNonQuery()时会触发错误。

2 个答案:

答案 0 :(得分:2)

我认为问题出在command.ExecuteNonQuery();

实际上您在这里执行查询,因此您应该使用DataAdapterDataReader

public void UpdateDatabase()
{
   System.Data.OracleClient.OracleConnection conn = new    System.Data.OracleClient.OracleConnection();
  conn.ConnectionString = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.144)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)));UID=mwm;PWD=mwm";
     conn.Open();
     OracleCommand command = conn.CreateCommand();            
     SqlDataAdapter a = new SqlDataAdapter("Select * from \"Task\"", command))            
     DataTable t = new DataTable();
     a.Fill(t);
     command.Dispose();
}

答案 1 :(得分:1)

任务是oracle Reserve Word,这就是你收到此错误的原因。使用双引号。

command.CommandText = "Select * from \"Task\"";

ExectueNonQuery,可能不会给你任何错误,但它不会给你想要的结果。你需要做command.ExecuteReader。请参阅链接。

您可能还会看到此Getting Started with Oracle Data Provider for .NET (C# Version)