使用网络提供的web服务开发航班查询程序

这个是一个基于winform的客户端程序,使用了网络提供的web服务,进行国内机票查询,本文主要讲解一下具体的客户端开发

主界面如下图:

 

关于如何引用一个web服务,请参见一些网络资料,比如:MSDN

下面,我为了使用多线程,并且向线程中传递参数,新建了一个类库thread.cs,具体代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif thread.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;


namespace WindowsService
{
    
class thread
    {
        
string startcity;//定义输出参数,出发城市
        string endcity;
        
string date;//出发时间
        DataSet ds;//返回的数据集
        
        
public string Scity
        {
            
get { return startcity; }
            
set { startcity = value; }
        }
        
public string Ecity
        {
            
get { return endcity; }
            
set { endcity = value; }
        }
        
public string Dt
        {
            
get { return date; }
            
set { date = value; }
        }
        
public DataSet Ds
        {
            
get { return ds; }
        }
        
public DataSet GetTime()
        {
            
try
            {
                airline.DomesticAirline airline 
= new WindowsService.airline.DomesticAirline();//实例化服务
                ds = airline.getDomesticAirlinesTime(Scity, Ecity, Dt, "");//输入参数,得到查询结果
                return ds;//返回结果
            }
            
catch (Exception e)
            {
                
throw new Exception(e.ToString());
            }
        }
    }
}

 

通过这个类库,就完成了向线程中传递参数Scity,Ecity等工作了。下面我们设置窗体代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace WindowsService
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
            UpdateGridview 
= new GetTimeAir(GetTimes);
        }
        
string date;//预先定义好参数
        string sc;
        
string ec;

        
delegate void GetTimeAir();//定义委托,实现多线程
        GetTimeAir UpdateGridview;
        
private void button1_Click(object sender, EventArgs e)
        {
            
try
            {
                Thread 
get = new Thread(GetTimes);
                
get.IsBackground = true;
                
get.Start();
                statusBar1.Text 
= "正在获取数据,请耐心等待"
            }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
finally
            {
                statusBar1.Text 
= "数据收集完成,耐心等待显示"
                
            }
        }
        
private void GetTimes()//主要的方法
        {
            
if (dataGridView1.InvokeRequired)
            {
                
this.Invoke(UpdateGridview);
            }
            
else
            {
                
try
                {
                    thread th 
= new thread();//实例化thread的类th
                    th.Scity = sc;
                    th.Ecity 
= ec;
                    th.Dt 
= date;
                    dataGridView1.DataSource 
= th.GetTime().Tables[0];
                }
                
catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                
finally
                {

                }
            }
        }

        
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            date 
= dateTimePicker1.Value.ToShortDateString();//获取起飞时间
        }
       
       

        
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            sc 
= comboBox1.SelectedItem.ToString();//获取出发城市
        }

        
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ec 
= comboBox2.SelectedItem.ToString();//获取抵达城市
        }
       
    }
}

 

至此,整个过程都好了,调试一下,由于网速,数据量大小问题,请耐心点哦。对了这里提供下载整个项目:点击下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值