从 oracle DB 获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11329058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:56:51  来源:igfitidea点击:

Fetching data from oracle DB

c#.netsqloracle

提问by user1501034

I use the below code to connect to my oracle DB and execute a query. The query I used in the example simply fetches a set of rows from a table. However, I keep getting an error message that "The table or view does not exist". But, I am pretty sure that the table exists in the DB. Where am I going wrong?

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

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

The error is triggered when command.ExecuteNonQuery() is reached.

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

采纳答案by Asif Mushtaq

I think the problem is with command.ExecuteNonQuery();

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

Actually you are executing a query here therefore you should use either DataAdapteror DataReader.

实际上,您正在此处执行查询,因此您应该使用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();
}

回答by Habib

Task is oracle Reserve Word, that is why you are getting this error. Use double quotes.

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

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

ExectueNonQuery, may not give you any error, but it will not give you the desired result. You need to do command.ExecuteReader. See the link.

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

You may also see this Getting Started with Oracle Data Provider for .NET (C# Version)

您还可能会看到适用于 .NET 的 Oracle 数据提供程序入门(C# 版)