Oracle database TNS The value's length for key 'data source' exceeds it's limit of '128'

做~自己de王妃 提交于 2019-11-30 22:13:35

You haven't shown us the full code you use for connecting to a database, but it seems from a comment to another answer that you are using OLE DB. I would avoid using it, especially if it seems to have a somewhat arbitrary limit of 128 characters for a data source.

I would also point out that you can also avoid having to install an Oracle client, as recommended by another answerer. I don't have much experience with the 'instant' client, but the full client is a hefty download and isn't necessary just to be able to connect a C# program to Oracle.

Instead, we can use the Oracle Managed Data Access library. You can install this using NuGet. To do this:

  • go to Tools > Library Package Manager > Package Manager Console,
  • ensure that the correct project is chosen in the 'Default project' drop-down list,
  • enter

    Install-Package odp.net.managed
    

This should add download the library from NuGet and add Oracle.ManagedDataAccess to the References of your project.

Then, if you add a using directive for Oracle.ManagedDataAccess.Client, the following code should talk to an Oracle database:

string connStr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<hostname>)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=<service_name>)));User Id=<user>;Password=<password>";
Console.WriteLine("Connection string has length " + connStr.Length);
using (var connection = new OracleConnection() { ConnectionString = connStr })
{
    connection.Open();
    OracleCommand command = new OracleCommand("SELECT * FROM DUAL", connection);
    using (OracleDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            Console.WriteLine(reader.GetString(0));
        }
    }
}

Here is what my app.config looks like for ODP.NET which I access via customized Enterprise Library (I converted data layer of EnterpriseLibrary5.0 from System.Data.OracleClient to ODP.NET's Oracle.DataAccess.Client. Note that publickey is my own produced one which is unique, and the version is again my own version number). Why did I customize it, well it's because System.Data.OracleClient cannot handle CLOB if it exceeds 32kb.

<configSections>
  <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.1.0.0, Culture=neutral, PublicKeyToken=4133a635bb2789db" requirePermission="true" />
</configSections>

<dataConfiguration defaultDatabase="DatabaseConnectionString" />
<connectionStrings>
    <add name="DatabaseConnectionString" connectionString="Data Source=TestDb;Persist Security Info=True;User ID=Usrname;Password=Pwd!;Max Pool Size=500;" providerName="Oracle.DataAccess.Client" />        
</connectionStrings>

And my TNS in tnsnames.ora file

TestDb=
  (DESCRIPTION=
    (ADDRESS=
      (PROTOCOL=TCP)
      (HOST=10.10.10.1)
      (PORT=1234)
    )
    (CONNECT_DATA=
      (SID=TestDb)
    )
  )

Hope that app.config and tnsnames.ora help you figure it out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!