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 enterstationid = 0; int leavestationid = 0; int lineid = 0; float ticketprice = 0; if (!getStandard(out enterstationid, Text_enterstationid.Text) || !getStandard(out leavestationid, Text_leavestationid.Text) || !getStandard(out lineid, Text_lineid.Text) || !getStandard(out ticketprice, Text_ticketprice.Text)) { MessageBox.Show("查询信息填写错误"); return; } var package = new TTS_Core.TicketPriceOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_QUERY, enterstationid, leavestationid, lineid, ticketprice); 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 TicketPriceClass { enterstationid = int.Parse(package_rec.dataSet.Tables[0].Rows[i][0].ToString().Trim()), leavestationid = int.Parse(package_rec.dataSet.Tables[0].Rows[i][1].ToString().Trim()), lineid = int.Parse(package_rec.dataSet.Tables[0].Rows[i][2].ToString().Trim()), ticketprice = float.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_modify_Click(object sender, RoutedEventArgs e) { if (DataView.SelectedItem == null) { return; } int selected_enterstationid = 0; int selected_leavestationid = 0; int selected_lineid = 0; TicketPriceClass itemInfo = DataView.SelectedItem as TicketPriceClass; if (itemInfo != null && itemInfo is TicketPriceClass) { selected_enterstationid = itemInfo.enterstationid; selected_leavestationid = itemInfo.leavestationid; selected_lineid = itemInfo.lineid; } 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) { float ticketprice = 0; if (!getStandard(out ticketprice, Text_ticketprice_Copy.Text)) { MessageBox.Show("查询信息填写错误"); return; } var package = new TTS_Core.TicketPriceOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_MODIFY, selected_enterstationid, selected_leavestationid, selected_lineid, ticketprice); 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.ticketprice = ticketprice; 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(); } }