gpt4 book ai didi

c# - 以编程方式创建和执行合并复制

转载 作者:行者123 更新时间:2023-11-30 15:12:39 24 4
gpt4 key购买 nike

让我先说一下,我现在意识到我过去和现在是多么愚蠢。我已经开发了 1 年(到今天),这是我写的第一件事。我现在又回到了它,我无法理解它的正面或反面。它曾在一个非常简单的应用程序上运行过,但那是不久前的事了。

具体来说,我在使用 outLocalDBConn 方面遇到了问题,但我一直记不起原因。

指导、指点、重构、拍脑袋都是受欢迎和赞赏的!

public class MergeRepl
{
// Declare nessesary variables
private string subscriberName;
private string publisherName;
private string publicationName;
private string subscriptionDbName;
private string publicationDbName;
private MergePullSubscription mergeSubscription;
private MergePublication mergePublication;
private ServerConnection subscriberConn;
private ServerConnection publisherConn;
private Server theLocalSQLServer;
private ReplicationDatabase localRepDB;

public MergeRepl(string subscriber, string publisher, string publication, string subscriptionDB, string publicationDB)
{
subscriberName = subscriber;
publisherName = publisher;
publicationName = publication;
subscriptionDbName = subscriptionDB;
publicationDbName = publicationDB;

//Create connections to the Publisher and Subscriber.
subscriberConn = new ServerConnection(subscriberName);
publisherConn = new ServerConnection(publisherName);


// Define the pull mergeSubscription
mergeSubscription = new MergePullSubscription
{
ConnectionContext = subscriberConn,
DatabaseName = subscriptionDbName,
PublisherName = publisherName,
PublicationDBName = publicationDbName,
PublicationName = publicationName
};

// Ensure that the publication exists and that it supports pull subscriptions.
mergePublication = new MergePublication
{
Name = publicationName,
DatabaseName = publicationDbName,
ConnectionContext = publisherConn
};

// Create the local SQL Server instance
theLocalSQLServer = new Server(subscriberConn);
// Create a Replication DB Object to initiate Replication settings on local DB
localRepDB = new ReplicationDatabase(subscriptionDbName, subscriberConn);

// Check that the database exists locally
CreateDatabase(subscriptionDbName);
}

public void RunDataSync()
{
// Keep program from appearing 'Not Responding'
///// Application.DoEvents();

// Does the needed Databases exist on local SQLExpress Install
/////CreateDatabase("ContactDB");

try
{
// Connect to the Subscriber
subscriberConn.Connect();

// if the Subscription exists, then start the sync
if (mergeSubscription.LoadProperties())
{
// Check that we have enough metadata to start the agent
if (mergeSubscription.PublisherSecurity != null || mergeSubscription.DistributorSecurity != null)
{
// Synchronously start the merge Agent for the mergeSubscription
// lblStatus.Text = "Data Sync Started - Please Be Patient!";
mergeSubscription.SynchronizationAgent.Synchronize();
}
else
{
throw new ApplicationException("There is insufficient metadata to synchronize the subscription." +
"Recreate the subscription with the agent job or supply the required agent properties at run time.");
}
}
else
{
// do something here if the pull mergeSubscription does not exist
// throw new ApplicationException(String.Format("A mergeSubscription to '{0}' does not exist on {1}", publicationName, subscriberName));
CreateMergeSubscription();
}
}
catch (Exception ex)
{
// Implement appropriaate error handling here
throw new ApplicationException("The subscription could not be synchronized. Verify that the subscription has been defined correctly.", ex);
//CreateMergeSubscription();
}
finally
{
subscriberConn.Disconnect();
}
}

public void CreateMergeSubscription()
{
// Keep program from appearing 'Not Responding'
// Application.DoEvents();

try
{

if (mergePublication.LoadProperties())
{
if ((mergePublication.Attributes & PublicationAttributes.AllowPull) == 0)
{
mergePublication.Attributes |= PublicationAttributes.AllowPull;
}

// Make sure that the agent job for the mergeSubscription is created.
mergeSubscription.CreateSyncAgentByDefault = true;

// Create the pull mergeSubscription at the Subscriber.
mergeSubscription.Create();

Boolean registered = false;

// Verify that the mergeSubscription is not already registered.
foreach (MergeSubscription existing in mergePublication.EnumSubscriptions())
{
if (existing.SubscriberName == subscriberName
&& existing.SubscriptionDBName == subscriptionDbName
&& existing.SubscriptionType == SubscriptionOption.Pull)
{
registered = true;
}
}
if (!registered)
{
// Register the local mergeSubscription with the Publisher.
mergePublication.MakePullSubscriptionWellKnown(
subscriberName, subscriptionDbName,
SubscriptionSyncType.Automatic,
MergeSubscriberType.Local, 0);
}
}
else
{
// Do something here if the publication does not exist.
throw new ApplicationException(String.Format(
"The publication '{0}' does not exist on {1}.",
publicationName, publisherName));
}
}
catch (Exception ex)
{
// Implement the appropriate error handling here.
throw new ApplicationException(String.Format("The subscription to {0} could not be created.", publicationName), ex);

}
finally
{
publisherConn.Disconnect();
}
}

/// <summary>
/// This will make sure the needed DataBase exists locally before allowing any interaction with it.
/// </summary>
/// <param name="whichDataBase">The name of the DataBase to check for.</param>
/// <returns>True if the specified DataBase exists, False if it doesn't.</returns>
public void CreateDatabase(string whichDataBase)
{
Database db;
LocalDBConn(whichDataBase, out theLocalSQLServer, out localRepDB, out db);


if (!theLocalSQLServer.Databases.Contains(whichDataBase))
{
//Application.DoEvents();
// Create the database on the instance of SQL Server.
db = new Database(theLocalSQLServer, whichDataBase);
db.Create();

}

localRepDB.Load();
localRepDB.EnabledMergePublishing = false;
localRepDB.CommitPropertyChanges();

if (!mergeSubscription.LoadProperties())
{
CreateMergeSubscription();
}

}

private void LocalDBConn(string databaseName, out Server server, out ReplicationDatabase replicationDatabase, out Database db)
{
db = server.Databases[replicationDatabase.Name];
}

/// <summary>
/// Checks for the existince of the Publication. If there is one it verify's Allow Pull is set
/// </summary>
/// <returns>True if Publication is present. False if not.</returns>
public bool CheckForPublication()
{
// If LoadProperties() returns TRUE then the Publication exists and is reachable
if (mergePublication.LoadProperties())
return true;

if ((mergePublication.Attributes & PublicationAttributes.AllowPull) == 0)
{
mergePublication.Attributes |= PublicationAttributes.AllowPull;
}

return false;
} // end CheckForPublication()

/// <summary>
/// Checks for the existence of a Subscription.
/// </summary>
/// <returns>True if a Subscription is present. False if not</returns>
public bool CheckForSubscription()
{
// Check for the existence of the Subscription
return mergeSubscription.IsExistingObject;
} // end CheckForSubscription()
}

编辑1

哎呀,具体错误忘记了。在 serverreplicationDatabase 上,我得到一个“Out parameter might not be initialized before accessing”

        private void LocalDBConn(string databaseName, out Server server, out ReplicationDatabase replicationDatabase, out Database db)
{
db = server.Databases[replicationDatabase.Name];
}

最佳答案

看起来你可以安全地移除 out 的。

我什至不确定它是如何编译的,除非可能是使用 VS2003 并且编译器没有检查此类错误。

来自 MSDN:虽然作为 out 参数传递的变量在传递之前不必初始化,但被调用的方法需要在方法返回之前赋值。

private void LocalDBConn(string databaseName, Server server, 
ReplicationDatabase replicationDatabase, out Database db)
{
db = server.Databases[replicationDatabase.Name];
}

private Database LocalDBConn(string databaseName, Server server, 
ReplicationDatabase replicationDatabase)
{
return server.Databases[replicationDatabase.Name];
}

然后将您在 CreateDatabase 中的代码更新为:

Database db;
LocalDBConn(whichDataBase, theLocalSQLServer, localRepDB, out db);

Database db = LocalDBConn(whichDataBase, theLocalSQLServer, localRepDB);

关于c# - 以编程方式创建和执行合并复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/941720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com