📄 controlreplication.cs
字号:
this.panelProps.Enabled = false;
buttonSync.Text = "Synchronize";
}
}
// This function loads the connection information info from the "ApplicationProperties" table.
//
private void LoadAppProps() {
// Create a SqlCeCommand object associated with the connection to NorthwindDemo database.
//
SqlCeCommand cmd = dataNorthwind.NorthwindConnection.CreateCommand();
// Load the connect information from the ApplicationProperties table in the NorthwindDemo database.
//
cmd.CommandText = @"SELECT InternetURL, InternetLogin, InternetPassword, Publisher, PublisherLogin, PublisherPassword, PublisherSecurityMode, Subscriber FROM ApplicationProperties;";
SqlCeDataReader drAppProp = null;
try {
drAppProp = cmd.ExecuteReader();
if (drAppProp.Read()) {
int index;
// Get the Internet URL data.
//
index = drAppProp.GetOrdinal("InternetURL");
if (!drAppProp.IsDBNull(index)) {
this.textBoxInternetUrl.Text = drAppProp.GetString(index);
}
else {
this.textBoxInternetUrl.Text = "";
}
// Get the Internet login data.
//
index = drAppProp.GetOrdinal("InternetLogin");
if (!drAppProp.IsDBNull(index)) {
this.textBoxInternetLogin.Text = drAppProp.GetString(index);
}
else {
this.textBoxInternetLogin.Text = "";
}
// Get the Internet password data.
//
index = drAppProp.GetOrdinal("InternetPassword");
if (!drAppProp.IsDBNull(index)) {
this.textBoxInternetPwd.Text = drAppProp.GetString(index);
}
else {
this.textBoxInternetPwd.Text = "";
}
// Get the publisher data.
//
index = drAppProp.GetOrdinal("Publisher");
if (!drAppProp.IsDBNull(index)) {
this.textBoxPublisher.Text = drAppProp.GetString(index);
}
else {
this.textBoxPublisher.Text = "";
}
// Get the publisher login data.
//
index = drAppProp.GetOrdinal("PublisherLogin");
if (!drAppProp.IsDBNull(index)) {
this.textBoxUserID.Text = drAppProp.GetString(index);
}
else {
this.textBoxUserID.Text = "";
}
// Get the publisher password data.
//
index = drAppProp.GetOrdinal("PublisherPassword");
if (!drAppProp.IsDBNull(index)) {
this.textBoxPassword.Text = drAppProp.GetString(index);
}
else {
this.textBoxPassword.Text = "";
}
// Get the publisher security mode data.
//
index = drAppProp.GetOrdinal("PublisherSecurityMode");
if (!drAppProp.IsDBNull(index)) {
this.radioButtonSQLAuth.Checked = (1 == drAppProp.GetByte(index));
}
else {
this.radioButtonSQLAuth.Checked = true;
}
// Get the subscriber data.
//
index = drAppProp.GetOrdinal("Subscriber");
if (!drAppProp.IsDBNull(index)) {
this.textBoxSubscriber.Text = drAppProp.GetString(index);
}
else {
this.textBoxSubscriber.Text = "";
}
}
}
finally {
drAppProp.Close();
}
}
// This function saves the connecting information in the "ApplicationProperties" table.
//
private void SaveAppProps()
{
// Create the "ApplicationProperties" table.
//
SqlCeCommand cmd = dataNorthwind.NorthwindConnection.CreateCommand();
cmd.CommandText = @"CREATE TABLE ApplicationProperties (" +
"InternetURL NTEXT, " +
"InternetLogin NVARCHAR(255), " +
"InternetPassword NVARCHAR(255), " +
"Publisher NVARCHAR(255), " +
"PublisherLogin NVARCHAR(255), " +
"PublisherPassword NVARCHAR(255), " +
"PublisherSecurityMode TINYINT, " +
"Subscriber NVARCHAR(255));";
cmd.ExecuteNonQuery();
// Save the connection information
//
cmd.CommandText = @"INSERT INTO ApplicationProperties " +
"(InternetURL, InternetLogin, InternetPassword, Publisher, PublisherLogin, PublisherPassword, PublisherSecurityMode, Subscriber) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
cmd.Parameters.Add("@p1", this.textBoxInternetUrl.Text);
cmd.Parameters.Add("@p2", this.textBoxInternetLogin.Text);
cmd.Parameters.Add("@p3", this.textBoxInternetPwd.Text);
cmd.Parameters.Add("@p4", this.textBoxPublisher.Text);
cmd.Parameters.Add("@p5", this.textBoxUserID.Text);
cmd.Parameters.Add("@p6", this.textBoxPassword.Text);
cmd.Parameters.Add("@p7", this.radioButtonSQLAuth.Checked ? 1 : 0);
cmd.Parameters.Add("@p8", this.textBoxSubscriber.Text);
cmd.ExecuteNonQuery();
}
// This function synchronizes the device database with the server database.
//
private void ReplSync() {
SqlCeReplication replNorthwind = new SqlCeReplication();
// Set the Internet properties.
//
replNorthwind.InternetUrl = textBoxInternetUrl.Text;
replNorthwind.InternetLogin = textBoxInternetLogin.Text;
replNorthwind.InternetPassword = textBoxInternetPwd.Text;
// Set the Publisher properties.
//
replNorthwind.Publisher = textBoxPublisher.Text;
replNorthwind.PublisherDatabase = publisherDatabase;
replNorthwind.Publication = publication;
replNorthwind.PublisherSecurityMode = radioButtonSQLAuth.Checked ? SecurityType.DBAuthentication : SecurityType.NTAuthentication;
replNorthwind.PublisherLogin = textBoxUserID.Text;
replNorthwind.PublisherPassword = textBoxPassword.Text;
// Set the Subscriber properties.
//
replNorthwind.SubscriberConnectionString = dataNorthwind.LocalConnString;
replNorthwind.Subscriber = textBoxSubscriber.Text;
bool changed = false;
// Starts the cursor icon since this function may take some time.
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
try
{
if (ConnectionState.Open == dataNorthwind.NorthwindConnection.State) {
dataNorthwind.NorthwindConnection.Close();
}
if (init) {
// Delete the database if the database exists.
//
if (File.Exists(dataNorthwind.LocalDatabaseFile)) {
File.Delete(dataNorthwind.LocalDatabaseFile);
}
// Clear the dataset if it exists.
//
if (null != dataNorthwind.NorthwindDataSet) {
dataNorthwind.NorthwindDataSet.Clear();
}
// Create the Local SSCE Database subscription.
//
replNorthwind.AddSubscription(AddOption.CreateDatabase);
}
// Synchronize to the server database to populate the Subscription.
//
replNorthwind.Synchronize();
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
// Display the synchronization results.
//
MessageBox.Show("Synchronization Complete:\n" +
"Publisher changes = " + replNorthwind.PublisherChanges.ToString() + "\n" +
"Publisher conflicts = " + replNorthwind.PublisherConflicts.ToString() + "\n" +
"Subscriber changes = " + replNorthwind.SubscriberChanges.ToString(),
"Synchronize",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
changed = replNorthwind.PublisherChanges > 0 || replNorthwind.PublisherConflicts > 0;
}
catch(SqlCeException e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
NorthwindData.ShowErrors(e);
if (init)
{
this.formNorthwind.UpdateMenu();
return;
}
}
catch(Exception e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
MessageBox.Show(e.Message, "Northwind");
if (init) {
this.formNorthwind.UpdateMenu();
return;
}
}
finally {
// Dispose of the Replication Object
//
replNorthwind.Dispose();
}
try {
// Open the connection to the Northwind database.
//
dataNorthwind.NorthwindConnection.Open();
if (init) {
// Save the connection information
//
SaveAppProps();
// Set the initialization flag to false and disable the connection information controls.
//
init = false;
this.buttonSync.Text = "Synchronize";
this.panelProps.Enabled = false;
}
if (changed) {
formNorthwind.Refresh();
}
}
catch(SqlCeException e) {
NorthwindData.ShowErrors(e);
}
catch(Exception e) {
MessageBox.Show(@"Save connection information: " + e.Message, "Northwind");
}
}
private void radioButtonSQLAuth_CheckedChanged(object sender, System.EventArgs e) {
if (this.radioButtonSQLAuth.Checked) {
this.textBoxUserID.Enabled = true;
this.textBoxPassword.Enabled = true;
}
else {
this.textBoxUserID.Enabled = false;
this.textBoxPassword.Enabled = false;
}
}
private void buttonReset_Click(object sender, System.EventArgs e) {
if (DialogResult.OK == MessageBox.Show("You are about to reset all data. Continuing will discard all changes.",
"Northwind",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1)) {
if (ConnectionState.Open == dataNorthwind.NorthwindConnection.State) {
dataNorthwind.NorthwindConnection.Close();
}
try {
if (File.Exists(dataNorthwind.LocalDatabaseFile)) {
File.Delete(dataNorthwind.LocalDatabaseFile);
}
// Clear the dataset if it exists.
//
if (null != dataNorthwind.NorthwindDataSet) {
dataNorthwind.NorthwindDataSet.Clear();
}
}
catch(Exception err) {
MessageBox.Show(err.Message, "Northwind");
return;
}
init = true;
this.panelProps.Enabled = true;
buttonSync.Text = "Initialize";
formNorthwind.UpdateMenu();
}
}
private void buttonSync_Click(object sender, System.EventArgs e) {
if (DialogResult.OK == MessageBox.Show("You are about to synchronize your data. Continue?",
"Northwind",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1)) {
ReplSync();
formNorthwind.UpdateMenu();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -