2010-06-11 79 views
3

我試圖在卸載期間刪除ODBC條目。做這個的最好方式是什麼?我有一個標準的VS安裝項目。C#:卸載時的自定義操作

+0

看看http://stackoverflow.com/questions/334939/how-do-i-create-an-odbc-dsn-entry-using-c – volody 2010-06-11 13:14:20

回答

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

這是哪裏安裝程序類?我在安裝項目中看不到任何* .cs文件。 – sbenderli 2010-06-11 13:41:32

+0

檢查http://devcity.net/Articles/339/1/article.aspx – volody 2010-06-11 13:58:12

+0

這似乎將工作,謝謝! – sbenderli 2010-06-11 15:31:55