⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmr.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;
using System.Data.Common;

namespace Merge_Repl
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormMR : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button buttonAddCar;
		private System.Windows.Forms.TextBox textBoxLocation;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBoxReg;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button buttonMerge;
		private System.Windows.Forms.DataGrid dataGridCars;
		private System.Windows.Forms.MainMenu mainMenu1;
        private BindingSource carsBindingSource;
        private System.ComponentModel.IContainer components;

		private DataSet dsCars;
        private SqlCeDataAdapter daCars;
        private SqlCeConnection cnCars;

		public FormMR()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.buttonAddCar = new System.Windows.Forms.Button();
            this.textBoxLocation = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBoxReg = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.buttonMerge = new System.Windows.Forms.Button();
            this.carsBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.dataGridCars = new System.Windows.Forms.DataGrid();
            this.SuspendLayout();
            // 
            // buttonAddCar
            // 
            this.buttonAddCar.Location = new System.Drawing.Point(64, 184);
            this.buttonAddCar.Name = "buttonAddCar";
            this.buttonAddCar.Size = new System.Drawing.Size(64, 20);
            this.buttonAddCar.TabIndex = 2;
            this.buttonAddCar.Text = "Add";
            this.buttonAddCar.Click += new System.EventHandler(this.buttonAddCar_Click);
            // 
            // textBoxLocation
            // 
            this.textBoxLocation.Location = new System.Drawing.Point(64, 152);
            this.textBoxLocation.Name = "textBoxLocation";
            this.textBoxLocation.Size = new System.Drawing.Size(160, 21);
            this.textBoxLocation.TabIndex = 3;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 160);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 12);
            this.label2.Text = "Location:";
            // 
            // textBoxReg
            // 
            this.textBoxReg.Location = new System.Drawing.Point(64, 128);
            this.textBoxReg.Name = "textBoxReg";
            this.textBoxReg.Size = new System.Drawing.Size(64, 21);
            this.textBoxReg.TabIndex = 5;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 136);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(32, 16);
            this.label1.Text = "Reg:";
            // 
            // buttonMerge
            // 
            this.buttonMerge.Location = new System.Drawing.Point(64, 224);
            this.buttonMerge.Name = "buttonMerge";
            this.buttonMerge.Size = new System.Drawing.Size(88, 24);
            this.buttonMerge.TabIndex = 1;
            this.buttonMerge.Text = "Merge";
            this.buttonMerge.Click += new System.EventHandler(this.buttonMerge_Click);
            // 
            // dataGridCars
            // 
            this.dataGridCars.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
            this.dataGridCars.DataSource = this.carsBindingSource;
            this.dataGridCars.Location = new System.Drawing.Point(8, 8);
            this.dataGridCars.Name = "dataGridCars";
            this.dataGridCars.Size = new System.Drawing.Size(224, 112);
            this.dataGridCars.TabIndex = 0;
            // 
            // FormMR
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.dataGridCars);
            this.Controls.Add(this.buttonMerge);
            this.Controls.Add(this.buttonAddCar);
            this.Controls.Add(this.textBoxLocation);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBoxReg);
            this.Controls.Add(this.label1);
            this.Menu = this.mainMenu1;
            this.MinimizeBox = false;
            this.Name = "FormMR";
            this.Text = "Traffic";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.FormMR_Closing);
            this.Load += new System.EventHandler(this.FormMR_Load);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Application.Run(new FormMR());
		}

		private void FormMR_Load(object sender, System.EventArgs e)
		{

            // Configure the DataAdapter for use in UI binding
            cnCars = new SqlCeConnection(@"Data Source=\My Documents\TrafficMR.sdf");
            daCars = new SqlCeDataAdapter("SELECT CarId, Reg, Location FROM Cars", cnCars);

            // Configure the update comand
            new SqlCeCommandBuilder(daCars);

			Merge();
		}

        private void Merge()
        {
            using (SqlCeReplication rep = new SqlCeReplication(
                @"http://CL1MBING/TrafficMR/sqlcesa30.dll", //URL to Agent
                @"CL1MBING\ReplUser",  // InternetUser
                "P@ssw0rd",            // InternetPassword
                "CL1MBING",            // Publisher server
                "Traffic",             // Publisher Database
                "TrafficMR",           // Publication name
                "Testing",             // Subscriber name
                @"Data Source=\My Documents\TrafficMR.sdf" //Connection string to local database
                ))
            {
                try
                {
                    if (!System.IO.File.Exists(@"\My Documents\TrafficMR.sdf"))
                    {
                        rep.AddSubscription(AddOption.CreateDatabase);
                    }

                    rep.Synchronize();
                }
                catch (SqlCeException ex)
                {
                    DisplaySQLCEErrors(ex);
                }
            }

            SetupDataSetforUI();
        }

        private void SetupDataSetforUI()
        {
            // Set up the dataset which is the data source for the UI
            if (dsCars == null)
            {
                dsCars = new DataSet();
            }
            try
            {
                dsCars.Clear();
                daCars.Fill(dsCars, "Cars");
                dsCars.Tables["Cars"].Columns["CarID"].AutoIncrement = true;

                //Get current highest value in that column
                cnCars.Open();
                int MaxCarID = -1;
                using (SqlCeCommand cmd = new SqlCeCommand(
                    "SELECT MAX(CarID) FROM Cars", cnCars))
                {
                    object result = cmd.ExecuteScalar();
                    if (!(result is DBNull))
                        MaxCarID = Convert.ToInt32(result);
                }
                cnCars.Close();

                // Set the AutoIncrement seed accordingly
                this.dsCars.Tables["Cars"].Columns["CarID"].AutoIncrementSeed = MaxCarID + 1;

                carsBindingSource.DataSource = dsCars;
                carsBindingSource.DataMember = "Cars";
            }
            catch (SqlCeException ex)
            {
                DisplaySQLCEErrors(ex);
            }
        }

		private void DisplaySQLCEErrors(SqlCeException ex)
		{
			for (int i=0; i < ex.Errors.Count; i++)
			{
				MessageBox.Show("Index #" + i.ToString() + "\n" 
					+ ex.Errors[i].Source + "\n"
					+ "Error: " + ex.Errors[i].Message,		
					"Error No. " + ex.Errors[i].NativeError.ToString());
			}
		}

		private void buttonAddCar_Click(object sender, System.EventArgs e)
		{
            this.dsCars.Tables["Cars"].Rows.Add(new object[] { null, this.textBoxReg.Text, this.textBoxLocation.Text });

            try
            {
                // Call Update on the DataAdapter to write to database
                daCars.Update(dsCars, "Cars");
            }
            catch (SqlCeException ex)
            {
                DisplaySQLCEErrors(ex);
            }
        }

		private void buttonMerge_Click(object sender, System.EventArgs e)
		{
			Merge();
            MessageBox.Show("Merge complete");
		}

        private void FormMR_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            cnCars.Close();
            cnCars.Dispose();
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -