检查系统 DSN,如果不存在则创建系统 DSN(iSeries Access ODBC 驱动程序)

发布于 2025-01-05 03:07:30 字数 137 浏览 3 评论 0原文

有人可以帮我吗? 我需要通过系统 DSN 检查与 AS400 服务器的 ODBC 连接,如果特定的 DSN 不存在,则创建一个系统 DSN。 我尝试过谷歌搜索,但找不到任何对我有用的东西。

顺便说一句,我对编程很陌生。任何帮助将不胜感激。 谢谢

can someone please help me with this?
i need to check through the System DSN for my ODBC connection to the AS400 servier and create a System DSN if a particular one does not exist.
i've tried googling and have not been able to find anything good for me.

btw, i am quite new to programming. any help will be much appreciated.
thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2025-01-12 03:07:30

在浏览了网上提供的几个不太复杂的示例之后,这就是我设法想出的(并且它对我来说效果很好)。

using System;
using System.Runtime.InteropServices;

public class ODBC_Manager
{
    [DllImport("ODBCCP32.dll")]
    public static extern bool SQLConfigDataSource(IntPtr parent, int request, string driver, string attributes);

    [DllImport("ODBCCP32.dll")]
    public static extern int SQLGetPrivateProfileString(string lpszSection, string lpszEntry, string lpszDefault, string @RetBuffer, int cbRetBuffer, string lpszFilename);

    private const short ODBC_ADD_DSN = 1;
    private const short ODBC_CONFIG_DSN = 2;
    private const short ODBC_REMOVE_DSN = 3;
    private const short ODBC_ADD_SYS_DSN = 4;
    private const short ODBC_CONFIG_SYS_DSN = 5;
    private const short ODBC_REMOVE_SYS_DSN = 6;
    private const int vbAPINull = 0;

    public void CreateDSN(string strDSNName)
    {
        string strDriver;
        string strAttributes;

        try
        {
            string strDSN = "";

            string _server = //ip address of the server
            string _user = //username
            string _pass = //password
            string _description = //not required. give a description if you want to


            strDriver = "iSeries Access ODBC Driver";

            strAttributes = "DSN=" + strDSNName + "\0";
            strAttributes += "SYSTEM=" + _server + "\0";
            strAttributes += "UID=" + _user + "\0";
            strAttributes += "PWD=" + _pass + "\0";

            strDSN = strDSN + "System = " + _server + "\n";
            strDSN = strDSN + "Description = " + _description + "\n";

            if (SQLConfigDataSource((IntPtr)vbAPINull, ODBC_ADD_SYS_DSN, strDriver, strAttributes))
            {
                Console.WriteLine("DSN was created successfully");
            }
            else
            {
                Console.WriteLine("DSN creation failed...");
            }
        }
        catch (Exception ex)
        {
            if (ex.InnerException != null)
            {
                Console.WriteLine(ex.InnerException.ToString());
            }
            else
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }

    public int CheckForDSN(string strDSNName)
    {
        int iData;
        string strRetBuff = "";
        iData = SQLGetPrivateProfileString("ODBC Data Sources", strDSNName, "", strRetBuff, 200, "odbc.ini");
        return iData;
    }
}

...然后从您的应用程序调用方法。

static void Main(string[] args)
{
    ODBC_Manager odbc = new ODBC_Manager();
    string dsnName = //Name of the DSN connection here

    if (odbc.CheckForDSN(dsnName) > 0)
    {
        Console.WriteLine("\n\nODBC Connection " + dsnName + " already exists on the system");
    }
    else
    {
        Console.WriteLine("\n\nODBC Connection " + dsnName + " does not exist on the system");
        Console.WriteLine("\n\nPress 'Y' to create the connection?");

        string cont = Console.ReadLine();
        if (cont == "Y" || cont == "y")
        {
            odbc.CreateDSN(dsnName);
            Environment.Exit(1);
        }
        else
        {
            Environment.Exit(1);
        }
    }
}

After going through the few less complicated examples available online, this is what i managed to come up with (and it works fine for me).

using System;
using System.Runtime.InteropServices;

public class ODBC_Manager
{
    [DllImport("ODBCCP32.dll")]
    public static extern bool SQLConfigDataSource(IntPtr parent, int request, string driver, string attributes);

    [DllImport("ODBCCP32.dll")]
    public static extern int SQLGetPrivateProfileString(string lpszSection, string lpszEntry, string lpszDefault, string @RetBuffer, int cbRetBuffer, string lpszFilename);

    private const short ODBC_ADD_DSN = 1;
    private const short ODBC_CONFIG_DSN = 2;
    private const short ODBC_REMOVE_DSN = 3;
    private const short ODBC_ADD_SYS_DSN = 4;
    private const short ODBC_CONFIG_SYS_DSN = 5;
    private const short ODBC_REMOVE_SYS_DSN = 6;
    private const int vbAPINull = 0;

    public void CreateDSN(string strDSNName)
    {
        string strDriver;
        string strAttributes;

        try
        {
            string strDSN = "";

            string _server = //ip address of the server
            string _user = //username
            string _pass = //password
            string _description = //not required. give a description if you want to


            strDriver = "iSeries Access ODBC Driver";

            strAttributes = "DSN=" + strDSNName + "\0";
            strAttributes += "SYSTEM=" + _server + "\0";
            strAttributes += "UID=" + _user + "\0";
            strAttributes += "PWD=" + _pass + "\0";

            strDSN = strDSN + "System = " + _server + "\n";
            strDSN = strDSN + "Description = " + _description + "\n";

            if (SQLConfigDataSource((IntPtr)vbAPINull, ODBC_ADD_SYS_DSN, strDriver, strAttributes))
            {
                Console.WriteLine("DSN was created successfully");
            }
            else
            {
                Console.WriteLine("DSN creation failed...");
            }
        }
        catch (Exception ex)
        {
            if (ex.InnerException != null)
            {
                Console.WriteLine(ex.InnerException.ToString());
            }
            else
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }

    public int CheckForDSN(string strDSNName)
    {
        int iData;
        string strRetBuff = "";
        iData = SQLGetPrivateProfileString("ODBC Data Sources", strDSNName, "", strRetBuff, 200, "odbc.ini");
        return iData;
    }
}

... and then call the methods from your application.

static void Main(string[] args)
{
    ODBC_Manager odbc = new ODBC_Manager();
    string dsnName = //Name of the DSN connection here

    if (odbc.CheckForDSN(dsnName) > 0)
    {
        Console.WriteLine("\n\nODBC Connection " + dsnName + " already exists on the system");
    }
    else
    {
        Console.WriteLine("\n\nODBC Connection " + dsnName + " does not exist on the system");
        Console.WriteLine("\n\nPress 'Y' to create the connection?");

        string cont = Console.ReadLine();
        if (cont == "Y" || cont == "y")
        {
            odbc.CreateDSN(dsnName);
            Environment.Exit(1);
        }
        else
        {
            Environment.Exit(1);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文