类型或命名空间名称 Oracle 找不到丢失的指令
Type or namespace name Oracle could not found Missing Directive
我想在 C#.Net 中构建简单的控制台应用程序,它将 table 的名称作为参数并显示 table 中的所有数据。
所以我用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();
}
}
}
}
但我收到错误
type or namespace name Oracle could not found.
我知道我必须添加一些 dll 引用,但我不知道该怎么做。
请建议我如何从错误中得到这个。
提前致谢!
您需要添加对包含 Oracle.ManagedDataAccess 命名空间的 DLL 的引用。它似乎是 Oracle 提供的 "Oracle Data Provider for .NET, Managed Driver" 的一部分 (link: https://docs.oracle.com/database/121/ODPNT/installODPmd.htm#ODPNT8149 )
最简单的方法(假设您使用的是 Visual Studio)是在解决方案资源管理器中右键单击您的项目,然后 select "Manage NuGet Packages"。然后浏览找到 "ODP.NET, Managed Driver",并将其添加到您的项目中。
或者,您可以手动将 Oracle Data Provider for .NET 安装到您的机器上,然后右键单击项目下的 "References" 条目,然后 select "Add Reference"。从这里,浏览找到文件系统上的 "Oracle.ManagedDataAccess.dll",并添加对它的引用。它应该在 "ORACLE_BASE\ORACLE_HOME\odp.net\bin".
希望对您有所帮助
您缺少添加 .net 框架提供的内置 oracle 参考 dll。
- 添加它,如果你在 Visual studio 2015 中右键单击 References-> Add References.
现在通过 using 语句
在代码文件中使用引用的 dll
绿色波浪线是不要使用过时方法的警告标志。
这将解决 Oracle 连接问题,但您还需要通过 nuget 下载并安装 Oracle.ManagedDataAccess
我想在 C#.Net 中构建简单的控制台应用程序,它将 table 的名称作为参数并显示 table 中的所有数据。 所以我用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();
}
}
}
}
但我收到错误
type or namespace name Oracle could not found.
我知道我必须添加一些 dll 引用,但我不知道该怎么做。 请建议我如何从错误中得到这个。
提前致谢!
您需要添加对包含 Oracle.ManagedDataAccess 命名空间的 DLL 的引用。它似乎是 Oracle 提供的 "Oracle Data Provider for .NET, Managed Driver" 的一部分 (link: https://docs.oracle.com/database/121/ODPNT/installODPmd.htm#ODPNT8149 )
最简单的方法(假设您使用的是 Visual Studio)是在解决方案资源管理器中右键单击您的项目,然后 select "Manage NuGet Packages"。然后浏览找到 "ODP.NET, Managed Driver",并将其添加到您的项目中。
或者,您可以手动将 Oracle Data Provider for .NET 安装到您的机器上,然后右键单击项目下的 "References" 条目,然后 select "Add Reference"。从这里,浏览找到文件系统上的 "Oracle.ManagedDataAccess.dll",并添加对它的引用。它应该在 "ORACLE_BASE\ORACLE_HOME\odp.net\bin".
希望对您有所帮助
您缺少添加 .net 框架提供的内置 oracle 参考 dll。
- 添加它,如果你在 Visual studio 2015 中右键单击 References-> Add References.
绿色波浪线是不要使用过时方法的警告标志。
这将解决 Oracle 连接问题,但您还需要通过 nuget 下载并安装 Oracle.ManagedDataAccess