3

I am trying to remove an ODBC entry during uninstall. What is the best way to do this? I have a standard VS Setup project.

sbenderli
  • 3,654
  • 9
  • 35
  • 48
  • take a look http://stackoverflow.com/questions/334939/how-do-i-create-an-odbc-dsn-entry-using-c – volody Jun 11 '10 at 13:14

1 Answers1

2

One more

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Runtime.InteropServices;

namespace MyInstallerClassDll
{
[RunInstaller(true)]
public partial class MyInstallerClass : Installer
{
    const int ODBC_REMOVE_DSN = 3;
    public MyInstallerClass()
    {
        InitializeComponent();
    }

    public override void Uninstall(System.Collections.IDictionary savedState)
    {
        RemoveSystemDSN();
        base.Uninstall(savedState);
    }

    [DllImport("ODBCCP32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int SQLConfigDataSource(int hwndParent, int fRequest, string lpszDriver, string lpszAttributes);
    private void RemoveSystemDSN()
    {
        string vAttributes = "DSN=DSN Name" + Strings.Chr(0);
        vAttributes += "Description=DSN Description" + Strings.Chr(0);
        vAttributes += "Trusted_Connection=Yes" + Strings.Chr(0);
        vAttributes += "Server=SQLINSTANCE" + Strings.Chr(0);
        vAttributes += "Database=databasename" + Strings.Chr(0);

        if (SQLConfigDataSource(0, ODBC_REMOVE_DSN, "SQL Server", vAttributes) == 0)
        {
            MessageBox.Show("Failed to remove ODBC data source!!");
        }
    }
}
} 
volody
  • 6,946
  • 3
  • 42
  • 54