Question

I have two databases: dbGlobal is SQL Server 2008, and dbLocal is SQL Server 2008 CE. For business reasons, I need to keep some data local to a user while other data global to all users. In many cases, I'll have a table in dbLocal that has a column which references a table in dcGlobal. In my C# code, I will want to have certain database operations happen within a transaction.

I've read that a single TransactionScope object can handle two database connections. I think this may not be the case if one of those two database connections connects to a SQL Server CE instance. Is this true? Here's my code:

using (TransactionScope scope = new TransactionScope())
{
    using (DataContext dcGlobal = new DataContext(Program.dbGlobal), 
        dcLocal = new DataContext(Program.dbLocal))
    {
        ITable<tblDbGlobal> tblGlobal = dcGlobal.GetTable<tblDbGlobal>();
        ITable<tblDbLocal> tblLocal = dcLocal.GetTable<tblDbLocal>();

        //Invalid Operation Exception here
        var join = from tGlobal in tblGlobal.ToList()
                    join tLocal in tblLocal.ToList() on 
                        tGlobal.PkDbGlobal equals tLocal.FkDbGlobal
                    select new { tGlobal.Name, tLocal.Gender };

        foreach (var item in join)
        {
            Console.WriteLine(string.Format("{0}, {1}", item.Name, item.Gender));
        }
    }
}

When I run this code, I get an Invalid Operation Exception with the message "The connection object can not be enlisted in transaction scope." at the var join ... line.

While dcLocal runs on localhost, dcGlobal runs on a different computer on the network. I double-checked that MS DTC (Distributed Transaction Controller) is running on both computers, and I ensured that the security settings were set up to allow almost anything. I've also got my windows firewall turned off on both computers.

I tried using the DTCPing tool but I'm getting a couple of errors with that. If I type in the full computer name of each computer to the other's DTCPing program, nothing happens. I neither get a success nor failure message. I can click on the PING button all day.

How can I set up a transaction for these two databases?

I realize that I probably don't need a transaction scope for a select query. I'm just using this as an example. I will have more complicated queries that involve inserts, updates, and deletes that should happen within a transaction that can span two databases.

Was it helpful?

Solution

You cannot use Distributed transaction with SQL Server Compact http://msdn.microsoft.com/en-us/library/bb896149.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top