private void button3_Click(object sender, EventArgs e) { try { if (dataGridView1.SelectedCells[5].Value.ToString().Trim() == "") { string sleNumber = dataGridView1.SelectedCells[0].Value.ToString().Trim(); //发送签收命令; byte[] Bs = new byte[1024]; Bs = Encoding.UTF8.GetBytes(sleNumber + "#南京南站#" + textBox3.Text + "#" + dateTimePicker2.Text + "#拒绝"); socket.Send(Bs, Bs.Length, 0); Bs = Encoding.UTF8.GetBytes("行调系统"); socket.Send(Bs, Bs.Length, 0); //将签收信息更新到数据库中; MyMeans.SQLstr = "Update qianshouml set 是否签收='已签收',签收人='" + textBox3.Text + "',签收时间='" + dateTimePicker2.Text + "',签收结果='拒绝' where 命令编号=" + int.Parse(sleNumber); //每个“列=值”对之间用逗号隔开 MyMeans.ExecuteSql(MyMeans.SQLstr); //在签收之后更新列表的内容; DataSet dataset = MyMeans.GetDataSet("Select*from qianshouml"); table = dataset.Tables[0]; //检查是否所有调度命令都有签收 MyMeans.SQLstr = "select*from qianshouml "; SqlDataReader Sdr = MyMeans.GgtDataReader(MyMeans.SQLstr); int i = 0; while (Sdr.Read()) { //如果有调度命令没有签收,将i置为1; string a = Sdr[6].ToString().Trim(); if (a == "") { i = 1; } } if (i == 0) //i=0表示所有调度命令均已签收; { button4.BackColor = Color.White; } } else { MessageBox.Show("该条命令已签收"); } } catch { MessageBox.Show("签收失败"); } }
private void Form2_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; try { socket.Connect(IPP); //将车务终端名称发送给服务器; string SendStr = "南京南站"; byte[] Bs = Encoding.UTF8.GetBytes(SendStr); socket.Send(Bs, Bs.Length, 0); label7.Text = "通信正常"; //新建接收命令的线程 RecMsg = new Thread(new ParameterizedThreadStart(ReciveMsg)); RecMsg.IsBackground = true; //将线程设置为后台线程,此时程序控制权归主线程; RecMsg.Start(socket); } catch { } try { //加载调度命令 DataSet dataset = MyMeans.GetDataSet("Select*from qianshouml"); table = dataset.Tables[0]; //检查是否所有调度命令都有签收 MyMeans.SQLstr = "select*from qianshouml "; SqlDataReader Sdr = MyMeans.GgtDataReader(MyMeans.SQLstr); int i = 0; while (Sdr.Read()) { //如果有调度命令没有签收,将i置为1; string a = Sdr[6].ToString().Trim(); if (a == "") { i = 1; } } if (i == 1) //i=0表示所有调度命令均已签收,则将指示按钮颜色设置为白色; { button4.BackColor = Color.Red; } } catch { } }
private void button4_Click(object sender, EventArgs e) { //检查是否所有调度命令都有签收 MyMeans.SQLstr = "select*from qianshouml "; SqlDataReader Sdr = MyMeans.GgtDataReader(MyMeans.SQLstr); int i = 0; while (Sdr.Read()) { //如果有调度命令没有签收,将i置为1; string a = Sdr[6].ToString().Trim(); if (a == "") { i = 1; } } if (i == 0) //i=0表示所有调度命令均已签收,则将指示按钮颜色设置为白色; { button4.BackColor = Color.White; } }
//接受调度命令的方法; public void ReciveMsg(object ClintSocket) { Socket sock = ClintSocket as Socket; while (true) { //接受行调系统发来的调度命令; string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes = 1; char[] cha = { '#' }; try { bytes = sock.Receive(recvBytes, recvBytes.Length, 0); if (bytes == 0) { label7.Text = "通信断开"; sock.Close(); Thread.CurrentThread.Abort(); } } catch (Exception ex) { if (ex.GetType() == typeof(ThreadAbortException)) { } else { label7.Text = "通信断开"; sock.Close(); Thread.CurrentThread.Abort(); } } recvStr = Encoding.UTF8.GetString(recvBytes, 0, bytes); string[] RecMsg = recvStr.Split(cha); //将新接受到的命令加入到数据库 if (RecMsg[0] == "断开") { sock.Close(); label7.Text = "通信断开"; Thread.CurrentThread.Abort(); } else { MyMeans.SQLstr = "Insert into qianshouml(命令编号,命令类型,发令单位,发令时间,命令内容)values(" + int.Parse(RecMsg[0]) + ",'" + RecMsg[1] + "','" + RecMsg[2] + "','" + RecMsg[3] + "','" + RecMsg[4] + "')";// 注意圆括号中的 , 用英文形式 MyMeans.My_conn = new SqlConnection(MyMeans.Constr); MyMeans.My_conn.Open(); SqlCommand Com = new SqlCommand(); Com.Connection = MyMeans.My_conn; Com.CommandText = MyMeans.SQLstr; Com.CommandType = CommandType.Text; Com.ExecuteNonQuery(); MyMeans.My_conn.Dispose(); //将接受消息后的数据表重新查询并绑定到列表上 DataSet dataset = MyMeans.GetDataSet("Select*from qianshouml"); table = dataset.Tables[0]; button4.BackColor = Color.Red; } } }