我已经在 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
{
}
}
}
}
}
}