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 TrottC# ASP.Net MVC • May 24, 2010
Using C# to Add ODBC System DSN to Connect to System ODBC Datasource

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

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. 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?

New comments for this post are currently closed.