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 stationid = -1; if (Text_stationid.Text != "" && !int.TryParse(Text_stationid.Text, out stationid)) { MessageBox.Show("lineid填写错误"); return; } var package = new TTS_Core.StationOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_QUERY, stationid, Text_stationname.Text); 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 StationClass { stationid = int.Parse(package_rec.dataSet.Tables[0].Rows[i][0].ToString().Trim()), stationname = package_rec.dataSet.Tables[0].Rows[i][1].ToString().Trim(), }); } DataView.Items.Refresh(); MessageBox.Show("查询成功"); } } } catch { MessageBox.Show("无法连接到服务器或字段填写错误!"); return; } finally { if (networkStream != null) { networkStream.Close(); } tcpClient.Close(); } }
private void Button_modify_Click(object sender, RoutedEventArgs e) { if (DataView.SelectedItem == null) { return; } StationClass itemInfo = DataView.SelectedItem as StationClass; if (itemInfo == null || !(itemInfo is StationClass)) { 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.StationOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_MODIFY, itemInfo.stationid, Text_stationname_Copy.Text); 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.stationname = Text_stationname_Copy.Text; 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(); } }