代码之家  ›  专栏  ›  技术社区  ›  jason

无法使sql server compact 3.5/4与ASP.NET MVC 2一起使用

  •  22
  • jason  · 技术社区  · 16 年前

    我正在使用VisualStudio2008Pro。

    我通过sqlstudioexpress创建了一个sdf文件。一个重要的注意事项是,我没有看到创建这个文件的ce4.0版本的选项,所以它是使用ce3.5创建的。我创建了一些表,向这些表中添加了一些行,将*.sdf文件复制到我的App\u Data目录中。值得一提的是,在VS2008内部,这个文件从未出现在我的项目中,但它确实存在于App\u 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.
    

    我想,从这里开始,我会尝试让SQLCE3.5正常工作。我将本地安装的SQLCE3.5升级到sp2。我在基本位置(c:\Program Files\Sql Server compact\v3.5)复制dll,包括从项目引用中删除和读取System.Data.SqlServerCE dll的版本。

    伙计们,我真的需要一些指导。我搜索了stack overflow、帮助文档、在线书籍和Google。对于ce3.5或4.0来说,我真的没有找到任何东西能从最上面的内容来告诉我要添加什么dll,放在哪里,如何引用它们,如何将.sdf文件添加到我的项目中,连接到它,并从中进行查询。我确实遇到过几次提到IBuySpy门户示例应用程序,该应用程序本应使用sqlce3.5,但实际上无法在msdn下载迷宫中找到它。理想情况下,我想为ce4.0设置一个私有部署。

    我洗耳恭听。建议,要点,无论什么都将不胜感激。谢谢您!

    是的,我确实看到了知识库。没用的

    请看这里: http://support.microsoft.com/kb/974247

    好吧,我试过了,结果如下: C:\Development\mvc2msingaround\mvc2msingaround\bin\Lib>corflags System.Data。 SqlServerCe.dll Microsoft(R).NET Framework转换工具。版本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
    

    我发誓我安装了两个版本的SQLCE(3.5/4)的x86版本。安装程序可能有点困惑,因为我的处理器是64位功能,但我运行的是WindowsXPSP332位。结果似乎表明它是64位的。是这样吗?

    到目前为止,以下配置已在两台机器上试用。两者都是Windows xp sp3 32位,带有64位处理器。两者的开发环境都是vs2008pro。机器2上的结果是在新安装了sqlce4ctp之后得到的。

    配置#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();
    

    配置2

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

    无法加载文件或程序集“System.Data.SqlServerCe.Entity”或其依赖项之一。此程序集由比当前加载的运行时新的运行时生成,因此无法加载。

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

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

    配置3

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

    按照埃里克的指示(如果我更仔细地遵循),设置应该是

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

    帮助链接:

    http://blogs.msdn.com/b/sqlservercompact/archive/2010/07/07/introducing-sql-server-compact-4-0-the-next-gen-embedded-database-from-microsoft.aspx

    5 回复  |  直到 16 年前
        1
  •  31
  •   John Raymund    14 年前

    SQL CE 3.5不适用于ASP.NET,必须使用4.0 CTP。

    下载自 here

    安装运行时。

    C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private版

    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文件夹中的System.Data.SqlServerCe.dll文件的引用。

    添加连接字符串:

    <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()));
                }
            }
        }
    
        2
  •  13
  •   Eric Labashosky    15 年前

    如果您使用的连接字符串使用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>
    

        3
  •  2
  •   Community Mohan Dere    9 年前

    好吧,我猜,既然你是在钓它们。

    corflags.exe 在复制到引用目录的程序集上。你在造什么类型的机器?如果您在64位计算机上并且正在编译到x64或anyCpu,请确保corflags告诉您您的引用不是仅32位的引用。可能是你的GAC版本“掉回去”了。如果它告诉您引用的程序集仅为32位,请将项目编译为32位项目或查找64位版本的DLL?

        4
  •  1
  •   Dale Anderson    11 年前

    如果您使用NuGet安装sqlce提供程序,最简单的解决方案是添加一个生成后步骤,从NuGet包NativeBinaries文件夹复制这些内容

        5
  •  0
  •   Mark McClelland    15 年前

    我认为微软发布更新的SqlServerCe.Entity.dll而没有相应的SqlServer.dll更新是一个错误。