Link to home
Start Free TrialLog in
Avatar of k heitz
k heitz

asked on

LocalDB 'Cannot Open Database' erro

Dear Experts;
I'm working with a Microsoft walkthru to Synchroinze a SQL Server database and a LocalDB database.
I've created and provisioned both dbs successfully, and was able to execute the synchronization project successfully ... once.
When I try to run the synchronization a second time (I added a record to LocalDB and was testing upload sync); I get a 'Cannot open database' exception (full detail attached).

It complains about login, but I was able to provision the localDB w/ the same connection string; and synchronization ran the first time, so I'm not sure login is really the issue.

Below is my code (C#). I'm using LocalDB ver 11.0, and SQL Server 2008.
Any guidance is appreciated.
Thank you!

**** code *** (error occurs on SyncOperationStatistics syncstats = syncOrchestrator.Synchronize(); line)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using Microsoft.Synchronization.Data.SqlServerCe;

namespace ExecuteExpressSync
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection clientConn = new SqlConnection(@"Data source = (LocalDb)\v11.0;AttachDbFilename=c:\SyncSQLServerAndSQLCompact\database\SyncExpressDB.mdf;Integrated Security=True");
            //SqlConnection clientConn = new SqlConnection(@"Data source = (LocalDb)\v11.0;Initial Catalog=SyncExpressDB;Integrated Security=True");

            SqlConnection serverConn = new SqlConnection("Data Source=<MyServer>.com; Initial Catalog=SyncDB; User ID = <UN>; Password = <PW>;");

            // create the sync orchestrator
            SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

            // set local proivder of orchestrator to sync provider associated with the
            // ProductsScope in the SyncExpressDB express client database
            syncOrchestrator.LocalProvider = new SqlSyncProvider("ProductsScope", clientConn);

            // set the remote provider of orchestrator to a server sync provider associated with
            // the ProductsScope in the SyncDB server database
            syncOrchestrator.RemoteProvider = new SqlSyncProvider("ProductsScope", serverConn);

            // set the direction of sync session to upload and download TODO: change to download only
            syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

            // subscribe for errors that occur when applying changes to the client
//**** Error occurs on next line ****
            SyncOperationStatistics syncstats = syncOrchestrator.Synchronize();

            // print statistics
            Console.WriteLine("Start Time: " + syncstats.SyncStartTime);
            Console.WriteLine("Total Changes Uploaded: " + syncstats.UploadChangesTotal);
            Console.WriteLine("Total Changes Downloaded: " + syncstats.DownloadChangesTotal);
            Console.WriteLine("Complete Time: " + syncstats.SyncEndTime);
            Console.WriteLine(string.Empty);

        }

        static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
        {
            // display conflict type
            Console.WriteLine(e.Conflict.Type);

            // display error message
            Console.WriteLine(e.Error);
        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of k heitz
k heitz

ASKER

Just what I needed! Thanks!!