简体   繁体   English

通过端口号,服务名称和数据库从C#进行Oracle连接(ORA-12514

[英]Oracle connection with port, service name, and database from C# (ORA-12514

I'm new to Oracle. 我是Oracle的新手。 Trying to connect C# windows app to an Oracle database but can't seem to establish a proper connection. 尝试将C#Windows应用程序连接到Oracle数据库,但似乎无法建立适当的连接。 Keep getting exception: "ORA-12514: TNS:listener does not currently know of service requested in connect descriptor". 不断出现异常:“ ORA-12514:TNS:listener当前不知道连接描述符中请求的服务”。 I have to specify the port, service name, and database name in the connection string because the service id has access to multiple databases. 我必须在连接字符串中指定端口,服务名称和数据库名称,因为服务ID可以访问多个数据库。 I know that the values in the string are valid (valid server, valid serviceid, valid username, password, etc) because I have a third-party tool that is able to connect using the same parameters from a wizard. 我知道字符串中的值是有效的(有效的服务器,有效的serviceid,有效的用户名,密码等),因为我有一个第三方工具,该工具能够使用向导中的相同参数进行连接。 I've tried a lot of different ways to format the connection string but I always get the same 12514 error. 我尝试了许多不同的方式来格式化连接字符串,但是我总是遇到相同的12514错误。 In the code example, you'll see three formats (cxn, cxn2, and cxn3), I've tried each of them but get the same error. 在代码示例中,您将看到三种格式(cxn,cxn2和cxn3),我尝试了每种格式,但得到相同的错误。

        string cxn = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyServerName)(PORT=MyPortNumber))" +
            "(CONNECT_DATA=(SERVICE_NAME=MyServiceId)));User Id=MyUserName; Password=MyPassword;";

        string cxn2 = "DATA SOURCE=MyServerName:MyPortNumber/MyUserName;" +
            "PERSIST SECURITY INFO=True;USER ID=MyUserName; password=MyPassword; Pooling = False;";

        string cxn3 = "DATA SOURCE=MyServerName:MyPortNumber/MyServiceId;" +
            "PERSIST SECURITY INFO=True;USER ID=MyUserName; password=MyPassword; Pooling = False;";

        using (OracleConnection conn = new OracleConnection(cxn3))
        {
            string sqlSelect = "SELECT * FROM PERSONS";
            using (OracleDataAdapter da = new OracleDataAdapter(sqlSelect, conn))
            {
                var table = new DataTable();
                da.Fill(table);

                if (table.Rows.Count > 1)
                    Console.WriteLine("Successfully read oracle.");
            }
        }

Again, I've used MyServiceId in the third-party tool's wizard and I connect just fine and select my database. 同样,我在第三方工具的向导中使用了MyServiceId,并且可以很好地连接并选择数据库。 I'm using Oracle.ManagedDataAccess.Client. 我正在使用Oracle.ManagedDataAccess.Client。 I consulted a number of articles online including Oracle's guidance in section "Getting Started with ODP.NET, Managed Driver" . 我在网上查阅了许多文章,包括“可管理驱动程序ODP.NET入门”部分中的Oracle指南 How can I get the driver to recognize the valid service id and then also accept the database name? 如何使驱动程序识别有效的服务ID,然后接受数据库名称? Any guidance is appreciated. 任何指导表示赞赏。 Thanks. 谢谢。

Well I wish I had a more definitive explanation but as it turns out the code from my original question works NOW using the connection string defined in variable "cxn". 好吧,我希望我能有一个更明确的解释,但是事实证明,我原来的问题中的代码现在使用变量“ cxn”中定义的连接字符串有效。 I ran it many times before with no success, so my only guess is that the DBA changed something or rebooted the server since initial configuration. 我之前运行了很多次都没有成功,所以我唯一的猜测是自初始配置以来,DBA更改了某些内容或重新启动了服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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