1

我已经在 StackOverflow 上搜寻了这个问题的答案,但似乎没有任何帮助,所以我现在就在这里问它:

因此,我有一个相当简单的表单,其中包含一些标签和字段以及一个按钮。

单击按钮后,您将对数据库执行查询。结果显示在多个字段中。当我在工作中使用 Visual Studio 2013 中的 F5 为自己运行该程序时,该表单使用 Oracle.ManagedDataAccess 引用完美运行。

但是,当我在另一台工作的 PC(相同的网络和所有网络)上运行该程序时,该程序返回一个 oracle“ORA-12154:TNS:无法解析指定的连接标识符”-错误。

您应该知道的:我设置了 Oracle.ManagedDataAccess 以便它使用位于 c:\Oracle\product\10.2.0\client_1\network\ADMIN 的标准 TNSNames.ora 文件。对于网络上的每台工作中的 PC,此位置都是相同的。但是,TNSNames.ora 文件并不总是像我一样完整,我可能比其他人有更多可用的连接。这就是为什么,正如您很快将在我的代码中看到的那样,我基本上说:“如果在位置 Y 的文档 X 中找到 SID xxxxx,则什么也不做,否则,添加此处指定的必要连接信息:...”

这是我使用的涉及所有连接的代码。出于隐私原因,我用假名替换了连接细节,并将一些内容从荷兰语翻译成英文,以便您更好地理解。

由于代码很长,我只展示了有关连接的部分,其他所有内容都省略了。但正如我之前所说,完整的代码只适用于我。我想让它为工作中的其他人工作。

using System;

using System.Collections.Generic;   
using System.Data;    
using System.Linq;   
using System.Windows.Formss    
using System.IO;

namespace WindowsFormsApplication4
{
    public partial class GetLivingWagesV2 : Form
    {
        public GetLivingWagesV2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("PLEASE SELECT AN ENVIRONMENT FIRST.");
            }

            var objtextbox = textBox1.Text;

            var ConnectionString350 = "DATA SOURCE=DATABASEPREREGRESSION01;PASSWORD=test;USER ID=REGGY";

            var ConnectionString300 = "DATA SOURCE=DATABASEREGRESSION01;PASSWORD=test;USER ID=REGGY";

            var Connection = new OracleConnection();

            if (comboBox1.SelectedIndex == 0)
            {
                var PEN300 = File.ReadAllText(@"C:\oracle\product\10.2.0\client_1\network\ADMIN\tnsnames.ora").Contains("PENREG.RSVZINASTI.BE") ? 1 : 0;

                Connection.ConnectionString = ConnectionString300;

                Connection.Open();

                var cmd = Connection.CreateCommand();

                cmd.CommandText = "SELECT DBMS_LOB.SUBSTR(b.msg_flux_in, 4000, 200) AS MSG_FLUX_IN, DBMS_LOB.SUBSTR(b.msg_flux_out, 4000, 200) AS MSG_FLUX_OUT FROM tb2b_flux a, tb2b_flux_status b where a.flux_svf_id = '" + objtextbox + "' and a.flux_id = b.flux_id order by a.dt_creatie desc";

                var Reader = cmd.ExecuteReader();


                var dt = new DataTable();

                dt.Load(Reader);

                dataGridView1.DataSource = dt;

                var column = dataGridView1.Columns[0];
                column.Width = 380;

                var column1 = dataGridView1.Columns[1];
                column1.Width = 380;

                if (PEN300 == 0)
                {
                    using (var file = new System.IO.StreamWriter(@"C:\oracle\product\10.2.0\client_1\network\ADMIN\tnsnames.ora", true))
                    {
                        file.WriteLine("DATABASEREGRESSION.ORGANISATION.BE =");
                        file.WriteLine("  (DESCRIPTION =");
                        file.WriteLine("    (ADDRESS_LIST =");
                        file.WriteLine("      (ADDRESS = (PROTOCOL = TCP)(HOST = unxin600)(PORT = 1522))");

                        file.WriteLine("    )");
                        file.WriteLine("    (CONNECT_DATA =");
                        file.WriteLine("      (SID = DATABASEREGRESSION01)");
                        file.WriteLine("    )");
                        file.WriteLine("  )");
                    }
                }
            }


