检查系统DSN并创建系统DSN不存在则(iSeries的访问ODBC驱动程序)系统、不存在、驱动程序、DSN

2023-09-03 22:16:53 作者:无色无味德生活

有人可以帮助我? 我需要检查通过系统DSN我的ODBC连接到AS400施维雅并创建一个系统DSN如果一个特定的不存在。 我试过在Google上搜寻,并一直没能找到任何为我好。

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.

顺便说一句,我是很新的编程。任何帮助将非常AP preciated。 谢谢

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

推荐答案

在经历可在网上少一些复杂的例子,这是我设法想出(和它的作品对我罚款)。

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);
        }
    }
}