我有问题。我想使用Sync Framework将我的本地MSSQL数据库同步到托管的MySQL数据库,但我遇到了问题,因为我的代码目前使用SqlConnection
,但我不知道如何能够使serverConn
1}}一个MySqlConnection。
我的代码:
AppConnectionFactory.cs:
public class AppConnectionFactory : IConnectionFactory
{
private readonly DbProviderFactory _provider;
private readonly string _connectionString;
private readonly string _name;
public AppConnectionFactory(string connectionName)
{
if (connectionName == null)
throw new ArgumentNullException("connectionName");
var connStr = ConfigurationManager.ConnectionStrings[connectionName];
if (connStr == null)
throw new ConfigurationErrorsException(string.Format("Failed to find the connection named {0} in App.config",connectionName));
_name = connStr.ProviderName;
_provider = DbProviderFactories.GetFactory(connStr.ProviderName);
_connectionString = connStr.ConnectionString;
}
public IDbConnection Create()
{
var connection = _provider.CreateConnection();
connection.ConnectionString = _connectionString;
connection.Open();
return connection;
}
}
DbSync.cs:
public class DbSync
{
private readonly IConnectionFactory _serverConn;
private readonly IConnectionFactory _clientConn;
private readonly string _sScope = "SmartFridgeScope";
public DbSync(IConnectionFactory serverConn, IConnectionFactory clientConn)
{
_serverConn = serverConn;
_clientConn = clientConn;
}
public void ProvisionServer()
{
var serverConn = (SqlConnection)_serverConn.Create();
var serverProvision = new SqlSyncScopeProvisioning(serverConn);
if (serverProvision.ScopeExists(_sScope)) return;
var scopeDesc = new DbSyncScopeDescription(_sScope);
var listDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("List", serverConn);
scopeDesc.Tables.Add(listDesc);
var itemDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Item", serverConn);
scopeDesc.Tables.Add(itemDesc);
var listItemDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("ListItem",
serverConn);
scopeDesc.Tables.Add(listItemDesc);
serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc);
serverProvision.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
serverProvision.Apply();
}
public void ProvisionClient()
{
var clientConn = (SqlConnection)_clientConn.Create();
var serverConn = (SqlConnection)_serverConn.Create();
var clientProvision = new SqlSyncScopeProvisioning(clientConn);
if (clientProvision.ScopeExists(_sScope)) return;
var scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(_sScope, serverConn);
clientProvision = new SqlSyncScopeProvisioning(clientConn, scopeDesc);
clientProvision.Apply();
}
public void Sync()
{
var clientConn = (SqlConnection)_clientConn.Create();
var serverConn = (SqlConnection)_serverConn.Create();
var syncOrchestrator = new SyncOrchestrator
{
LocalProvider = new SqlSyncProvider(_sScope, clientConn),
RemoteProvider = new SqlSyncProvider(_sScope, serverConn),
Direction = SyncDirectionOrder.DownloadAndUpload
};
syncOrchestrator.Synchronize();
}
}
任何人都可以帮助我使用MySqlConnection而不是SqlConnection吗?
告诉我是否需要解释更多。提前谢谢。