Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:30:02+00:00 2026-05-30T08:30:02+00:00

can someone please help me with this? i need to check through the System

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:30:04+00:00Added an answer on May 30, 2026 at 8:30 am

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone please help me. why does this return an error: Dim stuff As
Can someone please help me out with a JavaScript/jQuery solution for this arithmetic problem:
Could someone please help explain why I can't get this to work? I properly
Can someone please help me with this preg_match if (preg_match('~[^A-Za-z0-9_\./\]~', $filepath)) // Show Error
Can someone please help me out with printing the contents of an IFrame via
Can someone please help me? In Perl, what is the difference between: exec command;
Can someone please help quickly mute or unmute the stage volume in Flash CS3
Can someone please help me with using Regex with NSPredicate? NSString *regex = @(?:[A-Za-z0-9]);
Can someone please help me figure out a way to achieve the following (see
Please can someone help? I have the following code which uploads a file to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.