locked
Cannot load SQL Server Compact DLL while creating new SqlCeReplication RRS feed

  • Question

  • I am writing a WinCE 5 application which will do a synchronization with my server.

    Sometimes when I finish my jobs and want to upload the data, it will have an error

    A SQL Server Compact DLL could not be loaded. Reinstall SQL Server Compact Edition [ DLL Name = sqlceca35.dll ]

    while constructing the replication

    SqlCeReplication repl = new SqlCeReplication();

    I am sure that my installation of SQLCE do not have any problem, as when I restart the application and do the synchronization job as the first job, the synchronization will success.

    I think that it would be a memory issue, as the failure would only happen if

    - I do not call the replication when I start the application

    - Certain works (loading database and do some SQL query) are done and use up some memory

    I have tried to release the memory using the dispose function and garbage collect, but it doesn't work.

    Do anyone have some suggestions? Should I preload all SQLCE DLL files while starting the application?

    Wednesday, November 2, 2011 10:40 AM

Answers

  • Hi,
    The "secret" here is to load early, use it when you need it. This thread gives you some hints, this is how we solved it:
    Public Declare Function LoadLibrary Lib "coredll.dll" (ByVal fileName As String) As IntPtr
    
    Public Declare Function GetModuleHandle Lib "coredll.dll" (ByVal ModuleName As String) As IntPtr
    
        Shared Sub InicializeReplication()
            Dim regKey As Global.Microsoft.Win32.RegistryKey = Global.Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Microsoft SQL Server Compact Edition\v3.5")
    
            Dim s As String = String.Format("{0}\", regKey.GetValue("InstallDir"))
            regKey.Close()
            If Not LoadDLL(String.Format("{0}sqlceoledb35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlceqp35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlcese35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlcecompact35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlceme35.dll", s)) Then ExitApplication()
    
    'this is a SqlCeReplication object instance that later will be used to invoke the replication process
    mssceRepl = New SqlCeReplication End Sub Private Shared Function LoadDLL(ByVal pstrLibraryName As String) As Boolean Dim hModule As IntPtr = GetModuleHandle(pstrLibraryName) Dim result As Boolean = True 'Check whether the sqlceoledb30.dll is loaded If (hModule = IntPtr.Zero) Then 'load this DLL hModule = LoadLibrary(pstrLibraryName) If (hModule = IntPtr.Zero) Then MsgBox(String.Format("Error while loading '{0}'", pstrLibraryName)) result = False End If End If Return result End Function

     You just have to make sure you call InitializeReplication as soon as the app starts.

    Alberto Silva Microsoft MVP - Device Application Development - http://msmvps.com/AlbertoSilva moving2u - R&D Manager - http://www.moving2u.pt
    • Edited by Alberto Silva Thursday, November 3, 2011 11:10 AM bad code formatting
    • Marked as answer by ErikEJMVP Thursday, November 3, 2011 11:12 AM
    Thursday, November 3, 2011 11:07 AM

All replies

  • It is a memory issue yes, make sure to dispose of all connection and command objects, also make sure not to use datasets, but datareader or SqlCeResultSet instead.


    Please mark as answer, if this was it. Visit my SQL Server Compact blog
    Wednesday, November 2, 2011 1:38 PM
  • I have read the blog post in http://blogs.msdn.com/b/sqlservercompact/archive/2007/10/26/troubleshooting-can-t-load-sqlce-dll.aspx, and have tried the following works.

    - Combine the small dlls into several large dlls using the ILMerge.

    - Check that there are no connections are made to the database (I can rename the database at that time, so that it is not been lock)

    - No dataset is used

    - Load the GC(garbage collector) more, to release as much as memory it can.

    That still do not enough. I have read document talking about using CoFreeUnusedLibraries to unload some dll which is unused, is it a good method to release memory for replication?

     

    Thursday, November 3, 2011 10:22 AM
  • Hi,
    The "secret" here is to load early, use it when you need it. This thread gives you some hints, this is how we solved it:
    Public Declare Function LoadLibrary Lib "coredll.dll" (ByVal fileName As String) As IntPtr
    
    Public Declare Function GetModuleHandle Lib "coredll.dll" (ByVal ModuleName As String) As IntPtr
    
        Shared Sub InicializeReplication()
            Dim regKey As Global.Microsoft.Win32.RegistryKey = Global.Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Microsoft SQL Server Compact Edition\v3.5")
    
            Dim s As String = String.Format("{0}\", regKey.GetValue("InstallDir"))
            regKey.Close()
            If Not LoadDLL(String.Format("{0}sqlceoledb35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlceqp35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlcese35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlcecompact35.dll", s)) Then ExitApplication()
            If Not LoadDLL(String.Format("{0}sqlceme35.dll", s)) Then ExitApplication()
    
    'this is a SqlCeReplication object instance that later will be used to invoke the replication process
    mssceRepl = New SqlCeReplication End Sub Private Shared Function LoadDLL(ByVal pstrLibraryName As String) As Boolean Dim hModule As IntPtr = GetModuleHandle(pstrLibraryName) Dim result As Boolean = True 'Check whether the sqlceoledb30.dll is loaded If (hModule = IntPtr.Zero) Then 'load this DLL hModule = LoadLibrary(pstrLibraryName) If (hModule = IntPtr.Zero) Then MsgBox(String.Format("Error while loading '{0}'", pstrLibraryName)) result = False End If End If Return result End Function

     You just have to make sure you call InitializeReplication as soon as the app starts.

    Alberto Silva Microsoft MVP - Device Application Development - http://msmvps.com/AlbertoSilva moving2u - R&D Manager - http://www.moving2u.pt
    • Edited by Alberto Silva Thursday, November 3, 2011 11:10 AM bad code formatting
    • Marked as answer by ErikEJMVP Thursday, November 3, 2011 11:12 AM
    Thursday, November 3, 2011 11:07 AM