日期:2014-05-17  浏览次数:21214 次

C#采用无客户端访问Oracle,出现System.AccessViolationException: 尝试读取或写入受保护的内存
客户要求我们的应用不安装客户端访问Oracle数据库,我是把oci.dll、ociw32.dll、Oracle.DataAccess.dll、orannzsbb10.dll、oraocci10.dll、oraociei10.dll、OraOps10.dll
放入安装文件夹,然后用System.Data.OracleClient访问Oracle
但问题来了,如果电脑上没有安装Oracle客户端或服务器,没问题能正常访问;如果电脑上装装Oracle客户端或服务器,偶尔会出现下列异常:
************** 异常文本 **************
System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
   在 System.Data.Common.UnsafeNativeMethods.OCIEnvCreate(IntPtr& envhpp, MODE mode, IntPtr ctxp, IntPtr malocfp, IntPtr ralocfp, IntPtr mfreefp, UInt32 xtramemsz, IntPtr usrmempp)
   在 System.Data.OracleClient.TracedNativeMethods.OCIEnvCreate(IntPtr& envhpp, MODE mode)
   在 System.Data.OracleClient.OciHandle..ctor(OciHandle parentHandle, HTYPE handleType, MODE ocimode, HANDLEFLAG handleflags)
   在 System.Data.OracleClient.OracleInternalConnection.OpenOnLocalTransaction(String userName, String password, String serverName, Boolean integratedSecurity, Boolean unicode, Boolean omitOracleConnectionName)
   在 System.Data.OracleClient.OracleInternalConnection..ctor(OracleConnectionString connectionOptions)
   在 System.Data.OracleClient.OracleConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   在 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   在 System.Data.OracleClient.OracleConnection.Open()
   在 DCM.util.Functions.CreateDbConnection()
   在 DCM.form.FormLogin.ExecuteLogin(Object sender, EventArgs e)
   在 DCM.util.Functions.ButtonHandlerNoRepeat(Object sender, EventArgs e, EventHandler Handler, Boolean ret)
   在 DCM.form.FormLogin.btnLogin_Click(Object sender, EventArgs e)
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   在 System.Windows.Forms.Button.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)