📄 line information.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 民航订票管理系统.Company_Work
{
public partial class Line_information : Form
{
#region 用户自定义变量
/// <summary>
/// 自定义变量
/// </summary>
private string strconn = "";
private SqlCommand command;
private SqlConnection conn;
private DataSet ds = new DataSet();//数据集
private SqlDataAdapter SqlDA;
private CurrencyManager cmOrders;//数据导航
private string temp = "";
#endregion
public Line_information()
{
InitializeComponent();
}
/// <summary>
/// 重载构造函数
/// </summary>
/// <param name="stringConnection"></param>
public Line_information(string stringConnection)
{
InitializeComponent();
strconn = stringConnection;
}
/// <summary>
/// 数据绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Line_information_Load_1(object sender, EventArgs e)
{
conn = new SqlConnection(strconn);
string mySqlstring = "select * from Line";
command = new SqlCommand(mySqlstring, conn);
command.CommandType = CommandType.Text;
SqlDA = new SqlDataAdapter();
SqlDA.SelectCommand = command;
this.ds.Clear();
SqlDA.Fill(ds, "Line");
this.dataGrid1.SetDataBinding(ds, "Line");
this.textBox3.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.LineID"));
this.textBox4.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.BPosition"));
this.textBox5.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.EPosition"));
this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.CompID"));
cmOrders = (CurrencyManager)BindingContext[ds, "Line"];
SqlConnection cn = new SqlConnection(this.strconn);
cn.Open();
SqlCommand cmd = cn.CreateCommand();
cmd.CommandText = "select distinct CompID from AirComp";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr.GetValue(0).ToString().Trim());
}
conn.Close();
}
#region 工具栏控件
/// <summary>
/// 工具栏控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FirstRecord_toolStripButton_Click(object sender, EventArgs e)
{
this.dataGrid1.UnSelect(cmOrders.Position); //取消原选中的行
cmOrders.Position = 0;
this.dataGrid1.Select(cmOrders.Position); //选中当前行
this.dataGrid1.CurrentRowIndex = cmOrders.Position; //移动表头指示图标
return;
}
private void AboveRecord_toolStripButton_Click(object sender, EventArgs e)
{
if (cmOrders.Position >= 0)
{
this.dataGrid1.UnSelect(cmOrders.Position);
cmOrders.Position--;
this.dataGrid1.Select(cmOrders.Position);
this.dataGrid1.CurrentRowIndex = cmOrders.Position;
}
return;
}
private void NextRecord_toolStripButton_Click(object sender, EventArgs e)
{
if (cmOrders.Position <= cmOrders.Count - 1)
{
this.dataGrid1.UnSelect(cmOrders.Position);
cmOrders.Position++;
this.dataGrid1.Select(cmOrders.Position);
this.dataGrid1.CurrentRowIndex = cmOrders.Position;
}
return;
}
private void LastRecord_toolStripButton_Click(object sender, EventArgs e)
{
this.dataGrid1.UnSelect(cmOrders.Position);
cmOrders.Position = cmOrders.Count - 1;
this.dataGrid1.Select(cmOrders.Position);
this.dataGrid1.CurrentRowIndex = cmOrders.Position;
return;
}
private void NewRecord_toolStripButton_Click(object sender, EventArgs e)
{
//设置默认值
temp = "新增记录";
SetDefaultValue();
SetModifyMode(true);
}
private void Xiugai_toolStripButton_Click(object sender, EventArgs e)
{
this.textBox3.DataBindings.Clear();
this.textBox4.DataBindings.Clear();
this.textBox5.DataBindings.Clear();
this.comboBox1.DataBindings.Clear();
this.dataGrid1.SetDataBinding(ds, "Line");
this.textBox3.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.LineID"));
this.textBox4.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.BPosition"));
this.textBox5.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.EPosition"));
this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", ds, "Line.CompID"));
SetModifyMode(true);
}
private void Delete_toolStripButton_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确认删除?", "删除数据", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
if (cmOrders.Count > 0)//立即从数据集中删除
{
cmOrders.RemoveAt(cmOrders.Position);
SqlCommandBuilder cd = new SqlCommandBuilder(SqlDA);
SqlDA.Update(this.ds, "Line");
}
else
MessageBox.Show("表中为空,已无可删除数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void Tijiao_toolStripButton_Click(object sender, EventArgs e)
{
if (textBox3.Text.Trim() == "")//检查非空字段
{
MessageBox.Show("类型名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (temp == "新增记录")
{
temp = "insert into Line(LineID,BPosition,EPosition,CompID) VALUES ('" + this.textBox3.Text.Trim() + "','" + this.textBox4.Text.Trim() + "','" + this.textBox5.Text.Trim() + "','" + this.comboBox1.Text.Trim() + "')";
//temp="INSERT INTO PLANE(PlaneID,type,SeatsNum,CompID) values('ef12d','null','null','A05')";
SqlCommand mysqlcommand = new SqlCommand(temp, conn);
mysqlcommand.CommandType = CommandType.Text;
conn.Open();
mysqlcommand.ExecuteNonQuery();
conn.Close();
this.dataGrid1.SetDataBinding(null, null);
this.dataGrid1.Update();
SqlDA = new SqlDataAdapter();
SqlDA.SelectCommand = command;
this.ds.Clear();
SqlDA.Fill(ds, "Line");
this.dataGrid1.SetDataBinding(this.ds, "Line");
temp = "";
}
cmOrders.EndCurrentEdit();
if (ds.GetChanges() != null)
{
try
{
SqlCommandBuilder cd = new SqlCommandBuilder(SqlDA);
SqlDA.Update(ds, "Line");
SetModifyMode(false);
}
catch (Exception express)
{
MessageBox.Show(express.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
ds.RejectChanges();
}
}
return;
}
private void cancel_toolStripButton_Click(object sender, EventArgs e)
{
try
{
cmOrders.CancelCurrentEdit(); //取消编辑
SetModifyMode(false);
}
catch (Exception express)
{
MessageBox.Show(express.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
private void Close_toolStripButton_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
///设置默认值
/// </summary>
private void SetDefaultValue()
{
this.textBox3.DataBindings.Clear();
this.textBox4.DataBindings.Clear();
this.textBox5.DataBindings.Clear();
this.comboBox1.DataBindings.Clear();
this.textBox3.Text = "P";
this.textBox4.Text = "NULL";
this.textBox5.Text = "NULL";
comboBox1.SelectedIndex = 0;//选择默认类型
}
/// <summary>
/// 控件只读属性设置
/// </summary>
/// <param name="blnEdit">bool型值改变控件的只读属性</param>
private void SetModifyMode(bool blnEdit)
{
//设置文本框属性
this.textBox3.ReadOnly = !blnEdit;
this.textBox4.ReadOnly = !blnEdit;
this.textBox5.ReadOnly = !blnEdit;
//this.comboBox1.ReadOnly = !blnEdit;
//设置搜索按钮属性
Sousuo_button.Enabled = !blnEdit;
}
#endregion
#region 航线信息查询
/// <summary>
/// 按起点城市与终点城市查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Sousuo_button_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim() != "")
{
if (this.textBox2.Text.Trim() != "")
{
string mySqlstring = "select * from Line where BPosition like '%" + this.textBox1.Text.Trim() + "%' and EPosition like '%" + this.textBox2.Text.Trim() + "%'";
command = new SqlCommand(mySqlstring, conn);
command.CommandType = CommandType.Text;
SqlDA = new SqlDataAdapter();
SqlDA.SelectCommand = command;
this.ds.Clear();
SqlDA.Fill(ds, "Line");
this.dataGrid1.SetDataBinding(ds, "Line");
cmOrders = (CurrencyManager)BindingContext[ds, "Line"];
}
else
{
string mySqlstring = "select * from Line where BPosition like '%" + this.textBox1.Text.Trim() + "%'";
command = new SqlCommand(mySqlstring, conn);
command.CommandType = CommandType.Text;
SqlDA = new SqlDataAdapter();
SqlDA.SelectCommand = command;
this.ds.Clear();
SqlDA.Fill(ds, "Line");
this.dataGrid1.SetDataBinding(ds, "Line");
cmOrders = (CurrencyManager)BindingContext[ds, "Line"];
}
}
else
{
if (this.textBox2.Text.Trim() != "")
{
string mySqlstring = "select * from Line where EPosition like '%" + this.textBox2.Text.Trim() + "%'";
command = new SqlCommand(mySqlstring, conn);
command.CommandType = CommandType.Text;
SqlDA = new SqlDataAdapter();
SqlDA.SelectCommand = command;
this.ds.Clear();
SqlDA.Fill(ds, "Line");
this.dataGrid1.SetDataBinding(ds, "Line");
cmOrders = (CurrencyManager)BindingContext[ds, "Line"];
}
else
{
string mySqlstring = "select * from Line";
command = new SqlCommand(mySqlstring, conn);
command.CommandType = CommandType.Text;
SqlDA = new SqlDataAdapter();
SqlDA.SelectCommand = command;
this.ds.Clear();
SqlDA.Fill(ds, "Line");
this.dataGrid1.SetDataBinding(ds, "Line");
cmOrders = (CurrencyManager)BindingContext[ds, "Line"];
}
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -