无法让 SQL Server Compact 3.5 / 4 与 ASP .NET MVC 2 一起使用

发布于 2024-09-09 03:55:40 字数 4448 浏览 1 评论 0原文

我使用的是 Visual Studio 2008 专业版。

我可能在这里遗漏了一些非常明显的东西,但我一直在尝试让 Sql Server Compact 4 的 CTP 在我的 asp.net mvc 应用程序中工作。我几乎找不到有关如何设置此功能或工作示例应用程序的说明。我的目标是私人安装,因此我可以将其包含在我的 Web 应用程序中,而无需在我的域托管上进行 sql 服务器设置。这实际上只是我在闲聊并试图解决这个问题。我不打算举办市场或任何与此相关的活动。

因此,我已将基本 4.0 方向安装的所有 dll (c:\Program Files\Sql Server Compact\v4.0) 复制到我的应用程序中的 lib 文件夹中。我已将复制到输出方向选项设置为“如果较新则复制”。然后,我引用 System.Data.SqlServerCE dll 并将“Copy Local”设置为 True。

我通过 Sql Studio Express 创建了一个 sdf 文件。重要的一点是,我没有看到创建此文件的 CE 4.0 版本的选项,因此它是使用 CE 3.5 创建的。我创建了几个表,向这些表添加了几行,将 *.sdf 文件复制到我的 App_Data 目录。值得一提的是,从VS 2008内部来看,这个文件从未出现在我的项目中,但它确实存在于App_Data目录的物理位置中。我不知道这是为什么。

接下来,我只是尝试通过以下方式与我的 sdf 文件建立基本连接:

SqlCeConnection conn = new SqlCeConnection("DataSource=rpg.sdf");

这会产生以下错误:

Unable to load the native components of SQL Server Compact corresponding to the ADO.NET provider of version 8402. Install the correct version of SQL Server Compact. Refer to KB article 974247 for more details.

我从这里算出来,我只是尝试让 Sql CE 3.5 工作。我将本地安装的 Sql CE 3.5 升级到 sp2。我将 dll 复制到基本位置 (c:\Program Files\Sql Server Compact\v3.5),包括从项目引用中删除和读取 System.Data.SqlServerCE dll 的版本。

这里奇怪的是,当我右键单击并查看引用的 SqlServerCE dll 的属性时,它总是显示版本为 4.0.0.1。

伙计们,我真的需要一些指导。我搜索了堆栈溢出、帮助文档、在线书籍和谷歌搜索。我真的没有找到任何东西可以从 CE 3.5 或 4.0 的最顶层开始,并准确地告诉我要添加哪些 dll、将它们放在哪里、如何引用它们、如何将 .sdf 文件添加到我的项目中,连接到它并从中查询。我确实提到过一些 IBuySpy 门户示例应用程序,该应用程序应该使用 Sql CE 3.5,但实际上无法导航到 msdn 下载迷宫来找到它。理想情况下,我想为 CE 4.0 设置私有部署。

我洗耳恭听。建议,观点,无论什么都将受到高度赞赏。谢谢你!

是的,我确实看到了知识库。它没有帮助

请在此处查看:http://support.microsoft.com/kb /974247

CORFLAG 结果

好的,尝试了一下,这些是我的结果: C:\Development\Mvc2MessingAround\Mvc2MessingAround\bin\Lib>corflags System.Data。 SQLServerCe.dll Microsoft (R) .NET Framework CorFlags 转换工具。版本3.5.21022.8 版权所有 (c) Microsoft Corporation。版权所有。

Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32
CorFlags  : 9
ILONLY    : 1
32BIT     : 0
Signed    : 1

我发誓我安装了两个版本的 Sql CE (3.5/4) 的 x86 版本。安装程序可能会感到困惑,因为我的处理器支持 64 位,但我运行的是 Windows xp sp 3 32 位。结果似乎表明它是 64 位的。是这样吗?

添加详细信息

迄今为止,已在 2 台计算机上尝试了以下配置。两者都是 Windows xp sp3 32 位,具有 64 位功能的处理器。两者的开发环境都是VS 2008 Pro。机器 2 上的结果是在全新安装 Sql CE 4 Ctp 后得出的。

CONFIGURATION #1

myapp\bin\
     System.Data.SqlServerCe.dll

myapp\bin\private
    amd64
    x86
    
myapp\bin\private\x86
    sqlceca40.dll
    sqlcecompact40.dll
    sqlceer40EN.dll
    sqlceme40.dll
    sqlceqp40.dll
    sqlcese40.dll
    
myapp\bin\private\amd64
    sqlceca40.dll
    sqlcecompact40.dll
    sqlceer40EN.dll
    sqlceme40.dll
    sqlceqp40.dll
    sqlcese40.dll

错误:

An exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.DLL but was not handled in user code

Additional information: Unable to load the native components of SQL Server Compact corresponding to the ADO.NET provider of version 8402. Install the correct version of SQL Server Compact. Refer to KB article 974247 for more details.

代码:

SqlCeConnection conn = new SqlCeConnection();

CONFIGURATION 2

与 #1 相同,但在 myapp\bin 方向使用 System.Data.SqlServerCE.Entity.dll。

在点击上面的代码之前页面出现错误。这是消息:

无法加载文件或程序集“System.Data.SqlServerCe.Entity”或其依赖项之一。该程序集是由比当前加载的运行时更新的运行时构建的,无法加载。

描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.BadImageFormatException:无法加载文件或程序集“System.Data.SqlServerCe.Entity”或其依赖项之一。该程序集是由比当前加载的运行时更新的运行时构建的,无法加载。

我检查了 VS 2008 Pro 中的项目设置,并将 .Net 3.5 框架设置为目标。

配置 3

与 #1 相同,但 System.Data.SqlServerCE.dll 是从 myapp\bin\private 文件夹引用的。

结果与配置 #1 相同(错误消息 100% 相同,并且错误发生在同一行代码上)。

正确配置

根据 Erik 的指示(如果我更仔细地遵循它们),设置应该

myapp\bin
    x86
    amd64
    System.Data.SqlServerCE.dll

直接从代码的 bin 文件夹中引用 System.Data.SqlServerCE.dll。我愚蠢地认为需要包含“私人”文件夹,但事实并非如此。除非您使用的是 .net 4.0 解决方案,否则请勿将 System.Data.SqlServerCE.Entity.dll 放入 bin 文件夹中。我认为 dll 不适用于 3.5。

有用的链接:

链接

I'm using Visual Studio 2008 Pro.

I'm probably missing something very obvious here, but I've been trying to get the CTP for Sql Server compact 4 to work in my asp.net mvc application. I can find next to no instruction on how to set this up or a working example application. My goal is a private install so I can just include it in my web app without having to do sql server setup on my domain hosting. This is really just me shooting the breeze and trying to figure this out. I don't plan to host a market or anything with this.

So, I've copied all the dll's that install in the base 4.0 direction (c:\Program Files\Sql Server compact\v4.0) to a lib folder in my application. I've set the copy to output direction option to 'Copy if Newer'. I then reference the System.Data.SqlServerCE dll and set 'Copy Local' to True.

I created an sdf file via Sql Studio Express. An important note is that I did not see an option for creating a CE 4.0 version of this file, so it was created using CE 3.5. I create a few tables, add a few rows to those tables, copy the *.sdf file to my App_Data directory. It's worth mentioning that, from inside VS 2008, this file never appears in my project, but it does exist in the physical location of the App_Data directory. I'm not sure why this is.

Next, I just try making a basic connection to my sdf file via:

SqlCeConnection conn = new SqlCeConnection("DataSource=rpg.sdf");

This yields the error below:

Unable to load the native components of SQL Server Compact corresponding to the ADO.NET provider of version 8402. Install the correct version of SQL Server Compact. Refer to KB article 974247 for more details.

I figure from here, I'd just try getting Sql CE 3.5 to work. I upgrade my local installation of Sql CE 3.5 to sp2. I copy the dlls at the base location (c:\Program Files\Sql Server compact\v3.5), including removing and readding the version of the System.Data.SqlServerCE dll from my project references.

The curious thing here is when I right click and look at the properties of the referenced SqlServerCE dll, it always says it's version 4.0.0.1.

Guys, I really could use some direction here. I have searched stack overflow, the help docs, books online, and googled. I really haven't found anything that takes this from the very top for either CE 3.5 or 4.0 and tells me exactly what dll's to add, where to put them, how to reference them, how to add the .sdf file to my project, connect to it, and query from it. I did come across a few mentions of an IBuySpy portal sample app that was supposed to use Sql CE 3.5, but can't actually navigate the msdn download maze to get to it. Ideally, I want to setup a private deploy for CE 4.0.

I'm all ears. Suggestions, points, whatever would be highly appreciated. Thank you!

YES I DID SEE THE KB. IT DIDN'T HELP

See it here: http://support.microsoft.com/kb/974247

RESULTS FROM CORFLAG

Okay, tried that and these are my results:
C:\Development\Mvc2MessingAround\Mvc2MessingAround\bin\Lib>corflags System.Data.
SqlServerCe.dll
Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.

Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32
CorFlags  : 9
ILONLY    : 1
32BIT     : 0
Signed    : 1

I would have sworn I installed the x86 version of both versions of Sql CE (3.5/4). The installer might have gotten confused somehow because my processor is 64bit capable, but i'm running Windows xp sp 3 32 bit. The results seem to indicate it's 64 bit. Is that the case?

ADDED DETAILS

To date the configurations below have been tried on 2 machines. Both are Windows xp sp3 32 bit with a 64 bit capable processor. The development environment on both is VS 2008 Pro. The results on machine 2 come after a fresh install of the Sql CE 4 Ctp.

CONFIGURATION #1

myapp\bin\
     System.Data.SqlServerCe.dll

myapp\bin\private
    amd64
    x86
    
myapp\bin\private\x86
    sqlceca40.dll
    sqlcecompact40.dll
    sqlceer40EN.dll
    sqlceme40.dll
    sqlceqp40.dll
    sqlcese40.dll
    
myapp\bin\private\amd64
    sqlceca40.dll
    sqlcecompact40.dll
    sqlceer40EN.dll
    sqlceme40.dll
    sqlceqp40.dll
    sqlcese40.dll

Error:

An exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.DLL but was not handled in user code

Additional information: Unable to load the native components of SQL Server Compact corresponding to the ADO.NET provider of version 8402. Install the correct version of SQL Server Compact. Refer to KB article 974247 for more details.

Code:

SqlCeConnection conn = new SqlCeConnection();

CONFIGURATION 2

Same as #1, but with System.Data.SqlServerCE.Entity.dll at myapp\bin direction.

The page errors before hitting the code above. This is the message:

Could not load file or assembly 'System.Data.SqlServerCe.Entity' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.BadImageFormatException: Could not load file or assembly 'System.Data.SqlServerCe.Entity' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

I've checked the project settings in VS 2008 Pro and the .Net 3.5 framework is set as the target.

CONFIGURATION 3

Same as #1, except the System.Data.SqlServerCE.dll is referenced from the myapp\bin\private folder.

Results are the same as CONFIGURATION #1 (error message is 100% same and the error occurrs on the same line of code).

CORRECT CONFIGURATION

Per Erik's instructions (had I followed them more carefully), the setup should be

myapp\bin
    x86
    amd64
    System.Data.SqlServerCE.dll

Reference the System.Data.SqlServerCE.dll directly from the bin folder for the code. My folly was thinking the Private folder needed to be included, but it doesn't. Do not put the System.Data.SqlServerCE.Entity.dll in the bin folder unless you are using a .net 4.0 solution. I don't think that dll works w/ 3.5.

Helpful link:

Link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吃不饱 2024-09-16 03:55:40

SQL CE 3.5 不能与 ASP.NET 一起使用,必须使用 4.0 CTP。

此处下载。

安装运行时。

将以下目录内容(包括 x86 和 amd64 文件夹)复制到 ASP.NET 应用程序的 bin 文件夹中:
C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private

更新:使用桌面文件夹中的 System.Data.SqlServerCe.dll 以避免中等信任问题

myapp\bin\ 
 System.Data.SqlServerCe.dll 

myapp\bin\x86 
 sqlceca40.dll 
 sqlcecompact40.dll 
 sqlceer40EN.dll 
 sqlceme40.dll 
 sqlceqp40.dll 
 sqlcese40.dll 

myapp\bin\amd64 
 sqlceca40.dll 
 sqlcecompact40.dll 
 sqlceer40EN.dll 
 sqlceme40.dll 
 sqlceqp40.dll 
 sqlcese40.dll 

添加对系统的引用。您刚刚放入 /bin 文件夹中的 Data.SqlServerCe.dll 文件。

将 SQL Compact sdf 文件放入 App_Data 文件夹中。

添加连接字符串:

<connectionStrings>
   <add name ="NorthWind"
   connectionString="data source=|DataDirectory|\Nw40.sdf" />
</connectionStrings>

连接! :-)

using System.Data.SqlServerCe;

    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlCeConnection conn = new SqlCeConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
            conn.Open();
            using (SqlCeCommand cmd = new SqlCeCommand("SELECT TOP (1) [Category Name] FROM Categories", conn))
            {
                string valueFromDb = (string)cmd.ExecuteScalar();
                Response.Write(string.Format("{0} Time {1}", valueFromDb, DateTime.Now.ToLongTimeString()));
            }
        }
    }

SQL CE 3.5 does not work with ASP.NET, you must use 4.0 CTP.

Download from here.

Install the runtime.

Copy the following directory contents (including the x86 and amd64 folders) to the bin folder of your ASP.NET app:
C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private

UPDATE: Use System.Data.SqlServerCe.dll from the Desktop folder to avoid Medium Trust issues

myapp\bin\ 
 System.Data.SqlServerCe.dll 

myapp\bin\x86 
 sqlceca40.dll 
 sqlcecompact40.dll 
 sqlceer40EN.dll 
 sqlceme40.dll 
 sqlceqp40.dll 
 sqlcese40.dll 

