Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I actually solved this myself in the end by manipulating the registry. I've created a class to contain the functionality, the contents of which I've included here:</p> <pre><code>///&lt;summary&gt; /// Class to assist with creation and removal of ODBC DSN entries ///&lt;/summary&gt; public static class ODBCManager { private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\"; private const string ODBCINST_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBCINST.INI\\"; /// &lt;summary&gt; /// Creates a new DSN entry with the specified values. If the DSN exists, the values are updated. /// &lt;/summary&gt; /// &lt;param name="dsnName"&gt;Name of the DSN for use by client applications&lt;/param&gt; /// &lt;param name="description"&gt;Description of the DSN that appears in the ODBC control panel applet&lt;/param&gt; /// &lt;param name="server"&gt;Network name or IP address of database server&lt;/param&gt; /// &lt;param name="driverName"&gt;Name of the driver to use&lt;/param&gt; /// &lt;param name="trustedConnection"&gt;True to use NT authentication, false to require applications to supply username/password in the connection string&lt;/param&gt; /// &lt;param name="database"&gt;Name of the datbase to connect to&lt;/param&gt; public static void CreateDSN(string dsnName, string description, string server, string driverName, bool trustedConnection, string database) { // Lookup driver path from driver name var driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + driverName); if (driverKey == null) throw new Exception(string.Format("ODBC Registry key for driver '{0}' does not exist", driverName)); string driverPath = driverKey.GetValue("Driver").ToString(); // Add value to odbc data sources var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources"); if (datasourcesKey == null) throw new Exception("ODBC Registry key for datasources does not exist"); datasourcesKey.SetValue(dsnName, driverName); // Create new key in odbc.ini with dsn name and add values var dsnKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + dsnName); if (dsnKey == null) throw new Exception("ODBC Registry key for DSN was not created"); dsnKey.SetValue("Database", database); dsnKey.SetValue("Description", description); dsnKey.SetValue("Driver", driverPath); dsnKey.SetValue("LastUser", Environment.UserName); dsnKey.SetValue("Server", server); dsnKey.SetValue("Database", database); dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No"); } /// &lt;summary&gt; /// Removes a DSN entry /// &lt;/summary&gt; /// &lt;param name="dsnName"&gt;Name of the DSN to remove.&lt;/param&gt; public static void RemoveDSN(string dsnName) { // Remove DSN key Registry.LocalMachine.DeleteSubKeyTree(ODBC_INI_REG_PATH + dsnName); // Remove DSN name from values list in ODBC Data Sources key var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources"); if (datasourcesKey == null) throw new Exception("ODBC Registry key for datasources does not exist"); datasourcesKey.DeleteValue(dsnName); } ///&lt;summary&gt; /// Checks the registry to see if a DSN exists with the specified name ///&lt;/summary&gt; ///&lt;param name="dsnName"&gt;&lt;/param&gt; ///&lt;returns&gt;&lt;/returns&gt; public static bool DSNExists(string dsnName) { var driversKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + "ODBC Drivers"); if (driversKey == null) throw new Exception("ODBC Registry key for drivers does not exist"); return driversKey.GetValue(dsnName) != null; } ///&lt;summary&gt; /// Returns an array of driver names installed on the system ///&lt;/summary&gt; ///&lt;returns&gt;&lt;/returns&gt; public static string[] GetInstalledDrivers() { var driversKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + "ODBC Drivers"); if (driversKey == null) throw new Exception("ODBC Registry key for drivers does not exist"); var driverNames = driversKey.GetValueNames(); var ret = new List&lt;string&gt;(); foreach (var driverName in driverNames) { if (driverName != "(Default)") { ret.Add(driverName); } } return ret.ToArray(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload