最重要的就是连接数据库,连接数据库全部封装在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.下面介绍查看页面的功能
点击刷新:
点击“导出excel”
要到出excel表格,必须添加如下类
public bool DataSetToExcel(DataSet dataSet, bool isShowExcle) { int rowNumber = dataSet.Tables[0].Rows.Count; int columnNumber = dataSet.Tables[0].Columns.Count; if (rowNumber == 0) { MessageBox.Show("没有任何数据可以导入到Excel文件!"); return false; } //建立Excel对象 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Application.Workbooks.Add(true); excel.Visible = isShowExcle;//是否打开该Excel文件 //填充数据 for (int c = 0; c < rowNumber; c++) { for (int j = 0; j < columnNumber; j++) { excel.Cells[c + 1, j + 1] = dataSet.Tables[0].Rows[c].ItemArray[j]; } } return true; }
选择显示的日期范围代码如下:
private void button3_Click(object sender, EventArgs e) { DataSetToExcel(SqlHelper.GetDataSet("table1", "SELECT *FROM table1 WHERE Intime >" + time1.Value.Date.ToString("yyyyMMdd00") + "and Intime<" + time2.Value.Date.ToString("yyyyMMdd00")), true); }
做好以后,需要美化(我用的是IrisSkin2,IrisSkin2是用于控制系统皮肤的一款第三方组件)
具体参考:http://www.csharpwin.com/csharpspace/9006r6418.shtml
http://bbs.csdn.net/topics/290074705
http://www.educity.cn/wenda/229146.html
具体步骤:
下载IrisSkin2 地址:http://c11.yunpan.360.cn/my/index/#%2F%E6%96%87%E6%A1%A3%2F
2. 在 项目名字上,点击右键,选添加引用,选COM,选:
Microsoft Excel 11.0 object Library
Microsoft Office 11.0 object Library
然后在.cs文件里添加如下引用
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Reflection;
3.方法:
1.右键“工具箱”。“添加选项卡”,取名“皮肤”。
2.右键“皮肤”,“选择项”弹出对话框
3.点击“浏览“,找到IrisSkin2.dll,,next,next,确定。
4.在皮肤里会出现。
5.把SkinEngine拖到设计界面,会出现在下面。
4.把皮肤文件中以ssk为后缀名的文件(如OneGreen.ssk)放在bin文件夹的debug文件夹里。
5.添加代码,双击界面,进入代码
private void Form1_Load(object sender, EventArgs e) { skinEngine1.SkinFile = "WaveColor2.ssk"; //此处写的相对路径 //skinEngine1.SkinFile = Application.StartupPath + @"\MP10.ssk"; }
完整代码:http://c11.yunpan.360.cn/my/index/#%2F%E6%96%87%E6%A1%A3%2FC%23%E5%81%9A%E7%9A%84%E4%B8%80%E4%B8%AA%E6%97%85%E9%A6%86%E5%AE%A2%E6%88%B7%E4%BD%8F%E5%AE%BF%E7%99%BB%E8%AE%B0%E8%A1%A8%2F
又重新做了一遍这个系统,发现有几个地方没有弄清楚:
第一:连接数据库步骤:打开Microsoft SQL Management Studio。将数据库连接到服务器,服务器名称为:localhost 用SQL Server身份验证 登录名:sa 密码:123456(根据自己的设置)。连接。
第二,SqlHelper.cs文件是需要根据自己的数据库做相应的调整的。我现在还不会。
第三,在添加客户信息时,点击添加信息按钮(button1_Click),执行了语句
string CommandText = "INSERT INTO table1 VALUES('" + id + "','" + textBox1.Text + "','" + textBox2.Text + "','" + Username.Text + "','" + Idcard.Text + "','" + Allcost.Text + "','" + Discount.Text + "','" + Cost.Text + "')";
只记得写插入语句,忘了执行
SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, CommandText, null);
第四,在Discount_TextChanged事件中(计算折扣事件),也要添加
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();
这段代码,这样当输入折扣时,实收价钱会自动计算,否则,输入折扣,实收不会总价减折扣。
第五,在查看历史页面中,数据显示在DataGridView控件中,设置该控件的任务,选择数据源,找到manmanDataSet数据源,双击下面的table1,就加入了数据源。
第六,在查看历史页面中,因为要查看指定日期的住宿记录,所以会比较输入的日期和选择的日期比较,选择的日期中没有几点,所以要用yyyyMMdd00来代替,即用00代替几点来比较。所以在旅客信息登记表中comboBox1和comboBox2中字符串集合编辑器中时间都以两位数来表示。比如00,01,02等。