最重要的就是连接数据库,连接数据库全部封装在sqlHelper类中,下面代码是创建表
public Form1() { InitializeComponent(); string connectionString = "Data Source=localhost;Initial Catalog=sanhui;Persist Security Info=True;User ID=sa;Password=sa"; //连接数据库为sanhui SqlHelper.connectionString = connectionString; string CommandText = "IF OBJECTPROPERTY ( object_id(‘table1‘),‘ISTABLE‘) = 1 print ‘table1已存在‘else CREATE TABLE table1([Id] CHAR(100) NOT NULL PRIMARY KEY, [Intime] CHAR(255) NOT NULL, [Outtime] CHAR(255) NOT NULL, [Name] CHAR(255) NOT NULL, [IDcard] CHAR(18) NOT NULL, [Allcost] CHAR(255) NOT NULL, [Discount] CHAR(255) NOT NULL, [Cost] CHAR(255) NOT NULL)"; //验证表table1是否存在,没存在则创建 SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, CommandText, null); }
上面是四个选项卡,目前只有“旅客信息登记表”和“查看历史记录”做了初步的功能。下面详细介绍这两个界面的代码
1.首先是插入dateTimePicker空间,可以选择旅客入住时间,**年**月**日,有时候还需要精确到几点,所以旁边又添加ComboBox下拉列表,可以选择几点。入住时间用TextBox空间显示,为避免错误,必须选择输入,而不能手动输入日期,所以将属性ReadOnly改为true。要想使TextBox中显示**年**月**日 **点,需在Form.cs添加如下代码:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
Intime.Text = dateTimePicker1.Value.Date.ToString("yyyy/MM/dd") + " " + comboBox1.SelectedItem.ToString() + "点";
}
离开时间的功能同入住时间的功能设计一样。
右边那些属性都是用TextBox来显示。身份证必须保证18位。在数据库中设置。
string CommandText = "IF OBJECTPROPERTY ( object_id(‘table1‘),‘ISTABLE‘) = 1 print ‘table1已存在‘else CREATE TABLE table1([Id] CHAR(100) NOT NULL PRIMARY KEY, [Intime] CHAR(255) NOT NULL, [Outtime] CHAR(255) NOT NULL, [Name] CHAR(255) NOT NULL,[IDcard] CHAR(18) NOT NULL, [Allcost] CHAR(255) NOT NULL, [Discount] CHAR(255) NOT NULL, [Cost] CHAR(255) NOT NULL)";
由于所有信息都是必填的,点击“添加信息”,有如下代码:
private void button1_Click_1(object sender, EventArgs e) { try { if (Intime.Text.Trim() != "" && Outtime.Text.Trim() != "" && Username.Text.Trim() != "" && Idcard.Text.Trim() != "" && Allcost.Text.Trim() != "" && Discount.Text.Trim() != "" && Cost.Text.Trim() != "" && Idcard.Text.Trim().Length==18) { string id = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff"); string CommandText = "INSERT INTO table1 VALUES(‘" + id + "‘,‘" + Intime.Text + "‘,‘" + Outtime.Text + "‘,‘" + Username.Text + "‘,‘" + Idcard.Text + "‘,‘" + Allcost.Text + "‘,‘" + Discount.Text + "‘,‘" + Cost.Text + "‘)"; //在表1中插入这些信息 SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, CommandText, null); MessageBox.Show("添加成功!"); double a = Convert.ToDouble(Cost.Text.Trim()); MessageBox.Show(a.ToString()); } else MessageBox.Show("信息不完整"); } catch (Exception e1) { MessageBox.Show("添加失败!"+e1.ToString()); //将错误信息显示出来 } }
清空就更容易了
private void button2_Click(object sender, EventArgs e) { Intime.Text = ""; Outtime.Text = ""; Username.Text = ""; Idcard.Text = ""; Allcost.Text = ""; Discount.Text = ""; Cost.Text = ""; }
由于实收=应收金额-折扣。这里自动计算实收金额
private void Allcost_TextChanged(object sender, EventArgs e) //应收金额中 { if (Discount.Text.Trim() == "") Discount.Text = "0"; //默认为0 if (Allcost.Text.Trim() == "") Allcost.Text = "0"; Cost.Text = (Convert.ToDouble(Allcost.Text.Trim()) - Convert.ToDouble(Discount.Text.Trim())).ToString(); } private void Discount_TextChanged(object sender, EventArgs e) { if (Discount.Text.Trim() == "") Discount.Text = "0"; if (Allcost.Text.Trim() == "") Allcost.Text = "0"; Cost.Text = (Convert.ToDouble(Allcost.Text.Trim()) -Convert.ToDouble( Discount.Text.Trim())).ToString(); }
2.下面介绍查看页面的功能
这个功能还没弄成功,暂时不会,以后再写
原文:http://my.oschina.net/zhangxuman/blog/353962