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



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

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

在浏览了网上提供的几个不那么复杂的例子后,我终于想出了这个(对我来说效果很好)。

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 + "";
            strAttributes += "SYSTEM=" + _server + "";
            strAttributes += "UID=" + _user + "";
            strAttributes += "PWD=" + _pass + "";
            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("nnODBC Connection " + dsnName + " already exists on the system");
    }
    else
    {
        Console.WriteLine("nnODBC Connection " + dsnName + " does not exist on the system");
        Console.WriteLine("nnPress 'Y' to create the connection?");
        string cont = Console.ReadLine();
        if (cont == "Y" || cont == "y")
        {
            odbc.CreateDSN(dsnName);
            Environment.Exit(1);
        }
        else
        {
            Environment.Exit(1);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新