学生信息管理系统之查询数据信息

<span style="font-size:18px;">学生信心管理系统是我做的第一个用到VB和数据库连接的系统,在做这个系统的时候感触颇多。学习完SQL入门经典这本书,对数据库的操作来说,无非就是:“增、删、改、查”。当想要真正应用到一个系统中,对代码的编辑要求是非常苛刻的。比如:学生信息管理系统中查询数据信息这一块,要写的代码量不算多,但是还需好好理解其中代码的意思呢!好了,现在来谈谈我对这一块的理解。以学生系统中查询学籍信息为例。</span>

在这个窗体中我们用到了MSHFlexGrid控件,这个控件的主要作用就是用来显示数据信息。在窗体的加载事件中,我们用它来显示要显示数据的名称。其代码如下:
<span style="font-size:18px;">Private Sub Form_Load()
    With myflexgrid
        .CellAlignment = 4                        <span style="color:#009900;">  '设置MSHFlexGrid控件中每个单元格中显示数据为居中对齐</span>
        .TextMatrix(1, 0) = "学号"
        .TextMatrix(1, 1) = "姓名"
        .TextMatrix(1, 2) = "性别"
        .TextMatrix(1, 3) = "出生日期"
        .TextMatrix(1, 4) = "班号"
        .TextMatrix(1, 5) = "联系电话"
        .TextMatrix(1, 6) = "入校日期"
        .TextMatrix(1, 7) = "家庭住址"
    End With
End Sub</span>
另外,在这个窗体中还有两个命令按钮,”退出查询“这命令按钮其代码很简单,在这里就不再介绍。下面介绍一下”查询“命令按钮的代码段:
     <span style="font-size:18px;">Dim txtSQL As String                    <span style="color:#009900;">'执行SQL语句</span>
    Dim MsgText As String                   <span style="color:#009900;">'执行SQL语句后的弹出对话框</span>
    Dim dd(4) As Boolean                    <span style="color:#009900;">'定义数组dd()为布尔型,用于判断选中的查询方式</span>。
    Dim mrc As ADODB.Recordset              <span style="color:#009900;">'定义mrc为数据库记录集</span></span>
    在”查询“框架中,我们通过复选框Check控件数组来确定我们的查询方式,其代码如下:
<span style="font-size:18px;">txtSQL = "select * from student_Info where "            <span style="white-space:pre">			</span><span style="color:#009900;">'执行SQL语句,获得student_Info表中的信息</span>
    If Check1(0).Value Then                                 <span style="white-space:pre">			</span><span style="color:#009900;">'选择按“学号”查询信息</span>
        If Trim(txtSID.Text) = "" Then
            sMeg = "学号不能为空"
            MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
            txtSID.SetFocus
            Exit Sub
        Else
            If Not IsNumeric(Trim(txtSID.Text)) Then
                MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告"
                Exit Sub
                txtSID.SetFocus
            End If
            dd(0) = True                                                       <span style="white-space:pre">	</span><span style="color:#009900;"> '选择“学号”时,dd(0)为true</span>
            txtSQL = txtSQL & "student_ID = '" & Trim(txtSID.Text) & "'"
        End If
    End If</span>
If Check1(1).Value Then
        If Trim(txtName.Text) = "" Then
            sMeg = "姓名不能为空"
            MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
            txtName.SetFocus
            Exit Sub
        Else
            dd(1) = True                                                        <span style="color:#009900;">'已选中姓名复选框</span>
            If dd(0) Then
                txtSQL = <span style="color:#cc0000;">txtSQL</span> & "and student_Name = '" & txtName.Text & "'"   <span style="color:#009900;">'dd(0)为Ture,选中学号复选框。</span>
            Else
                txtSQL = <span style="background-color: rgb(255, 255, 255);">txtSQL </span>& "student_Name = '" & txtName.Text & "'"<span style="white-space:pre">	</span>
            End If
        End If
    End If
 End Sub<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">  </span>
这里大家注意下 txtSQL =txtSQL & "and student_Name = '" & txtClassno.Text & "'"和txtSQL = txtSQL & "stduent_Name = '" & txtClassno.Text & "'"红色标记的txtSQL和黑色的txtSQL代表的意思是不一样的。txtSQL表示的是txtSQL=txtSQL & "student_ID = '" & Trim(txtSID.Text) & " ' "这个过程(个人理解),txtSQL表示的是获得表中的信息。
 If Check1(2).Value Then
        If Trim(txtClassno.Text) = "" Then
            sMeg = "班号不能为空"
            MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
            txtClassno.SetFocus
            Exit Sub
        Else
            dd(2) = True                                               <span style="color:#009900;"> '已选中班号复选框</span>
            If dd(0) Or dd(1) Then
                txtSQL = txtSQL & "and class_No = '" & txtClassno.Text & "'"
            Else
                txtSQL = txtSQL & "class_No = '" & txtClassno.Text & "'"
            End If
        End If
    End If
这两段代码是用来判断用户是否同时选择多个查询方式,两段代码有异曲同工之处,在这里不再解释,理解就好。下面是把查询到的数据信息显示在MSHFlexGrid控件中。
    txtSQL = txtSQL & " order by student_ID "                     <span style="color:#009900;">'查询出数据信息按照学号排序显示。</span>
    Set mrc = ExecuteSQL(txtSQL, MsgText)
    
    With myflexgrid
        .Rows = 2<span style="white-space:pre">						</span> <span style="color:#009900;"> ’<span style="font-size:18px; white-space: pre;">MSHFlexGrid属性Rows表示显示的行,Col表示显示的列。</span></span>
        .CellAlignment = 4<span style="white-space:pre">			</span>
        .TextMatrix(1, 0) = "学号"<span style="white-space:pre">			</span>          <span style="color:#009900;">’MSHFlexGrid.textMatrix(Rows,Col)指返回第Rows行,第Col列的数据。</span><pre name="code" class="vb" style="font-size:18px;"><span style="font-family: Arial, Helvetica, sans-serif;">             .TextMatrix(1, 1) = "姓名" </span>
.TextMatrix(1, 2) = "性别" .TextMatrix(1, 3) = "出生日期" .TextMatrix(1, 4) = "班号" .TextMatrix(1, 5) = "联系电话" .TextMatrix(1, 6) = "入校日期" .TextMatrix(1, 7) = "家庭住址" Do While Not mrc.EOF 遍历记录法之一 .Rows = .Rows + 1 '这种方式也可防止空白行的出现 .CellAlignment = 4 .TextMatrix(.Rows - 1, 0) = mrc.Fields(0) .TextMatrix(.Rows - 1, 1) = mrc.Fields(1) .TextMatrix(.Rows - 1, 2) = mrc.Fields(2) .TextMatrix(.Rows - 1, 3) = Format(mrc.Fields(3), "yyyy-mm-dd") .TextMatrix(.Rows - 1, 4) = mrc.Fields(4) .TextMatrix(.Rows - 1, 5) = mrc.Fields(5) .TextMatrix(.Rows - 1, 6) = Format(mrc.Fields(6), "yyyy-mm-dd") .TextMatrix(.Rows - 1, 7) = mrc.Fields(7) mrc.MoveNext Loop End With mrc.Close
 
 好了,代码完成。这块大家应该注意的是,在设置 
 MSHFlexGrid控件的行和列时,一定要和代码中的返回数据时的行和列保持一致,要不然会出错的哦!!! 
 






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值