C#代码转VB.NET,错误求解!!
C#原码
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using Microsoft.Synchronization.Data.SqlServerCe;
namespace ExecuteCompactFilteredSync
{
class Program
{
static void Main(string[] args)
{
// create a connection to the SyncCompactDB database
SqlCeConnection clientConn = new SqlCeConnection(@"Data Source='C:\SyncSQLServerAndSQLCompact\SyncCompactDB.sdf'");
// create a connection to the SyncDB server database
SqlConnection serverConn = new SqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated Security=True");
// create the sync orhcestrator
SyncOrchestrator syncOrchestrator = new SyncOrchestrator();
// set local provider of orchestrator to a CE sync provider associated with the
// ProductsScope in the SyncCompactDB compact client database
syncOrchestrator.LocalProvider = new SqlCeSyncProvider("OrdersScope-NC", 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("OrdersScope-NC", serverConn);
// set the direction of sync session to Upload and Download
syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;
// subscribe for errors that occur when applying changes to the client
((SqlCeSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
// execute the synchronization process
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);
}
}
}
-------------------------------------------------------------
转换后VB.NET(红色字部分提示错误,求解!!)
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlServerCe
Imports Microsoft.Synchronization
Imports Microsoft.Synchronization.Data
Imports Microsoft.Synchronization.Data.SqlServer
Imports Microsoft.Synchronization.Data.SqlServerCe
Namespace ExecuteCompactFilteredSync
Class Program
Shared Sub Main(args As String())
' create a connection to the SyncCompactDB database
Dim clientConn As New SqlCeConnection("Data Source='C:\SyncSQLServerAndSQLCompact\SyncCompactDB.sdf'")
' create a connection to the SyncDB server database
Dim serverConn As New SqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated Security=True")
' create the sync orhcestrator
Dim syncOrchestrator As New SyncOrchestrator()
' set local provider of orchestrator to a CE sync provider associated with the
' ProductsScope in the SyncCompactDB compact client database
syncOrchestrator.LocalProvider = New SqlCeSyncProvider("OrdersScope-NC", 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("OrdersScope-NC", serverConn)
' set the direction of sync session to Upload and Download
syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload
' subscribe for errors that occur when applying changes to the client
(CType(syncOrchestrator.LocalProvider, SqlCeSyncProvider)).ApplyChangeFailed += New EventHandler(Of DbApplyChangeFailedEventArgs)(Program_ApplyChangeFailed)
'错误原因是:语法错误!!求正解!!
' execute the synchronization process
Dim syncStats As SyncOperationStatistics = 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)
End Sub
Shared Sub Program_ApplyChangeFailed(sender As Object, e As DbApplyChangeFailedEventArgs)
' display conflict type
Console.WriteLine(e.Conflict.Type)
' display error message
Console.WriteLine(e.[Error])
End Sub
End Class
End Namespace C# VB.NET
[解决办法]
前面少写了个AddHandler
AddHandler CType(syncOrchestrator.LocalProvider,SqlCeSyncProvider).ApplyChangeFailed ,
AddressoOf Program_ApplyChangeFailed