            if (comboBox1.SelectedIndex == 1)
            {
                var PEN350 = File.ReadAllText(@"C:\oracle\product\10.2.0\client_1\network\ADMIN\tnsnames.ora").Contains("pendvt01.rsvzinasti.be") ? 1 : 0;

                if (PEN350 == 0)
                {
                    using (var file = new System.IO.StreamWriter(@"C:\oracle\product\10.2.0\client_1\network\ADMIN\tnsnames.ora", true))
                    {
                        file.WriteLine("DATABASEPREREGRESSION =");
                        file.WriteLine("  (DESCRIPTION =");
                        file.WriteLine("    (ADDRESS_LIST =");
                        file.WriteLine("      (ADDRESS = (PROTOCOL = TCP)(HOST = UNXPR651)(PORT = 1522))");

                        file.WriteLine("    )");
                        file.WriteLine("    (CONNECT_DATA =");
                        file.WriteLine("      (SERVICE_NAME =DATABASEPREREGRESSION01)");
                        file.WriteLine("    )");
                        file.WriteLine("  )");
                    }
                }

                else
                {
                    Connection.ConnectionString = ConnectionString350;

                    Connection.Open();

                    var cmd = Connection.CreateCommand();

                    cmd.CommandText = "SELECT DBMS_LOB.SUBSTR(b.msg_flux_in, 4000, 200) AS MSG_FLUX_IN, DBMS_LOB.SUBSTR(b.msg_flux_out, 4000, 200) AS MSG_FLUX_OUT FROM tb2b_flux a, tb2b_flux_status b where a.flux_svf_id = '" + objtextbox + "' and a.flux_id = b.flux_id order by a.dt_creatie desc";

                    var Reader = cmd.ExecuteReader();


                    var dt = new DataTable();

                    dt.Load(Reader);

                    dataGridView1.DataSource = dt;

                    var column = dataGridView1.Columns[0];
                    column.Width = 380;

                    var column1 = dataGridView1.Columns[1];
                    column1.Width = 380;
                }
            }    

            if (Connection != null && Connection.State == ConnectionState.Closed)
            {
                Connection.Open();
            }

            else
            {
                Connection.Close();
            }



            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                foreach (DataGridViewCell dc in dr.Cells)
                {
                    if (dc.Value == null || dc.Value.ToString().Trim() == string.Empty)
                    {
                        if (dataGridView1.Rows.Count < 2)
                        {
                            MessageBox.Show("De FluxID werd niet gevonden.");
                            return;
                        }
                    }
                    else
                    {
                    }
                }
            }
        }
    }
}
4

2 回答 2

2

tnsnames.ora文件可能未使用,因为连接字符串使用 EZ CONNECT(易连接)格式。尝试修改连接字符串中的数据源:

DATA SOURCE=HOSTNAME:1521/DATABASENAME;PASSWORD=...;USER ID=...

对于 Windows 窗体应用程序来说,尝试不tnsnames.ora这样做是有意义的,因为它会使分发和应用程序支持更容易。不需要处理这个文件,它只是一个不受您控制的外部元素,需要处理,它的安装和更新,这可能意味着还需要解析文件以对其进行更新,可以更改由另一个用户或另一个应用程序意外。应用程序配置文件中的单行连接字符串比较少麻烦。

ODP.NET 提供程序的连接字符串格式概述如下: https ://www.connectionstrings.com/oracle-data-provider-for-net-odp-net/

于 2016-02-25T21:53:17.523 回答
0

我建议将您的服务器信息放在您的web/app.config. 这样,客户甚至不需要有tnsnames.ora文件,甚至不需要安装 oracle。

以下是您可能需要添加到web/app.config文件中以使其正常工作的部分。我通常在连接名称后面加上.managed以确保没有命名冲突(理论上,客户端应该在web/app.config去之前查看tnsnames.ora,但我不信任 oracle,这使得它非常明确:)

<configuration>
    <configSections>
        <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </configSections>
    <connectionStrings>
        <add name="DbContext" connectionString="DATA SOURCE=XXXXX.managed;USER ID=XXXXX;PASSWORD=XXXXXXX;PERSIST SECURITY INFO=True;POOLING=False" providerName="Oracle.ManagedDataAccess.Client" />
    </connectionStrings>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <publisherPolicy apply="no" />
                <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>

    <!-- add this if you use Entity Framework only -->
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
            <provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
        </providers>
    </entityFramework>
    <!-- here's the important bit, just copy descriptor from your existing tnsnames.ora file -->
    <oracle.manageddataaccess.client>
        <version number="*">
            <dataSources>
                <dataSource alias="XXXXX.managed" descriptor="(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = XXXXXX.com)(PORT = 999999))(CONNECT_DATA = (SERVICE_NAME = XXXXX)(SERVER = dedicated)))" />
            </dataSources>
        </version>
    </oracle.manageddataaccess.client>
    <system.data>
        <DbProviderFactories>
            <remove invariant="Oracle.ManagedDataAccess.Client" />
            <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
        </DbProviderFactories>
    </system.data>
</configuration>

之后,您可以更改代码以使用ConfigurationManager.ConnectionStrings(或等效的,取决于版本),而不是将字符串硬编码到代码中(因为这通常是一种不好的做法)。

于 2016-02-25T22:25:58.560 回答