private void Button_modify_Click(object sender, RoutedEventArgs e) { if (DataView.SelectedItem == null) { return; } TrainClass itemInfo = DataView.SelectedItem as TrainClass; if (itemInfo == null || !(itemInfo is TrainClass)) { MessageBox.Show("获取选中项出现问题"); return; } TcpClient tcpClient = null; NetworkStream networkStream = null; try { tcpClient = new TcpClient(); tcpClient.Connect(ip_address, port); //建立与服务器的连接 networkStream = tcpClient.GetStream(); if (networkStream.CanWrite) { int lineid = 0; int seatcount = 0; if (!getStandard(out lineid, Text_lineid_Copy.Text) || !getStandard(out seatcount, Text_seatcount_Copy.Text)) { MessageBox.Show("查询信息填写错误"); return; } var package = new TTS_Core.TrainOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_MODIFY, itemInfo.trainid, lineid, Text_traintype_Copy.Text, seatcount); byte[] sendBytes = package.DataPackageToBytes(); //注册数据包转化为字节数组 networkStream.Write(sendBytes, 0, sendBytes.Length); var newClient = tcp_listener.AcceptTcpClient(); var bytes = ReadFromTcpClient(newClient); //获取数据 var package_rec = new TTS_Core.DataSetPackage(bytes); if (package_rec.forbid != 0 && package_rec.forbid != 1) { MessageBox.Show("出大问题"); } if (package_rec.forbid == 1) { MessageBox.Show("修改失败,请检查完整性约束或者是服务器故障"); } else { itemInfo.lineid = lineid; itemInfo.traintype = Text_traintype_Copy.Text; itemInfo.seatcount = seatcount; DataView.Items.Insert(DataView.SelectedIndex, itemInfo); DataView.Items.Remove(DataView.SelectedItem); DataView.Items.Refresh(); } } } catch { MessageBox.Show("无法连接到服务器!"); return; } finally { if (networkStream != null) { networkStream.Close(); } tcpClient.Close(); } }
private void Button_query_Click(object sender, RoutedEventArgs e) { DataView.Items.Clear(); TcpClient tcpClient = null; NetworkStream networkStream = null; try { tcpClient = new TcpClient(); tcpClient.Connect(ip_address, port); //建立与服务器的连接 networkStream = tcpClient.GetStream(); if (networkStream.CanWrite) { int trainid = 0; int lineid = 0; int seatcount = 0; if (!getStandard(out trainid, Text_trainid.Text) || !getStandard(out lineid, Text_lineid.Text) || !getStandard(out seatcount, Text_seatcount.Text)) { MessageBox.Show("查询信息填写错误"); return; } var package = new TTS_Core.TrainOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_QUERY, trainid, lineid, Text_traintype.Text, seatcount); byte[] sendBytes = package.DataPackageToBytes(); //注册数据包转化为字节数组 networkStream.Write(sendBytes, 0, sendBytes.Length); var newClient = tcp_listener.AcceptTcpClient(); var bytes = ReadFromTcpClient(newClient); //获取数据 var package_rec = new TTS_Core.DataSetPackage(bytes); if (package_rec.forbid != 0 && package_rec.forbid != 1) { MessageBox.Show("出大问题"); } if (package_rec.forbid == 1) { MessageBox.Show("查询失败,服务器或者网络出现故障"); } else { int row = package_rec.row; int col = package_rec.col; for (int i = 0; i < row; ++i) { DataView.Items.Add(new TrainClass { trainid = int.Parse(package_rec.dataSet.Tables[0].Rows[i][0].ToString().Trim()), lineid = int.Parse(package_rec.dataSet.Tables[0].Rows[i][1].ToString().Trim()), traintype = package_rec.dataSet.Tables[0].Rows[i][2].ToString().Trim(), seatcount = int.Parse(package_rec.dataSet.Tables[0].Rows[i][3].ToString().Trim()) }); } DataView.Items.Refresh(); MessageBox.Show("查询成功"); } } } catch { MessageBox.Show("无法连接到服务器或字段填写错误!"); return; } finally { if (networkStream != null) { networkStream.Close(); } tcpClient.Close(); } }
private void Button_delete_Click(object sender, RoutedEventArgs e) { if (DataView.SelectedItem == null) { return; } int selected_trainid; TrainClass itemInfo = DataView.SelectedItem as TrainClass; if (itemInfo != null && itemInfo is TrainClass) { selected_trainid = itemInfo.trainid; } else { MessageBox.Show("获取选中项出现问题"); return; } TcpClient tcpClient = null; NetworkStream networkStream = null; try { tcpClient = new TcpClient(); tcpClient.Connect(ip_address, port); //建立与服务器的连接 networkStream = tcpClient.GetStream(); if (networkStream.CanWrite) { var package = new TTS_Core.TrainOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_DELETE, selected_trainid, 0, "", 0); byte[] sendBytes = package.DataPackageToBytes(); networkStream.Write(sendBytes, 0, sendBytes.Length); var newClient = tcp_listener.AcceptTcpClient(); var bytes = ReadFromTcpClient(newClient); //获取数据 var package_rec = new TTS_Core.DataSetPackage(bytes); if (package_rec.forbid != 0 && package_rec.forbid != 1) { MessageBox.Show("出大问题"); } if (package_rec.forbid == 1) { MessageBox.Show("删除失败,请检查完整性约束或者是服务器故障"); } else { DataView.Items.Remove(DataView.SelectedItem); DataView.Items.Refresh(); } } } catch { MessageBox.Show("无法连接到服务器!"); return; } finally { if (networkStream != null) { networkStream.Close(); } tcpClient.Close(); } }