c# to vb using Sync Framework strange error

395 views Asked by At

I followed Microsoft's walkthrough for Sync Framework which is written in C#. And I wish to translate it to VB. I have simplified the code as much as possible. Both codes compile fine. They are running under 2 projects within the same solution. C# runs fine, but VB gives the following error.

System.Data.SqlClient.SqlException was unhandled Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid. )

Searching for this error, it leads toward an error in the connection string, but as you can see both connection strings are identical. The only difference is that one is running in C# the other in VB.

The C# Code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;


namespace ExecuteExpressSync
{
class Program
{
    static void Main(string[] args)
    {

        SqlConnection clientConn = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=c:\\Database\\SyncExpressDB.mdf;Integrated Security=True");
        SqlConnection serverConn = new SqlConnection("Data Source=Toshiba\\SQLEXPRESST;Initial Catalog=SyncDB;User ID=sa;Password=XXX");

        SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

        syncOrchestrator.LocalProvider = new SqlSyncProvider("ProductsScope", clientConn);
        syncOrchestrator.RemoteProvider = new SqlSyncProvider("ProductsScope", serverConn);

        syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

        SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();

        Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);

    }

}
}

and the VB code is:

Imports System.Data.SqlClient
Imports Microsoft.Synchronization
Imports Microsoft.Synchronization.Data
Imports Microsoft.Synchronization.Data.SqlServer

Module Module1

Sub Main()

    Dim clientConn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=c:\\Database\\SyncExpressDB.mdf;Integrated Security=True")
    Dim serverConn As SqlConnection = New SqlConnection("Data Source=Toshiba\\SQLEXPRESST;Initial Catalog=SyncDB;User ID=sa;Password=XXX")

    Dim syncOrchestrator As SyncOrchestrator = New SyncOrchestrator

    syncOrchestrator.LocalProvider = New SqlSyncProvider("ProductsScope", clientConn)
    syncOrchestrator.RemoteProvider = New SqlSyncProvider("ProductsScope", serverConn)

    syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload

    Dim syncStats As SyncOperationStatistics = syncOrchestrator.Synchronize()

    Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal)

End Sub


End Module

The error occurs in

Dim syncStats As SyncOperationStatistics = syncOrchestrator.Synchronize()
1

There are 1 answers

1
Jon Skeet On BEST ANSWER

The C# code contains doubled backslashes in the connection strings, because that's needed to escape them in string literals (or using a verbatim string literal). In VB that's not needed as \ isn't an escape character (as far as I'm aware) so you shouldn't double them:

Dim clientConn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Database\SyncExpressDB.mdf;Integrated Security=True")
Dim serverConn As SqlConnection = New SqlConnection("Data Source=Toshiba\SQLEXPRESST;Initial Catalog=SyncDB;User ID=sa;Password=XXX")