myapp\bin\amd64 
 sqlceca40.dll 
 sqlcecompact40.dll 
 sqlceer40EN.dll 
 sqlceme40.dll 
 sqlceqp40.dll 
 sqlcese40.dll 

Add a reference to the System.Data.SqlServerCe.dll file you just put in your /bin folder.

Place the SQL Compact sdf file in your App_Data folder.

Add connection string:

<connectionStrings>
   <add name ="NorthWind"
   connectionString="data source=|DataDirectory|\Nw40.sdf" />
</connectionStrings>

Connect! :-)

using System.Data.SqlServerCe;

    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlCeConnection conn = new SqlCeConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
            conn.Open();
            using (SqlCeCommand cmd = new SqlCeCommand("SELECT TOP (1) [Category Name] FROM Categories", conn))
            {
                string valueFromDb = (string)cmd.ExecuteScalar();
                Response.Write(string.Format("{0} Time {1}", valueFromDb, DateTime.Now.ToLongTimeString()));
            }
        }
    }
半边脸i 2024-09-16 03:55:40

如果您使用的连接字符串使用了providerName并且您还没有安装SDK,那么您还需要将其添加到您的web.config(或app.config)中

  <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
        <bindingRedirect oldVersion="4.0.0.0-4.0.0.1" newVersion="4.0.0.1"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0"/>
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
    </DbProviderFactories>
  </system.data>

注意:如果您安装了SDK,则需要“删除” SDK,因为这会将这些信息放入您的 machine.config 中

If your using a connection string that uses a providerName and you haven't installed the SDK, then you also need to add this to you web.config (or app.config)

  <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
        <bindingRedirect oldVersion="4.0.0.0-4.0.0.1" newVersion="4.0.0.1"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0"/>
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
    </DbProviderFactories>
  </system.data>

NOTE: the "remove" is needed in case you installed the SDK, as that will put this info in your machine.config

鹿港小镇 2024-09-16 03:55:40

OK, here's a guess, since you're fishing for them.

Run corflags.exe on the assembly you copied to your references directory. What type of machine are you building for? If you're on a 64-bit machine and you're compiling to x64 or anyCpu, make sure that corflags tells you that your references are not 32-bit only references. Maybe it's "falling back" to an the wrong version in your GAC or something. If it tells you that the referenced assembly is 32-bit only, either compile your project as a 32-bit project or find a 64-bit version of the DLL?

尘世孤行 2024-09-16 03:55:40

如果要使用 NuGet 安装 SQL CE 提供程序,最简单的解决方案是添加构建后步骤以从 NuGet 包 NativeBinaries 文件夹中复制这些内容

If you are installing the SQL CE provider using NuGet, the simplest solution is to add a post-build step to copy these from the NuGet package NativeBinaries folder

鹿童谣 2024-09-16 03:55:40

对我来说,关键是意识到 Private 目录 (C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private) 中 System.Data.SqlServerCe.Entity.dll 的版本是 4.0.0.1,其中版本桌面目录 (C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.Entity) 下的文件是 4.0.0.0。 Private目录中的System.Data.SqlServerCe.dll的版本是4.0.0.0。

我认为 Microsoft 分发更新的 SqlServerCe.Entity.dll 而不对 SqlServer.dll 进行相应的更新是一个错误。

The key for me was realizing that the version of System.Data.SqlServerCe.Entity.dll in the Private directory (C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private) is 4.0.0.1, where the version beneath the Desktop directory (C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.Entity) is 4.0.0.0. The version of System.Data.SqlServerCe.dll in the Private directory is 4.0.0.0.

I think it was a mistake on the part of Microsoft to distribute an updated SqlServerCe.Entity.dll without a corresponding update to SqlServer.dll.

蓝眼泪 2024-09-16 03:55:40

我构建了一个 asp.net Web api 并将其托管在 azure 上,并遇到了 sql server Compact 的一些问题,我通过以下方式修复它:
首先删除所有system.data.sqlserverce.dll和任何使用它的dll
安装了这两个软件包:

Install-Package SqlServerCompact 然后重建

Install-Package EntityFramework.SqlServerCompact -Version 4.3.6 然后

在我完成后重建它对我来说效果很好+安装任何依赖于 system.data.sqlserverce.dll 的 NuGet 包它们都工作得很好
我希望这会对某人有所帮助
参考

I build an asp.net web api and hosted it on azure and faced some issues with sql server compact I fix it by:
first remove all system.data.sqlserverce.dll and any dll use it then
installed these tow packages :

Install-Package SqlServerCompact then rebuild

Install-Package EntityFramework.SqlServerCompact -Version 4.3.6 then rebuild

after I did it just worked fine for me + install any NuGet package that depends on system.data.sqlserverce.dll they all just work great
I hope this will help some one
reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文