如何使我的 c# 表单与其他人的 oracle 一起使用

本文关键字:oracle 一起 其他人 何使 我的 表单 | 更新日期: 2023-09-27 18:36:12

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

所以,我有一个相当简单的表单,有几个标签和字段以及一个按钮。

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

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

你应该知道的:我设置了Oracle.ManagedDataAccess,以便它使用位于c:''Oracle''product''10.2.0''client_1'etwork''ADMIN的标准TNSNames.ora文件。对于网络上的每台 PC 在工作中,此位置都是相同的。但是,TNSNames.ora 文件并不总是像我一样完整,我可能比其他文件有更多的可用连接。这就是为什么,正如您很快就会在我的代码中看到的那样,我基本上说:"如果在文档 X 的位置 Y 中找到 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
                    {
                    }
                }
            }
        }
    }
}

如何使我的 c# 表单与其他人的 oracle 一起使用

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

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

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

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

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

以下是您可能需要添加到web/app.config文件中才能使其正常工作的部分。我通常用.managed连接名称后缀以确保没有命名冲突(理论上,客户端应该在tnsnames.ora之前查看web/app.config,但我不信任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(或等效版本,具体取决于版本),而不是将字符串硬编码到代码中(因为这通常是一种不好的做法)。