private void button7_Click(object sender, EventArgs e) { button4.Enabled = true; button5.Enabled = true; textBox2.Text = ""; textBox4.Text = ""; textBox5.Text = ""; comboBox1.Text = ""; richTextBox1.Text = ""; MyMeans.SQLstr = "select*from shoulinglb"; DataSet ds = MyMeans.GetDataSet(MyMeans.SQLstr); dataGridView4.DataSource = ds.Tables[0]; for (int i = 0; i < dataGridView4.Rows.Count; i++) { dataGridView4.Rows[i].Cells[0].Value = "0"; } DataSet dss; dss = MyMeans.GetDataSet("Select*from fasongml"); dataGridView2.DataSource = dss.Tables[0]; MyMeans.Close(); dss = MyMeans.GetDataSet("Select*from yifaml"); dataGridView3.DataSource = dss.Tables[0]; }
private void Form2_Load(object sender, EventArgs e) { //设置为允许跨线程调用空间 Control.CheckForIllegalCrossThreadCalls = false; DataSet ds; ds = MyMeans.GetDataSet("Select*from daifaml"); dataGridView1.DataSource = ds.Tables[0]; MyMeans.Close(); ds = MyMeans.GetDataSet("Select*from fasongml"); dataGridView2.DataSource = ds.Tables[0]; MyMeans.Close(); ds = MyMeans.GetDataSet("Select*from yifaml"); dataGridView3.DataSource = ds.Tables[0]; MyMeans.Close(); ds = MyMeans.GetDataSet("Select*from shoulinglb"); dataGridView4.DataSource = ds.Tables[0]; MyMeans.Close(); MyMeans.linq = new linqtosqlClassDataContext(MyMeans.Constr); var result = from items in MyMeans.linq.mlmoban select new { Number = items.模板编号, LeiXing = items.命令类型, NeiRong = items.命令内容, }; foreach (var item in result) { comboBox1.Items.Add(item.LeiXing); } SqlDataReader Sdr; MyMeans.SQLstr = "select*from changyongch"; Sdr = MyMeans.GgtDataReader(MyMeans.SQLstr); while (Sdr.Read()) { listBox1.Items.Add(Sdr[0]); } try //连接服务器,若连接成功,通信状态显示为“通信正常”; { socket.Connect(IPP); string SendStr = "行调系统"; byte[] Bs = Encoding.UTF8.GetBytes(SendStr); socket.Send(Bs, Bs.Length, 0); label7.Text = "通信正常"; jieshouMsg = new Thread(new ThreadStart(RecMsg)); jieshouMsg.IsBackground = true; jieshouMsg.Start(); } catch { } textBox4.Text = Form1.Uers_Now; }
private void 达ToolStripMenuItem_Click(object sender, EventArgs e) { byte[] sendByts = new byte[1024]; //发送调度命令给服务器 string sendStr = dataGridView1.SelectedCells[0].Value.ToString().Trim() + "#" + dataGridView1.SelectedCells[1].Value.ToString().Trim() + "#" + dataGridView1.SelectedCells[3].Value.ToString().Trim() + "#" + dateTimePicker1.Text.Trim() + "#" + dataGridView1.SelectedCells[5].Value.ToString().Trim(); sendByts = Encoding.UTF8.GetBytes(sendStr); socket.Send(sendByts, sendByts.Length, 0); //发送受令车站信息给服务器; string SC_ml = dataGridView1.SelectedCells[0].Value.ToString().Trim(); MyMeans.SQLstr = "select*from 受令车站对应表 where 命令编号=" + int.Parse(SC_ml); SqlDataReader read = MyMeans.GgtDataReader(MyMeans.SQLstr); string SLchezhan = null; while (read.Read()) { SLchezhan += (read[1].ToString().Trim() + "#"); } sendByts = Encoding.UTF8.GetBytes(SLchezhan); socket.Send(sendByts, sendByts.Length, 0); MessageBox.Show("命令发送成功"); DataSet ds; MyMeans.SQLstr = "Insert into fasongml(命令编号,命令类型,调度员姓名,发令单位,发令时间,命令内容,当前状态)values(" + int.Parse(SC_ml) + ",'" + dataGridView1.SelectedCells[1].Value.ToString().Trim() + "','" + dataGridView1.SelectedCells[2].Value.ToString().Trim() + "','" + dataGridView1.SelectedCells[3].Value.ToString().Trim() + "','" + dateTimePicker1.Text.Trim() + "','" + dataGridView1.SelectedCells[5].Value.ToString().Trim() + "','已发送')";// 注意圆括号中的 , 用英文形式 MyMeans.ExecuteSql(MyMeans.SQLstr); ds = MyMeans.GetDataSet("Select*from fasongml"); dataGridView2.DataSource = ds.Tables[0]; MyMeans.Close(); MyMeans.SQLstr = "Delete from daifaml where 命令编号=" + int.Parse(SC_ml); MyMeans.ExecuteSql(MyMeans.SQLstr); MyMeans.SQLstr = "Select*from daifaml"; ds = MyMeans.GetDataSet(MyMeans.SQLstr); dataGridView1.DataSource = ds.Tables[0]; }
//接受车站发来的签收状态 public void RecMsg() { int i = 0; //0表示某条调度命令所有受令车站均已签收,1表示存在受令车站没有签收; while (true) { byte[] Msg = new byte[1024]; int Bytes = 1; string receiveMsg = null; char[] fenge = { '#' }; string[] msg; try { Bytes = socket.Receive(Msg, Msg.Length, 0); if (Bytes == 0) { label7.Text = "通信断开"; socket.Close(); //在调用Abort()方法的线程上引发ThreadAbortException异常,以开始终止线程。 //即调用此方法会自动引发一个异常,程序便运行到catch语句块 Thread.CurrentThread.Abort(); } } catch (Exception ex) { //GetType()获取当前实例的运行类型 //typeof()活动类型声明的泛型类型 if (ex.GetType() == typeof(ThreadAbortException)) //如果捕捉到的异常为ThreadAbortException异常,则什么都不做 { } else { label7.Text = "通信断开"; socket.Close(); Thread.CurrentThread.Abort(); } } receiveMsg = Encoding.UTF8.GetString(Msg, 0, Bytes); msg = receiveMsg.Split(fenge); if (msg[0] == "断开") //如果服务器发来的是断开通信连接的信息,则终止线程; { label7.Text = "通信断开"; socket.Close(); Thread.CurrentThread.Abort(); //获取当前正在运行的线程,并将其终止; } else { //根据命令编号和受令单位更新 调度命令的相应车站的签收状态 MyMeans.SQLstr = "Update 受令车站对应表 Set 签收状态='签收',签收人='" + msg[2] + "',签收时间='" + msg[3] + "',签收结果='" + msg[4] + "'where 命令编号=" + int.Parse(msg[0]) + "and 受令单位='" + msg[1] + "'"; MyMeans.ExecuteSql(MyMeans.SQLstr); MyMeans.SQLstr = "select*from 受令车站对应表 where 命令编号=" + int.Parse(msg[0]); SqlDataReader Sdr = MyMeans.GgtDataReader(MyMeans.SQLstr); while (Sdr.Read()) { //如果有受令车站没有签收,将i置为1; if (Sdr[2].ToString().Trim() == "") { i = 1; } } if (i == 0) //i=0表示所有车站均已签收; { MyMeans.SQLstr = "select*from fasongml where 命令编号=" + int.Parse(msg[0]); SqlDataReader read = MyMeans.GgtDataReader(MyMeans.SQLstr); read.Read(); MyMeans.SQLstr = "Insert into yifaml(命令编号,命令类型,调度员姓名,发令单位,发令时间,命令内容,签收状态)values(" + int.Parse(read[0].ToString().Trim()) + ",'" + read[1].ToString().Trim() + "','" + read[2].ToString().Trim() + "','" + read[3].ToString().Trim() + "','" + read[4].ToString().Trim() + "','" + read[5].ToString().Trim() + "','全部签收')"; MyMeans.ExecuteSql(MyMeans.SQLstr); MyMeans.SQLstr = "Delete from fasongml where 命令编号=" + int.Parse(msg[0]); MyMeans.ExecuteSql(MyMeans.SQLstr); } DataSet ds; ds = MyMeans.GetDataSet("Select*from fasongml"); dataGridView2.DataSource = ds.Tables[0]; MyMeans.Close(); ds = MyMeans.GetDataSet("Select*from yifaml"); dataGridView3.DataSource = ds.Tables[0]; MyMeans.Close(); } } }