您的位置:首页 > 数据库 > Oracle

.net不用客户端连接Oracle数据库服务器的例子

2011-03-29 14:06 375 查看
 

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;
            }
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: