利用SQL处理DataGrid表格问题

时间:2014-06-02 12:36:12   收藏:0   阅读:287

    在最初选定这个控件作为表格的显示容器来说,也曾经尝试使用mshflexgrid控件。毕竟第一次机房收费系统就使用的那个控件。控件的好坏咱不做评价。

    mshflexgrid这个控件,类似于excel,使用的时候需要循环赋值,然后再显示出来。期间运行多长时间,我不太清楚。总感觉很浪费吧。之后又接触了一个DataGridView控件,虽然表格的显示都差不多,用法也几乎类似。

在使用三层架构的机房收费系统里面,很多的数据传递的时实体或者是表格。对于传递表格情况来说,直接把表格通过一个控件来显示出来就行了呗。所以本次机房收费系统,就使用了DataGridView作为表格的容器。

 

DataGridView作为显示的方法:

既然要作为表格显示使用,就肯定要有表头,数据来源等问题吧。

如果为了图方便,在使用DataGridView的时候,可以设置数据源DataSource来替前设置要显示的数据信息,这样,在使用的时候,稍添加一些限制,即可做到查询。

 

   在知道以上那种方法之前,曾经考虑过一个问题,作为查询显示的表格,已经从D层,把表格处理好并且以此传回到U层了,在这种情况下,直接把返回来的dataTable作为数据源显示,同样可以达到目的。这样就省去设置数据源,测试链接等问题。

   在三层之间传递的是DataTable,dataTable作为一个虚拟表,把虚拟表优化之后,直接传递到DataGridView中显示。如果因为数据库里面的字段与要显示的字段不一致,那么通过SQL语句处理好之后在作为返回值传递。

在纠结dataGrid控件,列表头等问题的时候。直接使用sql语句来优化,之后直接使用DataGrid显示。

接下来看一个查询余额实例实现过程:

U层:

Private Sub btnQuery_Click(sender As Object, e As EventArgs) Handles btnQuery.Click

        '验证 两个文本框中是否为空
        Dim MyObject As New UIcommonFunction
        If MyObject.verifyNull(Controls) = False Then
            Exit Sub
        End If

        Dim queryBll As New BLL.SqlQueryRecordBLL

        Try
            Dim dtRechargeLog As New DataTable
            dtRechargeLog = queryBll.QueryRechargeLog(txtCardNo.Text)

            dataGrid.DataSource = dtRechargeLog
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try

    End Sub



U层中使用了一个公有的函数:

 ''' <summary>
    ''' 验证文本框是否为空
    ''' </summary>
    ''' <param name="log"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function verifyNull(log As Windows.Forms.Control.ControlCollection) As Boolean
        Dim objCon As New Control
        Dim flag As Boolean = True  '默认 所有文本框都不为空

        '开始检测每个控件 是否为空的判断
        For Each objCon In log
            If TypeOf (objCon) Is TextBox And objCon.Visible = True Then
                If objCon.Text = "" Then
                    MsgBox(objCon.Tag.ToString & " 不能为空,请输入完整", vbOKOnly, "提示")
                    objCon.Focus()
                    flag = False
                    Return flag
                End If
            ElseIf TypeOf (objCon) Is ComboBox And objCon.Visible = True Then
                If objCon.Text = "" Then
                    MsgBox(objCon.Tag.ToString & " 不能为空,请输入完整", vbOKOnly, "提示")
                    objCon.Focus()
                    flag = False
                    Return flag
                End If
            End If
        Next objCon

        Return True
    End Function


之后调用B层

''' <summary>
    ''' 获取充值记录
    ''' </summary>
    ''' <param name="cardNo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function QueryRechargeLog(cardNo As String) As DataTable
        Dim iStudent As IDAL.IStudent
        iStudent = Factory.DBFactory.CreateStudent

        Dim iRechargeLog As IDAL.IRechargeLog
        iRechargeLog = Factory.DBFactory.CreateRechargeLog

        Dim dtStudent As New DataTable
        dtStudent = iStudent.QueryStudent(cardNo)

        '验证用户的卡号是否存在
        If dtStudent.Rows.Count = 0 Then
            Throw New Exception("卡号不存在")
        End If

        Dim dtRechargeLog As New DataTable
        dtRechargeLog = iRechargeLog.GetRechargeLog(cardNo)

        Return dtRechargeLog

    End Function


之后,表格的处理完全使用SQL语句来优化处理的:

''' <summary>
    ''' 查询指定时间段内的 收取金额
    ''' </summary>
    ''' <param name="stime">开始日期</param>
    ''' <param name="etime">终止日期</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetRechargeInDate(stime As String, etime As String) As DataTable Implements IDAL.IRechargeLog.GetRechargeInDate
        Dim strSQL As String = "select identity(int,1,1) as 序号,cardNo as 卡号,addCash as 充值金额,date as 日期,time as 时间,UserId as 操作人,status as 状态 into #1 from Recharge_Info where date between @sTime and @eTime  select * from #1 drop table #1"
        Dim sqlparams As SqlParameter() = {New SqlParameter("@sTime", stime),
                                           New SqlParameter("@eTime", etime)}

        Dim helper As New SqlHelper
        Dim dtRechargelog As New DataTable
        dtRechargelog = helper.Query(strSQL, CommandType.Text, sqlparams)
        Return dtRechargelog
    End Function


接口与工厂,与上个Demo类似,自行补充吧。

 

     DataGridView作为一个表格控件,可以事先设计好模版,之后显示,也可以仅仅作为一个容器来使用。sql语句可以处理的事情有很多,完全可以后台处理之后直接显示。

       有很多对表操作的地方,都可以用sql处理里面的知识解决。 视图 虚拟表就和很好的应用。

利用SQL处理DataGrid表格问题,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!