c# to vb using Sync Framework 奇怪的错误
c# to vb using Sync Framework strange error
我遵循了 Microsoft 的 Sync Framework 演练,它是用 C# 编写的。我希望将其翻译成 VB。我已经尽可能地简化了代码。两种代码都可以正常编译。它们在同一解决方案的 2 个项目下 运行。 C# 运行正常,但 VB 给出以下错误。
System.Data.SqlClient.SqlException 未处理
Message=建立与 SQL 服务器的连接时出现与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确并且 SQL 服务器配置为允许远程连接。 (提供商:SQL 网络接口,错误:50 - 发生本地数据库运行时错误。指定的 LocalDB 实例名称无效。
)
搜索此错误会导致连接字符串出错,但如您所见,两个连接字符串是相同的。唯一的区别是一个在 C# 中是 运行 另一个在 VB.
C#代码是:
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);
}
}
}
而 VB 代码是:
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
错误发生在
Dim syncStats As SyncOperationStatistics = syncOrchestrator.Synchronize()
C# 代码在连接字符串中包含双反斜杠,因为需要在字符串文字(或使用逐字字符串文字)中转义它们。在 VB 中不需要,因为 \
不是转义字符(据我所知)所以你不应该将它们加倍:
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")
我遵循了 Microsoft 的 Sync Framework 演练,它是用 C# 编写的。我希望将其翻译成 VB。我已经尽可能地简化了代码。两种代码都可以正常编译。它们在同一解决方案的 2 个项目下 运行。 C# 运行正常,但 VB 给出以下错误。
System.Data.SqlClient.SqlException 未处理 Message=建立与 SQL 服务器的连接时出现与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确并且 SQL 服务器配置为允许远程连接。 (提供商:SQL 网络接口,错误:50 - 发生本地数据库运行时错误。指定的 LocalDB 实例名称无效。 )
搜索此错误会导致连接字符串出错,但如您所见,两个连接字符串是相同的。唯一的区别是一个在 C# 中是 运行 另一个在 VB.
C#代码是:
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);
}
}
}
而 VB 代码是:
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
错误发生在
Dim syncStats As SyncOperationStatistics = syncOrchestrator.Synchronize()
C# 代码在连接字符串中包含双反斜杠,因为需要在字符串文字(或使用逐字字符串文字)中转义它们。在 VB 中不需要,因为 \
不是转义字符(据我所知)所以你不应该将它们加倍:
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")