private bool RegODBC(string DsnName,string ServerName) { try { //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI中创建一个子键和相应的值 Microsoft.Win32.RegistryKey regkey=Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software").OpenSubKey("ODBC").OpenSubKey("ODBC.INI",true).CreateSubKey(DsnName.Trim()); regkey.SetValue("DataBase",DsnName.Trim()); string strSystem32=Application.LocalUserAppDataPath.Substring(0,2); strSystem32=strSystem32+@"/WINDOWS/System32/SQLSRV32.dll"; regkey.SetValue("Driver",strSystem32); regkey.SetValue("Server",ServerName.Trim()); regkey.SetValue("Trusted_Connection","Yes"); //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI/ODBC Data Sources中增加一个字符串键值 regkey=Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software").OpenSubKey("ODBC").OpenSubKey("ODBC.INI",true).OpenSubKey("ODBC Data Sources",true); regkey.SetValue(DsnName.Trim(),"SQL Server"); return true; } catch(Exception Err) { } return false; }
新建