1. 先去oracle 下載一個叫 ODAC1110720Xcopy.rar 的檔案,解壓, 裡邊有一個instantclient_11_1 檔案夾, 11-1 是oracle 的版本號碼, 2. 將 instantclient_11_1 檔案夾放在 c:/windows/ 下 , 並修改系統內容變數 path=c:/windows/instantclient_11_1; 3. 修改oracle 的資料連線字串 <add key="Oracle_ConnectionString" value="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.17.102)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=servicename)));User ID=username;Password=password;"/>4. 使用方法如下: using (OracleConnection conn = new OracleConnection()) { try { conn.ConnectionString = Oracle_ConnectionString; conn.Open(); return DbHelperOra.ExecuteScalar(conn, strSql.ToString(), null); } catch (Exception ex) { ExpLog.Write(ex, ""); throw ex; } finally { if (conn != null) conn.Close(); } }5. 也可以程式自動改 path 如下:Application_StartupPath 是系統運行目錄 Application.StartupPath 就可以取到 /// <summary> /// 修改使用者環境變數 /// 寫註冊表,配置 windows 作業系統 .net 訪問 oracle 資料庫 /// 對目前使用者有效環境變數在 HKEY_CURRENT_USER Environment 中設定 /// </summary> /// <param name="Application"></param> public void Write_HKEY_CURRENT_USER_Environment(string Application_StartupPath) { try { string SIMPLIFIED = "SIMPLIFIED CHINESE_CHINA.ZHS16GBK"; // "SIMPLIFIED CHINESE_CHINA.AL32UTF8"; string App_Path = Application_StartupPath + "//instantclient;"; RegistryKey regLocalMachine = Registry.CurrentUser; RegistryKey regEnvironment = regLocalMachine.OpenSubKey("Environment", true); //開啟HKEY_CURRENT_USER下的 HKEY_CURRENT_USER/Environment object NLS_LANG = regEnvironment.GetValue("NLS_LANG"); // 編碼方式 //object ORACLE_HOME = regEnvironment.GetValue("ORACLE_HOME"); // ORACLE_HOME string Path = regEnvironment.GetValue("Path") == null ? "" : regEnvironment.GetValue("Path").ToString(); // Path string newPaht = Path == null ? "" : Path.ToString(); if (Path.Trim() == "") { newPaht = App_Path; } else { // 路經中是否包含當前路徑 , 如果不包含,將當前程式路徑加到最前面,包含時不處理 if (Path.ToString().Trim().IndexOf(App_Path) <= -1) { newPaht = App_Path + Path.ToString().Trim(); } else { int index = Path.IndexOf(";"); if (index > 0) { string firstStr = Path.Substring(0, index+1); // 是否在最前面 if (firstStr != App_Path) { Path = Path.Replace(App_Path, ""); newPaht = App_Path + Path.ToString().Trim(); } else { return; } } } } if ((string)NLS_LANG != SIMPLIFIED) regEnvironment.SetValue("NLS_LANG", SIMPLIFIED); // 不寫 ORACLE_HOME 註冊表,程式也能運行 //if ((string)ORACLE_HOME != App_Path) // regEnvironment.SetValue("ORACLE_HOME", App_Path); if ((string)Path != newPaht) regEnvironment.SetValue("Path", newPaht); //下面利用發送系統訊息,就不要重新啟動電腦了 const int windowHandle = 0xffff; // DWORD dwMsgResult = 0L; const UInt32 msg = 0; const int wParam = 0xffff; const string IParam = "Environment"; //const long SMTO_ABORTIFHUNG = 0x2; //System.UInt32 dwMsgResult1 = 0; int result; SendMessageTimeout(windowHandle, msg, wParam, IParam, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 5000, out result); //uint lMsg; //Register the message //lMsg = Win32.RegisterWindowMessage("WM_HTML_GETOBJECT"); //Get the object //SendMessageTimeout(windowHandle, lMsg, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out result); //if (lRes != IntPtr.Zero) //{ //Get the object from lRes // htmlDoc = (mshtml.IHTMLDocument)Win32.ObjectFromLresult(lRes, IID_IHTMLDocument, IntPtr.Zero); // return htmlDoc; //} //IntPtr windowHandle = new IntPtr(0); //uint msg = 0; //IntPtr wParam = new IntPtr(0); //IntPtr IParam = new IntPtr(0); //const uint timeout = 5000; //SendMessageTimeout(windowHandle, msg, wParam, IParam, SendMessageTimeoutFlags.SMTO_BLOCK, timeout, out result); } catch (Exception ex) { D_LogBLL.Instance.Add("", " 修改使用者環境變數 Write_HKEY_CURRENT_USER_Environment() Application_StartupPath=" + Application_StartupPath, ex); throw ex; } }