【问题标题】:Connect to oracle from c#从 c# 连接到 oracle
【发布时间】:2015-10-16 23:32:38
【问题描述】:

我正在尝试从 C# 连接 Oracle 11g。我已经关注 [this link],定义了这样的连接别名:

moviess =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = xe)
    )
  )

在 Visual Studio 中尝试使用服务器资源管理器进行连接时,出现以下错误:

这里是 Oracle Developer Tool for Visual Studio 的安装目录。

我哪里错了?请帮助我。

【问题讨论】:

  • 你把连接标识符放在哪里了?我认为 C# 默认情况下很难找到 TNSNames.ora,除非你告诉它它在哪里,或者在不使用 TNSNames 的情况下设置连接字符串。
  • 您是否从 Oracle 安装了 Visual Studio 的东西?我通常有更多的选择和要填写的东西……不确定……
  • 删除密码等后显示您的连接字符串。参考此链接:stackoverflow.com/questions/820440/…

标签: c# oracle


【解决方案1】:

您可以使用来自 NuGet 的 Oracle ODP.Net 驱动程序。

你可以试试这些:

  1. 右键单击References,然后点击Manage NuGet packages。
  2. 选择官方 Oracle ODP.Net,托管驱动程序

  1. 参考“Oracle.ManagedDataAccess.Client”
  2. 调用实际连接。

示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;

namespace SampleOracle
{
    class Program
    {
        static void Main(string[] args)
        {
            OracleConnection connection = new OracleConnection();
            connection.ConnectionString = "User Id=<username>;Password=<password>;Data Source=<data source>"; //Data Source Format -> //IP_HOST:PORT/SERVICE_NAME e.g. //127.0.0.1:1521/Service_Name
            connection.Open();
            Console.WriteLine("Connected to Oracle" + connection.ServerVersion);           
        }
    }
}

示例输出:

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    要到达您的Add Connection 窗口,您必须先通过此窗口,对吧?

    信息很清楚。您尝试使用的提供程序已弃用且不支持 Oracle 11。

    按照建议,请考虑下载并安装 Oracle Developer Tools for Visual Studio。

    编辑:我完全忘了提及这个非常重要的花絮,因为当你真正开始工作时:不要使用sys连接到 Oracle 数据库以进行开发。 sys 用户非常特殊,只能在绝对必要时使用。相反,创建一个单独的用户,并将其用于您的开发。

    【讨论】:

    • 我已经为 Visual Studio 安装了 Oracle Developer Tools。接下来我该怎么做? Visual Studio 如何知道它已安装以及从哪里获取 oracle 的连接字符串?
    【解决方案3】:

    这就是问题所在:

    ORA-12154: TNS: Could not resolve the connect identifier specified

    这就是原因:

    https://docs.oracle.com/cd/B19306_01/server.102/b14219/net12150.htm

    ORA-12154: TNS: 无法解析指定的连接标识符

    原因:使用连接标识符请求到数据库或其他服务的连接,而指定的连接标识符可以 不使用其中一种命名方式解析为连接描述符 配置的方法。

    例如,如果连接标识符的类型 used 是网络服务名称,那么网络服务名称不能是 在命名方法存储库中找到,或者该存储库无法 找到或到达。

    Action:
    
    - If you are using local naming (TNSNAMES.ORA file):
    
    - Make sure that "TNSNAMES" is listed as one of the values of the NAMES.DIRECTORY_PATH parameter in the Oracle Net profile (SQLNET.ORA)
    
    - Verify that a TNSNAMES.ORA file exists and is in the proper directory and is accessible.
    
    - Check that the net service name used as the connect identifier exists in the TNSNAMES.ORA file.
    

    问:Oracle 11g 是否真的安装在您的本地 PC 上?如果没有,则不能使用“localhost”。您还必须安装 Oracle 客户端。

    相关链接:

    【讨论】:

      猜你喜欢
      • 2010-12-10
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多