ADO.net连接oracle问题
(1)环境
windows7,64位,oracle11g,vs2010
(2)代码
OracleConnection conn = new OracleConnection("Data Source=orcl;User Id=scott;Password=tiger;Integrated Security=no;");
conn.Open();
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT TNAME FROM TAB";
Label1.Text = ((OracleString)cmd.ExecuteOracleScalar()).Value;
cmd.Dispose();
conn.Dispose();
(3)现象
编译通过,如果目标平台选择:X86,则:
“/”应用程序中的服务器错误。
--------------------------------------------
未能加载文件或程序集“WebApplication2”或它的某一个依赖项。试图加载格式不正确的程序。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.BadImageFormatException: 未能加载文件或程序集“WebApplication2”或它的某一个依赖项。试图加载格式不正确的程序。
源错误:
执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
程序集加载跟踪: 下列信息有助于确定程序集“WebApplication2”未能加载的原因。
警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。
堆栈跟踪:
[BadImageFormatException: 未能加载文件或程序集“WebApplication2”或它的某一个依赖项。试图加载格式不正确的程序。]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +39
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) +132
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +144
System.Reflection.Assembly.Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46
[ConfigurationErrorsException: 未能加载文件或程序集“WebApplication2”或它的某一个依赖项。试图加载格式不正确的程序。]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +618
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +209
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +94
System.Web.Compilation.BuildManager.CallPreStartInitMethods() +332
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +677
[HttpException (0x80004005): 未能加载文件或程序集“WebApplication2”或它的某一个依赖项。试图加载格式不正确的程序。]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9091196
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +258[/b][b][/b][/b]
如果目标平台选择其它,则全部出现以下现象:
试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
源错误:
(4)补充
此段代码在window xp,vs2008,oracle10环境下运行正常。
请帮助。谢谢
--------------------------------------------
版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.272
[解决办法]
vs2010里面,代码始终是以32位运行的,即使你的机器是64位,你必须安装32位的ODP.NET才能在VS里面运行。
当然,发布到iis不受这个限制
下载地址
http://www.oracle.com/technetwork/topics/dotnet/index-085163.html
[解决办法]
参见
http://stackoverflow.com/questions/4228739/vs2010-oracle-driver-ora-12154-tsncould-not-resolve-the-connect-identifier
http://www.oracle.com/technetwork/articles/dotnet/vs2010-oracle-dev-410461.html
http://www.blogjava.net/wahahacj/archive/2007/11/19/161689.html
另外,连接方法有很多种方法
http://connectionstrings.com/oracle
[解决办法]
希望你再好好看看
http://connectionstrings.com/oracle
这个,连接方法有很多,不同的配置可以有不同的连接方法,你可以再琢磨下,
现在手头没有现成的oracle版本帮你测试
[解决办法]
另外,可以参考
http://stackoverflow.com/questions/3477445/oracle-0x80004005ora-12154-tnscould-not-resolve-the-connect-identifier
里面有
After including the USERID\USERS in my Group or user names for the permission in the TNSNAMES.ORA file it worked
[解决办法]
oracle的问题一般都不好解决,不是通过回帖就能搞定的。