Using C# to Add ODBC System DSN to Connect to System ODBC Datasource

How to add an ODBC System DSN data source on the fly with C# without having to enter Windows Control Panel to configure DSN manually.

By Tim Trott | C# ASP.Net MVC | May 24, 2010

This snippet creates a wrapper function to the Microsoft ODBC interface to add ODBC System DSN. You can extend this to include other database drivers in addition to MS Access 2000.

What is ODBC System DSN?

ODBC stands for Open Database Connectivity and is a standard interface for accessing database management systems. ODBC allows applications to access many different database implementations without writing special code for each one.

Using C# to Add ODBC System DSN

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool success = Utils.CreateDataSource(Utils.ODBC_Drivers.Access2000, "MyDataSource", "c:database.mdb");
            if (success)
            {
                Console.WriteLine("DataSource Created OK");
            }
            else
            {
                Console.WriteLine("There was a problem creating DataSource");
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        public static class Utils
        {
            [DllImport("ODBCCP32.dll")]
            private static extern bool SQLConfigDataSource(IntPtr hwndParent, int fRequest, string lpszDriver, string lpszAttributes);

            / Add odbc system DSN
            public static bool CreateDataSource(string ODBCDriver, string DataSourceName, string DatabasePath)
            {
                return SQLConfigDataSource((IntPtr)0, ODBC_Request_Modes.ODBC_ADD_DSN, ODBCDriver, "DSN=" + DataSourceName + "Uid=Adminpwd=DBQ=" + DatabasePath + "");
            }

            / Request modes
            public static class ODBC_Request_Modes
            {
                public static int ODBC_ADD_DSN = 1;
                public static int ODBC_CONFIG_DSN = 2;
                public static int ODBC_REMOVE_DSN = 3;
                public static int ODBC_ADD_SYS_DSN = 4;
                public static int ODBC_CONFIG_SYS_DSN = 5;
                public static int ODBC_REMOVE_SYS_DSN = 6;
            }

            / List of ODBC Driver types
            public static class ODBC_Drivers
            {
                public static string Access2000 = "Microsoft Access Driver (*.MDB)";
            }
        }
    }
}
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.