本文介绍了SQLite的DLL适用于x86 / x64体系结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在VB.net程序,并使用预编译的二进制为.NET,但它是不工作的x64体系结构,而我得到了经典的文化问题,而不是加载正确的文件。

  System.BadImageFormatException:
无法加载文件或程序集System.Data.SQLite,版本= 1.0.65.0,文化=中立,
公钥= db937bc2d44ff139'或者一个它的依赖性。试图加载程序格式不正确。
档名:'System.Data.SQLite,
版本= 1.0.65.0,
区域性=中性,
公钥= db937bc2d44ff139'

有只使用一个DLL,可能的方式:




  1. 添加一些指令例如 #IFDEF (86包括代码的某些部分),否则64码

  2. 加入的dll做唯一的一个。

  3. 参考这个DLL在VB.net



你觉得是其他更好的想法,因为我想。进行一次完整的编译,而不是一个为X32和其他用于基于x64



有关实例(32位):



私人共享子OpenConection(康涅狄格州的ByRef作为SQLite.SQLiteConnection)
康涅狄格州=新SQLite.SQLiteConnection(数据源=&放大器;统环境.CurrentDirectory&安培;\database.db)
Conn.Open()
端子

私人共享子CloseConection(康涅狄格州的ByRef作为SQLite.SQLiteConnection)
Conn.Close()
Conn.Dispose()
康涅狄格州=什么
端子

公共共享功能ReturnSelect(BYVAL DataTAbleName作为字符串,BYVAL sQuery作为字符串,BYVAL sWhere作为字符串)作为Data.DataTable
昏暗LDT作为新的DataTable
昏暗LTA作为新SQLite.SQLiteDataAdapter
如果DataTAbleName是Nothing,然后返回新的DataTable(DataTAbleName)
尝试
OpenConection(conexion)
LTA =新SQLite.SQLiteDataAdapter(选择&放大器; sQuery&安培; FROM&放大器; DataTAbleName&安培; IIF(sWhere<>的String.Empty,哪里,)及sWhere,conexion)
lTA.Fill(LDT)
抓住EX作为例外
掷前
最后
CloseConection(conexion)
lTA.Dispose()
LTA =什么
端尝试
返回LDT
端功能

如何更改在64位架构的工作?
也许包括32位和64的DLL和功能做这样的事情。



尝试
实例=我
'检查,如果Homidom运行在32位或64位
。如果IntPtr.Size = 8然后_OsPlatForm =64否则_OsPlatForm =32
'继续代码

抓住EX作为例外
'ex.Message
端尝试


解决方案

有对使用SQLite从.NET程序集的各种选项。第一步是移动到的东西比古代的1.0.65版本更新。 或通过。这是SQLite的引擎来管理C#的端口,所以整个事情运行作为值为anycpu和的位数是不是一个问题。


I am developing a program in VB.net, and using System.Data.SQLite Precompiled Binaries for .NET, However It is not working for x64 Architectures, and I am getting the classic culture problem and not loading correct file.

System.BadImageFormatException:
Could not load file or assembly 'System.Data.SQLite, Version=1.0.65.0, Culture=neutral,
 PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'System.Data.SQLite,
 Version=1.0.65.0,
 Culture=neutral,
 PublicKeyToken=db937bc2d44ff139'

Is there a way to use only one dll, maybe:

  1. Add some directives like #IFDEF (x86 include some part of code) or else x64 code
  2. Join dlls to make only one.
  3. Reference this dll in VB.net

Do you think is other better Idea, as I would like to make only one compilation, not one for x32 and other for x64.

For instance (32 bits):

Private Shared Sub OpenConection(ByRef Conn As SQLite.SQLiteConnection)
    Conn = New SQLite.SQLiteConnection("Data Source=" & System.Environment.CurrentDirectory & "\database.db")
    Conn.Open()
End Sub

Private Shared Sub CloseConection(ByRef Conn As SQLite.SQLiteConnection)
    Conn.Close()
    Conn.Dispose()
    Conn = Nothing
End Sub

Public Shared Function ReturnSelect(ByVal DataTAbleName As String, ByVal sQuery As String, ByVal sWhere As String) As Data.DataTable
    Dim lDT As New DataTable
    Dim lTA As New SQLite.SQLiteDataAdapter
    If DataTAbleName Is Nothing Then Return New DataTable(DataTAbleName)
    Try
        OpenConection(conexion)
        lTA = New SQLite.SQLiteDataAdapter("SELECT " & sQuery & " FROM  " & DataTAbleName & IIf(sWhere <> String.Empty, " WHERE ", "") & sWhere, conexion)
        lTA.Fill(lDT)
    Catch ex As Exception
        Throw ex
    Finally
        CloseConection(conexion)
        lTA.Dispose()
        lTA = Nothing
    End Try
    Return lDT
End Function

How to change that to work on 64 bit architecture?Maybe including both 32 and 64 dll's and in functions do something like

Try
    Instance = Me
    'Check If Homidom Run in 32 or 64 bits
    If IntPtr.Size = 8 Then _OsPlatForm = "64" Else _OsPlatForm = "32"
    'continue code

Catch ex As Exception
    ' ex.Message
End Try
解决方案

There are various options for using SQLite from a .NET assembly. Your first step is to move to something newer than the ancient 1.0.65 version. Current versions can be downloaded from here: http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki or via the SQLite NuGet packages.

If you need to be able to run under both 32-bit and 64-bit, one option is to use the native library pre-loading option, where you distribute the native binaries in separate directories, so that it looks like this:

  • \App.exe (optional, managed-only application executable assembly)
  • \App.dll (optional, managed-only application library assembly)
  • \System.Data.SQLite.dll (required, managed-only core assembly)
  • \System.Data.SQLite.Linq.dll (optional, managed-only LINQ assembly)
  • \x86\SQLite.Interop.dll (required, x86 native interop assembly)
  • \x64\SQLite.Interop.dll (required, x64 native interop assembly)

Another option is to build two versions of your app, and in each version you reference the appropriate mixed-mode assembly. You'd then end up with two versions, but they're a bit simpler to deal with since you don't need the extra subdirectory and native *.Interop.dlls.

In these cases you'd need no code differences or optional compilation between 32-bit and 64-bit. Installing from the different NuGet packages will probably get you started most easily.

A final option is to go for the managed-only clone called C#-SQLite: https://code.google.com/p/csharp-sqlite/. It's a port of the SQLite engine to managed C#, so the whole thing runs as AnyCPU and the bitness is not an issue.

这篇关于SQLite的DLL适用于x86 / x64体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:52