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

📄 form3.cs

📁 .net2003 C# 基于pda的winsock通讯
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient ;
namespace SmartDeviceApplication5
{
	/// <summary>
	/// Form3 的摘要说明。
	/// </summary>
	public class Form3 : System.Windows.Forms.Form
	{
		public System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
		private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
		public System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
		private System.Windows.Forms.DataGridTableStyle dfgdf1;
		private System.Windows.Forms.DataGridTableStyle jkjkj1;
		public System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.ColumnHeader sr;
		private System.Windows.Forms.ColumnHeader user;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Button button1;
	
		Client client;
		public Form3()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.dfgdf1 = new System.Windows.Forms.DataGridTableStyle();
			this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.jkjkj1 = new System.Windows.Forms.DataGridTableStyle();
			this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.listView1 = new System.Windows.Forms.ListView();
			this.sr = new System.Windows.Forms.ColumnHeader();
			this.user = new System.Windows.Forms.ColumnHeader();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.button4 = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(40, 176);
			this.button1.Size = new System.Drawing.Size(48, 20);
			this.button1.Text = "退出";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// dfgdf1
			// 
			this.dfgdf1.GridColumnStyles.Add(this.dataGridTextBoxColumn1);
			this.dfgdf1.GridColumnStyles.Add(this.dataGridTextBoxColumn3);
			this.dfgdf1.MappingName = "ggg";
			// 
			// dataGridTextBoxColumn1
			// 
			this.dataGridTextBoxColumn1.HeaderText = "ggg";
			this.dataGridTextBoxColumn1.MappingName = "jjj";
			this.dataGridTextBoxColumn1.NullText = "(null)";
			// 
			// dataGridTextBoxColumn3
			// 
			this.dataGridTextBoxColumn3.HeaderText = "hgj";
			this.dataGridTextBoxColumn3.MappingName = "hj";
			this.dataGridTextBoxColumn3.NullText = "(null)";
			// 
			// dataGridTextBoxColumn2
			// 
			this.dataGridTextBoxColumn2.HeaderText = "j";
			this.dataGridTextBoxColumn2.MappingName = "j";
			this.dataGridTextBoxColumn2.NullText = "(null)";
			// 
			// listView1
			// 
			this.listView1.CheckBoxes = true;
			this.listView1.Columns.Add(this.sr);
			this.listView1.Columns.Add(this.user);
			this.listView1.FullRowSelect = true;
			this.listView1.Location = new System.Drawing.Point(0, 16);
			this.listView1.Size = new System.Drawing.Size(240, 120);
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// sr
			// 
			this.sr.Text = "输入数据";
			this.sr.Width = 160;
			// 
			// user
			// 
			this.user.Text = "用户";
			this.user.Width = 60;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(40, 152);
			this.button2.Size = new System.Drawing.Size(48, 20);
			this.button2.Text = "push";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(104, 152);
			this.button3.Size = new System.Drawing.Size(48, 20);
			this.button3.Text = "pull";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(0, 128);
			this.textBox1.Size = new System.Drawing.Size(240, 21);
			this.textBox1.Text = "";
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.menuItem1);
			// 
			// menuItem1
			// 
			this.menuItem1.Text = "退出";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(104, 176);
			this.button4.Size = new System.Drawing.Size(48, 20);
			this.button4.Text = "刷新";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(8, 62);
			this.listBox1.Size = new System.Drawing.Size(224, 62);
			this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
			// 
			// Form3
			// 
			this.ClientSize = new System.Drawing.Size(240, 247);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.button1);
			this.Menu = this.mainMenu1;
			this.Text = "Form2";
			this.Load += new System.EventHandler(this.Form3_Load);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void Form3_Load(object sender, System.EventArgs e)
		{
			client = new  Client();
			client = pubics.clients ;

			client.MessageReceived += new SocketBase.MessageEventHandler(_server_MessageReceived);
			client.Connected += new SocketBase.ConnectionHandler(_server_Connected);
			try 
			{

				if (! File.Exists (@"\windows\程序\SmartD\yj.sdf") )
				{
//					File.Delete (@"\windows\程序\SmartD\yj.sdf");
					SqlCeEngine engine = new SqlCeEngine (@"Data Source = \windows\程序\SmartD\yj.sdf");
					engine.CreateDatabase ();
				}
				Bind_data();

			}
			catch (SqlCeException ee) 
			{
				Message.ShowErrors(ee);

			}

		}
		private void _server_Connected(object sender, SocketBase.ConnectionEventArgs e) 
		{

		}
		private void _server_MessageReceived(object sender, SocketBase.MessageEventArgs e) 
		{
			
			listBox1.Items.Add( e.Message.MessageBody);
		}
		string strreturn = null;
		private void Bind_data()
		{
 			
			string strSql = "select name  from buser";
			ListViewItem lisvi;
			SqlCeConnection conn;
			conn = new SqlCeConnection (@"Data Source = \windows\程序\SmartD\yj.sdf");
			conn.Open();
			SqlCeCommand cmd = conn.CreateCommand();
			cmd.CommandText = strSql;
			SqlCeDataReader rdr = cmd.ExecuteReader();
			listView1.Items.Clear();
			while (rdr.Read())
			{
                 strreturn = rdr[0].ToString();
				 lisvi =new ListViewItem(strreturn);
				 lisvi.Checked =true;
				 lisvi.SubItems.Add("pda");
                 listView1.Items.Add(lisvi);

			}
			conn.Close();
            
		}


		string SQL; 
		string InternetServer = "http://192.168.1.178/sqlce/sscesa20.dll";
		string InternetUser ="123456";
		string InternetPassword = "123456"; 
		string RemoteConnection = "Provider=sqloledb; Data Source =192.168.1.178; Initial Catalog =lj;User Id=sa;Password=";
		string LocalConnection = @"Provider=Microsoft.SQLSERVER.OLEDB.CE.2.0;Data Source=\windows\程序\SmartD\yj.sdf";
		string LocalTableName = "CH_CZCX";
		string RemoteTableName = "HMIS.CH_CZCX_FIELD"; 
		
		public void Pull() 
		{ //把表下载到本地数据库
			
            SqlCeRemoteDataAccess RDA = null; 
			RDA = new SqlCeRemoteDataAccess(InternetServer,InternetUser,InternetPassword, LocalConnection);

			try 
			{  
                
//				SqlCeCommand cmd;
//				//建立和初始化新的RDA对象 
//				SqlCeConnection conn1;
//				conn1 = new SqlCeConnection (@"Data Source = \windows\程序\SmartD\yj.sdf");
//				conn1.Open();
//				//在下载前,必须删除本地表 //打开本地数据库连接
//				SQL = "DROP TABLE " + LocalTableName;
//				cmd = new SqlCeCommand(SQL, conn1); 
//				//如果表不存在,会出现错误 
//				try { cmd.ExecuteNonQuery(); } 
//				catch{ } 
//				conn1.Close();
				//最后下载远程表
				SQL = "SELECT *  FROM " + RemoteTableName; 
				RDA.Pull(LocalTableName, SQL, RemoteConnection, RdaTrackOption.TrackingOnWithIndexes, "RDAErrors");
				MessageBox.Show("ok");
				
			}
			catch (SqlCeException ee) 
			{
				Message.ShowErrors(ee);

			}
			finally 
			{
				RDA.Dispose();
			}


		}
		public void Push() 
		{ //把表下载到本地数据库
			
			SqlCeRemoteDataAccess RDA = null; 
			RDA = new SqlCeRemoteDataAccess(InternetServer,InternetUser,InternetPassword, LocalConnection);
			try 
			{  
                		
				SqlCeCommand cmd;
				SqlCeConnection conn;
				conn = new SqlCeConnection (@"Data Source = \windows\程序\SmartD\yj.sdf");
				conn.Open();
				SQL = "insert into  " + LocalTableName + "(fgh) values( '"  + textBox1.Text + "' )";
				cmd = new SqlCeCommand(SQL, conn); 
				try { cmd.ExecuteNonQuery(); } 
				catch{ } 
				conn.Close();
				RDA.Push(LocalTableName, RemoteConnection,RdaBatchOption.BatchingOff);
				MessageBox.Show("ok");
				
			}
			catch (SqlCeException ee) 
			{
				Message.ShowErrors(ee);

			}
			finally 
			{
				RDA.Dispose();
			}


		}

		private void button2_Click(object sender, System.EventArgs e)
		{
		 Push() ;
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
		  Pull() ;
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
		this.Close();
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
		  Bind_data();
		}

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


	}
}

⌨️ 快捷键说明

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