你觀察就可以發現用windows控制面板的工具建立ODBC數據源就是在注冊表中建立相應的值。 下面的函數可以建立SQL Server的ODBC數據源!可以自己觀察注冊表,修改一部分值! /// <summary> /// 注冊odbc數據源 /// </summary> /// <param name="DsnName">ODBC數據源名稱,這里要與SQL Server數據庫名保持一致</param> /// <param name="ServerName">SQL Server數據庫服務器名</param> /// <returns>返回是否成功</returns> private bool RegODBC(string DsnName,string ServerName) { try { //在HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI中創建一個子鍵和相應的值 Microsoft.Win32.RegistryKeyregkey=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; }
|