none
Change ODBC Connection Settings RRS feed

  • Question

  • A program we purchased uses a hardcoded odbc connection name. Our users (and our license allows) need to connect to multiple instance. Right now they are changing by hand the ODBC in their USER DSN. Is there anyway I can change this programmatically for them using C#. I am familar with connecting using a User DSN but I have no idea how to change one.

    Monday, March 10, 2008 10:10 PM

Answers

  • Code Snippet

    Here is some code from an old project that modifies DSN. This might be what you are looking for:

     

    public class AddLocalDSN {

    public AddLocalDSN(string intDSName, string intDBPath) {

    bool addDSNSuccess = CreateDataSource((IntPtr)0, 4,

    "Microsoft Access Driver (*.MDB)\0", "DSN="

    + intDSName + "\0Uid=Admin\0pwd=\0DBQ=" + intDBPath + "\0");

    }

     

    [DllImport( "ODBCCP32.dll")]

    private static extern bool SQLConfigDataSource(IntPtr hwndParent, int fRequest,

    string lpszDriver, string lpszAttributes);

    public static bool CreateDataSource(IntPtr hwndParent, int fRequest,

    string lpszDriver, string lpszAttributes) {

    return SQLConfigDataSource(hwndParent, fRequest, lpszDriver, lpszAttributes);

    }

    }

     

    /* Note:

    The int 4 refers to the value "ODBC_ADD_SYS_DSN". To remove a system DSN use

    the int 6. See below:

    1 - ODBC_ADD_DSN (use this to add a user DSN)

    2 - ODBC_CONFIG_DSN (use this to configure a user DSN)

    3 - ODBC_REMOVE_DSN (use this to remove a user DSN)

    4 - ODBC_ADD_SYS_DSN (use this to add a system DSN)

    5 - ODBC_CONFIG_SYS_DSN (use this to configure a system DSN)

    6 - ODBC_REMOVE_SYS_DSN (use this to remove a system DSN)

    Also, you must include the following using reference:

    using System.Runtime.InteropServices;

    */

     

     

    Tuesday, March 11, 2008 3:28 AM

All replies

  • Code Snippet

    Here is some code from an old project that modifies DSN. This might be what you are looking for:

     

    public class AddLocalDSN {

    public AddLocalDSN(string intDSName, string intDBPath) {

    bool addDSNSuccess = CreateDataSource((IntPtr)0, 4,

    "Microsoft Access Driver (*.MDB)\0", "DSN="

    + intDSName + "\0Uid=Admin\0pwd=\0DBQ=" + intDBPath + "\0");

    }

     

    [DllImport( "ODBCCP32.dll")]

    private static extern bool SQLConfigDataSource(IntPtr hwndParent, int fRequest,

    string lpszDriver, string lpszAttributes);

    public static bool CreateDataSource(IntPtr hwndParent, int fRequest,

    string lpszDriver, string lpszAttributes) {

    return SQLConfigDataSource(hwndParent, fRequest, lpszDriver, lpszAttributes);

    }

    }

     

    /* Note:

    The int 4 refers to the value "ODBC_ADD_SYS_DSN". To remove a system DSN use

    the int 6. See below:

    1 - ODBC_ADD_DSN (use this to add a user DSN)

    2 - ODBC_CONFIG_DSN (use this to configure a user DSN)

    3 - ODBC_REMOVE_DSN (use this to remove a user DSN)

    4 - ODBC_ADD_SYS_DSN (use this to add a system DSN)

    5 - ODBC_CONFIG_SYS_DSN (use this to configure a system DSN)

    6 - ODBC_REMOVE_SYS_DSN (use this to remove a system DSN)

    Also, you must include the following using reference:

    using System.Runtime.InteropServices;

    */

     

     

    Tuesday, March 11, 2008 3:28 AM
  • Thanks, I believe that will due just perfectly.

    I will report back.

    Andrew

     

    Friday, March 14, 2008 12:01 AM