简体   繁体   中英

Type or namespace name Oracle could not found Missing Directive

I want to build simple console application in C#.Net which will take name of table as argument and display all the data in that table. So I write the following code in C#

using System;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;

namespace Ass1Que1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Please replace the connection string attribute settings
                string constr = "DATA SOURCE=localhost:1521/orclpdb;PERSIST SECURITY INFO=True;USER ID=HR;password=hr";

                OracleConnection con = new OracleConnection(constr);
                con.Open();
                Console.WriteLine("Connected to Oracle Database {0}", con.ServerVersion);
                // con.Dispose();
                OracleCommand cmd = con.CreateCommand();
                cmd.CommandText = "SELECT FIRST_NAME FROM EMPLOYEES";
                OracleDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("" + reader.GetString(0));
                }
                Console.WriteLine("Press RETURN to exit.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : {0}", ex);
                Console.ReadKey();
            }
        }
    }
    }

But I am getting error as

 type or namespace name Oracle could not found. 

I know I have to add some dll reference but I dont know how to do that. Please suggest me how to get this from error.

Thanks in advance!

You need to add a reference to the DLL that contains the Oracle.ManagedDataAccess namespace. It appears that is part of the "Oracle Data Provider for .NET, Managed Driver" provided by Oracle (link: https://docs.oracle.com/database/121/ODPNT/installODPmd.htm#ODPNT8149 )

The easiest way to do that (assuming you are using Visual Studio) is to right-click on your Project in the Solution Explorer and select "Manage NuGet Packages". Then Browse to find the "ODP.NET, Managed Driver", and add it to your project.

Alternatively, you can manually install the Oracle Data Provider for .NET onto your machine, then right-click the "References" entry under your project, and select "Add Reference". From here, browse to find the "Oracle.ManagedDataAccess.dll" on your filesystem, and add the reference to that. It should be in "ORACLE_BASE\\ORACLE_HOME\\odp.net\\bin\\4".

Hope this helps

You are missing to add the built in oracle reference dll provided by the .net framework.

  • Add it ,if you are in Visual studio 2015 right click on References -> Add References .

在此处输入图片说明 Now use the referenced dll in your code file by using statement

在此处输入图片说明

Green squiggly is a warning sign not to use the obsolete method.

This will resolve the Oracle connection issue but you also need to download and install Oracle.ManagedDataAccess via nuget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM