C#数据库应用系统(机票预定系统)源代码
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Data.SqlClient; using System.Windows.Forms;
namespace TicketOrder {
public partial class Form1 : Form     {
public Form1()         {
InitializeComponent();         }
string sql = \"\";
string connStr =@\"server=sa\\SQLEXPRESS;database=Ticket;trusted_connection=true\";         SqlDataAdapter da;         DataSet ds;
private void Form1_Load(object sender, EventArgs e)         {
sql = \"select FlightNO as '航班号',LeaveCity as '出发城市',Destination as '目的城市',LeaveTime as '出发时间' from TicketInfo where 1=1\";             Fill();         }
///          /// 初始化函数         ///          private void Fill()         {
da = new SqlDataAdapter(sql, connStr);             ds = new DataSet();             try             {
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];             }
catch (Exception ex)             {
MessageBox.Show(\"输入信息有误,可能有不能识别的字符!\"+ex.Message);             }         }
private void button3_Click(object sender, EventArgs e)         {
bool isNew = true;             while (isNew)             {
if (txtChufa.Text != \"\")                 {
sql += string.Format(\" and LeaveCity like'%{0}%'\                    Fill();                 }
if (txtMudi.Text != \"\")                 {
sql += string.Format(\" and Destination like'%{0}%'\                    Fill();                 }
isNew = false;
sql = \"select FlightNO as '航班号',LeaveCity as '出发城市',Destination as '目的城市',LeaveTime as '出发时间' from TicketInfo where 1=1\";             }         }
private void button1_Click(object sender, EventArgs e)         {
sql = string.Format(\"insert into OrderInfo(FlightNO,SeatType,Number,LeaveDate) values('{0}','{1}','{2}','{3}')\            SqlConnection conn = new SqlConnection(connStr);             int num = 0;             try             {
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);                 num = cmd.ExecuteNonQuery();                 if (num != 0)
MessageBox.Show(\"预定成功!\");                 else
MessageBox.Show(\"预定失败!\");
}
catch (Exception ex)             {
MessageBox.Show(ex.Message);             }
finally             {
conn.Close();             }         }
private void button2_Click(object sender, EventArgs e)         {
Application.Exit();         }     } }