C#:卸载时的自定义操作

时间:2010-06-11 13:09:49

标签: c# odbc uninstall

我正在尝试在卸载期间删除ODBC条目。做这个的最好方式是什么?我有一个标准的VS安装项目。

1 个答案:

答案 0 :(得分:2)

再一次

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