首页 > 代码库 > VB.NET版机房收费系统---报表
VB.NET版机房收费系统---报表
报表,即报告情况的表格,简单的说:报表就是用表格、图表等格式来动态显示数据,可以用公式表示为:“报表 = 多样的格式 + 动态的数据”。 在没有计算机以前,人们利用纸和笔来记录数据。
比如:民间常常说的豆腐帐,就是卖豆腐的每天将自己的卖出的豆腐记在一个本子上,然后每月都要汇总算算,这种情况下,报表数据和报表格式是紧密结合在一起的,都在同一个本子上。数据也只能有一种几乎只有记帐的人才能理解的表现形式,且这种形式难于修改。
VB版机房收费系统的报表采用的是第三方的Grid++Report,.NET版机房收费系统,采用了自带的报表设计器,实现过程如下:第一部分:添加数据集
a、右击添加---新建项目;
b、选择窗体应用程序and重命名;
c、界面布局如下,其中ReportViewer自带具有刷新、打印、打印布局等功能,所以就不用再独自拿出来当一个小功能实现了。ps,这里添加一个DataGridView控件,设置为不可见,她有什么神奇的作用nie,`(*∩_∩*)′ ,我们可以把当天收取金额,退还金额,消费金额显示在这个控件上,比方拿消费金额来说,她的计算结果就是该控件所有的行,加上line表中第六列即consumeMoney相加,是不是很方便。
d、右击添加---新建项目;
e、Reoprting---报表;
第二部分:连接数据库
a、选择数据源
b、选择数据库类型
c、选择数据库模型
d、选择数据库连接;
e、将连接字符串保存到应用程序配置文件中;
f、添加连接
g、选择数据库对象
第三部分,设计报表;
a、添加表头
b、选择报表
第四部分:代码实现部分
首先,实体层;
<span style="font-size:18px;">Public Class CheckDayinfo Public rechargeCash As Integer '字段 Public Property _rechargeCash As Integer '属性 Get Return rechargeCash End Get Set(value As Integer) rechargeCash = value End Set End Property Public consumeCash As Integer Public Property _consumeCash As Integer Get Return consumeCash End Get Set(value As Integer) rechargeCash = value End Set End Property Public cancelCash As Integer Public Property _cancelCash As Integer Get Return cancelCash End Get Set(value As Integer) cancelCash = value End Set End Property Public allCash As Integer Public Property _allCash As Integer Get Return allCash End Get Set(value As Integer) allCash = value End Set End Property Public Ddate As String Public Property _date As String Get Return Ddate End Get Set(value As String) Ddate = value End Set End Property End Class</span>D层
<span style="font-size:18px;">Imports System.Data.SqlClient Public Class DayBillDAO Public Function queryRechargeCash(ByVal date1 As String) As DataTable '在recharge这张表中查询收取金额 Dim db As New Entity.Dbutil '实例化一个新的数据库连接 Dim dt As New DataTable '实例化D层DataTable这个类的一个对象 Using conn As New SqlConnection(db.connstring) conn.Open() Dim sql As String Dim cmd As New SqlCommand Dim dataAdapter As New SqlDataAdapter Dim dst As New DataSet sql = "select * from Recharge_info where date=@date" '从rechargeinfo这张表中查找充值金额,根据日期进行相关选择 cmd = New SqlCommand(sql, conn) cmd.Parameters.Add(New SqlParameter("@date", date1)) dataAdapter.SelectCommand = cmd dataAdapter.Fill(dst, "Recharge_info") dt = dst.Tables("Recharge_info") Return dt End Using End Function Public Function queryCancelCash(ByVal date2 As String) As DataTable '在卡表中查询退还金额也就是余额 Dim db As New Entity.Dbutil Dim dt As New DataTable Using conn As New SqlConnection(db.connstring) conn.Open() Dim sql As String Dim cmd As New SqlCommand Dim dataAdapter As New SqlDataAdapter Dim dst As New DataSet sql = "select * from card_info where returnDate=@date " '从卡表中选择退还金额,根据日期进行相关判断 cmd = New SqlCommand(sql, conn) cmd.Parameters.Add(New SqlParameter("@date", date2)) dataAdapter.SelectCommand = cmd dataAdapter.Fill(dst, "card_info") dt = dst.Tables("card_info") Return dt End Using End Function Public Function queryConsumeCash(ByVal date3 As String) As DataTable '从line表中查询消费金额 Dim db As New Entity.Dbutil Dim dt As New DataTable Using conn As New SqlConnection(db.connstring) conn.Open() Dim sql As String Dim cmd As New SqlCommand Dim dataAdapter As New SqlDataAdapter Dim dst As New DataSet sql = "select * from line_info where offdate=@date " '从line表中查找消费金额 cmd = New SqlCommand(sql, conn) cmd.Parameters.Add(New SqlParameter("@date", date3)) dataAdapter.SelectCommand = cmd dataAdapter.Fill(dst, "line_info") dt = dst.Tables("line_info") Return dt End Using End Function Public Sub insertDayBill(ByVal checkDayinfo As Entity.CheckDayinfo) '如果,我说的是如果啊,如果日结账单里面没有记录,我们需要在日结账单里面插入一条新的记录,如果有,我们更新即可 Dim db As New Entity.Dbutil Using conn As New SqlConnection(db.connstring) conn.Open() Dim sql As String Dim cmd As New SqlCommand sql = "insert into CheckDay_info values (@rechargeCash,@consumeCash,@cancelCash,@allCash,@date)" cmd = New SqlCommand(sql, conn) cmd.Parameters.Add(New SqlParameter("@rechargeCash", checkDayinfo.rechargeCash)) cmd.Parameters.Add(New SqlParameter("@consumeCash", checkDayinfo.consumeCash)) cmd.Parameters.Add(New SqlParameter("@cancelCash", checkDayinfo.cancelCash)) cmd.Parameters.Add(New SqlParameter("@allCash", checkDayinfo.allCash)) cmd.Parameters.Add(New SqlParameter("@date", checkDayinfo.Ddate)) cmd.ExecuteNonQuery() End Using End Sub Public Sub updateDayBill(ByVal checkDayinfo As Entity.CheckDayinfo) '更新日结账单里面的内容 Dim db As New Entity.Dbutil Dim dt As New DataTable Using conn As New SqlConnection(db.connstring) conn.Open() Dim sql As String Dim cmd As New SqlCommand sql = "update CheckDay_info set rechargeCash=@rechargeCash,consumeCash=@consumeCash,cancelCash=@cancelCash,allCash=@allCash where date=@date " cmd = New SqlCommand(sql, conn) cmd.Parameters.Add(New SqlParameter("@rechargeCash", checkDayinfo.rechargeCash)) cmd.Parameters.Add(New SqlParameter("@consumeCash", checkDayinfo.consumeCash)) cmd.Parameters.Add(New SqlParameter("@cancelCash", checkDayinfo.cancelCash)) cmd.Parameters.Add(New SqlParameter("@allCash", checkDayinfo.allCash)) cmd.Parameters.Add(New SqlParameter("@date", checkDayinfo.Ddate)) cmd.ExecuteNonQuery() End Using End Sub Public Function queryCheckDay(ByVal date1 As String) As Entity.CheckDayinfo '查询日结账单中的相关信息,决定我们在U层的时候到底是插入一条记录呢,还是更新一条记录 Dim db As New Entity.Dbutil Dim CheckDayinfo As New Entity.CheckDayinfo Using conn As New SqlConnection(db.connstring) conn.Open() Dim sql As String Dim cmd As SqlCommand Dim reader As SqlDataReader sql = "select * from CheckDay_info where date=@date" cmd = New SqlCommand(sql, conn) cmd.Parameters.Add(New SqlParameter("date", date1)) reader = cmd.ExecuteReader If (reader.Read()) Then CheckDayinfo.rechargeCash = reader.GetDecimal(reader.GetOrdinal("rechargeCash")) CheckDayinfo.consumeCash = reader.GetDecimal(reader.GetOrdinal("consumeCash")) CheckDayinfo.cancelCash = reader.GetDecimal(reader.GetOrdinal("cancelCash")) CheckDayinfo.allCash = reader.GetDecimal(reader.GetOrdinal("allCash")) CheckDayinfo.Ddate = reader.GetString(reader.GetOrdinal("date")) Else CheckDayinfo = Nothing End If End Using Return CheckDayinfo End Function End Class </span>B层
<span style="font-size:18px;">Public Class DayBillManager Public Function queryRechargeCash(ByVal date1 As String) As DataTable '在recharge这张表中查询收取金额的相关信息 Dim DayBillDAO As New DAL.DayBillDAO Return DayBillDAO.queryRechargeCash(date1) End Function Public Function queryCancelCash(ByVal date2 As String) As DataTable '从卡表中查询退还金额也就是余额 Dim DayBillDAO As New DAL.DayBillDAO Return DayBillDAO.queryCancelCash(date2) End Function Public Function queryConsumeCash(ByVal date3 As String) As DataTable '从line表查询消费金额 Dim DayBillDAO As New DAL.DayBillDAO Return DayBillDAO.queryConsumeCash(date3) End Function Public Sub inserDayBill(ByVal checkDayinfo As Entity.CheckDayinfo) '如果日结账单里面没有信息,我们需要插入一条 Dim DayBillDAO As New DAL.DayBillDAO DayBillDAO.insertDayBill(checkDayinfo) End Sub Public Sub updateDayBill(ByVal checkDayinfo As Entity.CheckDayinfo) '如果日结账单里面有信息,我们只需要更新即可 Dim DayBillDAO As New DAL.DayBillDAO DayBillDAO.updateDayBill(checkDayinfo) End Sub Public Function queryCheckDay(ByVal date1 As String) As Entity.CheckDayinfo '查找日结账单中的相关信息,如果没有信息,我们需要插入一条信息,如果有,我们需要更新一条信息 Dim DayBillDAO As New DAL.DayBillDAO Return DayBillDAO.queryCheckDay(date1) End Function End Class </span>U层
<span style="font-size:18px;">Public Class frmDayBill Private Sub frmDayBill_Load(sender As Object, e As EventArgs) Handles MyBase.Load Call write() 'TODO: 这行代码将数据加载到表“DataSet1.CheckDay_info”中。您可以根据需要移动或删除它。 Me.CheckDay_infoTableAdapter.Fill(Me.DataSet1.CheckDay_info, CStr(Format(dtpDate.Value, "yyyy-MM-dd"))) Me.ReportViewer1.RefreshReport() End Sub Public Sub write() '在这里,添加了一个DataGridView把相关信息显示在DataGridView中,在报表中显示的时候,我们只需要用一个循环进行相加即可 Dim daybill As New BLL.DayBillManager Dim dt1 As New DataTable dt1 = daybill.queryRechargeCash(Format(dtpDate.Value, "yyyy-MM-dd")) dgv.AutoGenerateColumns = True dgv.DataSource = dt1 Dim i As Integer '定义变量 Dim rechargeCash As Decimal For i = 0 To dgv.RowCount - 1 '充值的金额等于DataGirdView所有的行中的列相加,这里的列即recharge_info中的第二列(rechargeAmount)(当天的哦) rechargeCash = rechargeCash + dgv.Rows(i).Cells(2).Value Next i Dim cancelCash As Decimal dt1 = daybill.queryCancelCash(Format(dtpDate.Value, "yyyy-MM-dd")) dgv.DataSource = dt1 For i = 0 To dgv.RowCount - 1 '退还的金额等于DataGirdView所有的行中的列相加,这里的列即card_info中的第二列balance,也就是退还金额 cancelCash = cancelCash + dgv.Rows(i).Cells(2).Value Next i Dim consumeCash As Decimal dt1 = daybill.queryConsumeCash(Format(dtpDate.Value, "yyyy-MM-dd")) dgv.DataSource = dt1 For i = 0 To dgv.RowCount - 1 '消费的金额等于DataGridView中所有的行中的列相加,这里的列即line_info中的第六列consumeMoney,也就是消费金额 consumeCash = consumeCash + dgv.Rows(i).Cells(6).Value Next i Dim allCash As Decimal allCash = rechargeCash - cancelCash '总金额,等于充值金额减去退还金额 Dim enCheck As New Entity.CheckDayinfo '封装实体 Dim enCheck1 As New Entity.CheckDayinfo enCheck1.rechargeCash = rechargeCash enCheck1.cancelCash = cancelCash enCheck1.consumeCash = consumeCash enCheck1.allCash = allCash enCheck1.Ddate = CStr(Format(dtpDate.Value, "yyyy-MM-dd")) enCheck = daybill.queryCheckDay(Format(dtpDate.Value, "yyyy-MM-dd ")) '如果enCheck中没有记录,我们就插入一条,否则更新 If (enCheck Is Nothing) Then daybill.inserDayBill(enCheck1) Else daybill.updateDayBill(enCheck1) End If End Sub Private Sub dtpDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpDate.ValueChanged '调用窗体加载事件 Call frmDayBill_Load(sender, e) End Sub End Class</span>最后运行结果;
计算机出现之后,我们利用计算机处理数据和界面设计的功能来生成、展示报表。计算机上的报表的主要特点是数据动态化,格式多样化,并且实现报表数据和报表格式的完全分离,用户可以只修改数据,或者只修改格式。报表分类EXCEL、WORD等编辑软件:它们可以做出很复杂的报表格式,但是由于它们没有定义专门的报表结构来动态的加载报表数据,所有这类软件中的数据都是已经定义好的,静态的,不能动态变化的。它们没有办法实现报表软件的“数据动态化”特性。自此,日结的功能告一段落,机房收费系统未完,待续......