C#OleDConnection连接到以前的IP

本文关键字:IP 连接 C#OleDConnection | 更新日期: 2023-09-27 18:24:05

我有多个数据库(30+)在诊所使用,并通过他们使用的许可证软件自动设置。因此,每个数据库的名称相同,使用相同的端口,唯一更改的是IP。话虽如此,我使用以下代码尝试对它们单独运行查询。但是,当我更改IP并再次运行脚本时,它会返回上一台服务器的结果。

using System;
using System.Diagnostics;
using System.Data.OleDb;
namespace ConnectionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (OleDbConnection conn = new OleDbConnection("Provider=SAOLEDB.10;LINKS=tcpip(host=X.X.X.X,PORT=2638);ServerName=EAGLESOFT;Integrated Security = True; User ID = dba; PWD = sql"))
            {
                try
                {
                    conn.Open();
                    using (OleDbCommand cmd = new OleDbCommand("SELECT tran_num, provider_id, tran_date FROM transactions WHERE tran_date LIKE '2015-11-23%'", conn))
                    {
                        using (OleDbDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Console.WriteLine("{0}|{1}|{2}", reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString());
                            }
                        }
                    }
                }
                catch (Exception connerr) { Debug.WriteLine(connerr.Message); }
                conn.Close();
            }
            if (Debugger.IsAttached)
            {
                Console.ReadLine();
            }
        }
    }
}

C#OleDConnection连接到以前的IP

我看不出你为什么会在这里得到以前的结果。您是否同时运行此代码的多个实例?您是否每次都手动更改代码中的IP地址?我假设"Transactions"对象是一个实际的表,而不是动态生成的东西?

只需一点点修改,就可以将IP地址作为命令行参数传递:

static void Main(string[] args)
{
    string ip, port = null;
    for (int i = 0; i < args.Length; i++)
    {
        if (args[i].StartsWith("/i:"))
            ip = args[i].Substring(args[i].IndexOf(':') + 1);
        else if (args[i].StartsWith("/p:"))
            port = args[i].Substring(args[i].IndexOf(':') + 1);
    }
    // Default the port value to 2638 (since I have no idea if that changes).
    if (string.IsNullOrEmpty(port))
        port = "2638";
    string connStr = string.Format("Provider=SAOLEDB.10;LINKS=tcpip(host={0},PORT={1});ServerName=EAGLESOFT;Integrated Security = True; User ID = dba; PWD = sql", ip, port);
    using (OleDbConnection conn = new OleDbConnection(connStr))
    {
        try
        {
            conn.Open();
            if (conn.State != System.Data.ConnectionState.Open)
                // You could also implement a WHILE loop with a small delay (~1200ms) and try again to open the connection, with a counter to "fail" after a certain number (like 3) of attempts.
                throw new Exception("Unable to open connection to database.");
            using (OleDbCommand cmd = new OleDbCommand("SELECT tran_num, provider_id, tran_date FROM transactions WHERE tran_date LIKE '2015-11-23%'", conn))
            using (OleDbDataReader reader = cmd.ExecuteReader())
                while (reader.Read())
                    Console.WriteLine("{0}|{1}|{2}", reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString());
        }
        catch (Exception connerr)
        { Debug.WriteLine(connerr.Message); }
        finally
        { conn.Close(); }
    }
    if (Debugger.IsAttached)
    {
        Console.ReadLine();
    }
}

首先(出于测试目的),我可能会尝试删除ServerName参数,因为您已经明确提供了IP地址。

所以你的连接字符串应该是:

Provider=SAOLEDB.10;LINKS=tcpip(host=X.X.X.X,PORT=2638);Integrated Security = True; User ID = dba; PWD = sql

与ORA文件、DSN等类似,听起来您为EAGLESOFT配置了一个服务器别名,该别名可能会覆盖测试中的IP首选项。

确保更改EagleSoft和IP。您必须将IP地址作为arg[]进行传递。如下修改代码以允许IP更改

           string host = "X.X.X.X";
            string conStr = string.Format("Provider=SAOLEDB.10;LINKS=tcpip(host={0},PORT=2638);ServerName=EAGLESOFT;Integrated Security = True; User ID = dba; PWD = sql", host);
            using (OleDbConnection conn = new OleDbConnection(conStr))
 ​