| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5101 人关注过本帖
标题:要将在SQL里的查询结果,在VB界面中显示出来,该VB语句怎样编写?
只看楼主 加入收藏
lingchen
Rank: 1
等 级:禁止访问
帖 子:50
专家分:0
注 册:2006-5-7
结帖率:100%
收藏
 问题点数:0 回复次数:4 
要将在SQL里的查询结果,在VB界面中显示出来,该VB语句怎样编写?
如题,,哪们高手可以帮忙一下!
谢谢!
搜索更多相关主题的帖子: SQL 语句 界面 结果 编写 
2008-01-20 14:59
随风逐流
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:passerby
等 级:版主
威 望:8
帖 子:4054
专家分:271
注 册:2007-6-13
收藏
得分:0 
看你要显示在TEXT里面还是GRID里面.
根据你的需要而定的

[url=http://www./html/6/6694/]极道金丹[/url][url=http://www./html/2/2849/]九阴九阳[/url][url=http://www./html/2/2596/]凡人修仙传[/url]
2008-01-20 19:48
lingchen
Rank: 1
等 级:禁止访问
帖 子:50
专家分:0
注 册:2006-5-7
收藏
得分:0 
当然是要显示在GRID里面啦!
可是就是不知道怎么写这样的语句呀!!
请问可以帮忙吗?
2008-01-21 13:43
ccc111898
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-1-22
收藏
得分:0 
斑竹,我也遇到类似的问题
Option Explicit
Public txtSQL As String
Dim mrc As New ADODB.Recordset

Private Sub rtfText_SelChange()
    fMainForm.tbToolBar.Buttons("粗体").Value = IIf(rtfText.SelBold, tbrPressed, tbrUnpressed)
    fMainForm.tbToolBar.Buttons("斜体").Value = IIf(rtfText.SelItalic, tbrPressed, tbrUnpressed)
    fMainForm.tbToolBar.Buttons("下划线").Value = IIf(rtfText.SelUnderline, tbrPressed, tbrUnpressed)
    fMainForm.tbToolBar.Buttons("左对齐").Value = IIf(rtfText.SelAlignment = rtfLeft, tbrPressed, tbrUnpressed)
    fMainForm.tbToolBar.Buttons("置中").Value = IIf(rtfText.SelAlignment = rtfCenter, tbrPressed, tbrUnpressed)
    fMainForm.tbToolBar.Buttons("右对齐").Value = IIf(rtfText.SelAlignment = rtfRight, tbrPressed, tbrUnpressed)
End Sub

Private Sub Command2_Click()
 Unload Me
 cnn.Close
End Sub

Private Sub Form_Load()
  '创建Connection对象cnn,关键New用于创建新对象
  Dim cnn As New ADODB.Connection
  '创建Recordset对象rs1
  Dim rs1 As New ADODB.Recordset
  '设置连接字符串ConnectionString属性
  cnn.ConnectionString = "DRIVER={SQL SERVER};SERVER=YANG-CEBD011996;UID=sa;PWD=;Database=BAM"
  '打开到数据库的连接
  cnn.Open
  '设置查询记录集的SQL语句,从表Departments中读取记录
  Dim varSource As String
  varSource = "SELECT * FROM tbl_WSType"
  
  '判断连接的状态
  If cnn.State = adStateOpen Then
    '设置rs1的ActiveConnection属性,指定与其关联的数据库连接
    Set rs1.ActiveConnection = cnn
    '设置游标类型
    rs1.CursorType = adOpenStatic
    '打开记录集,将从表Users中读取的结果集保存到记录集rs1中
    rs1.Open varSource
    '显示记录集中记录的数量
    MsgBox "当前记录集中共有记录 " + Trim(rs1.RecordCount) + " 个"
   
    '将指针移动到记录集的最后
    ShowTitle
   ShowData
   
   rs1.Move rs1.RecordCount
    '判断EOF属性的值
    If rs1.EOF = True Then
      MsgBox "已经达到记录集的结尾"
    End If
    '关闭记录集
    rs1.Close
    '关闭cnn对象
    cnn.Close
  End If
End Sub

Private Sub ShowData()
   
    Dim j As Integer
    Dim i As Integer
    Dim MsgText As String
Dim cnn As New ADODB.Connection
  '创建Recordset对象rs1
  Dim rs1 As New ADODB.Recordset
  '设置连接字符串ConnectionString属性
  cnn.ConnectionString = "DRIVER={SQL SERVER};SERVER=YANG-CEBD011996;UID=sa;PWD=;Database=BAM"
  '打开到数据库的连接
  cnn.Open
  
  txtSQL = "select * from tbl_WSType"
    Set mrc = ExecuteSQL(txtSQL, MsgText)
   
        With msglist
        .Rows = 1
        
        Do While Not mrc.EOF
            .Rows = .Rows + 1
            For i = 1 To mrc.Fields.Count
                If Not IsNull(Trim(mrc.Fields(i - 1))) Then
                Select Case mrc.Fields(i - 1).Type
                    Case adDBDate
                        .TextMatrix(.Rows - 1, i) = Format(mrc.Fields(i - 1) & "", "yyyy-mm-dd")
                    Case Else
                        .TextMatrix(.Rows - 1, i) = mrc.Fields(i - 1) & ""
                End Select
                End If
            Next i
            mrc.MoveNext
        Loop
    End With
    mrc.Close
End Sub

Private Sub ShowTitle()
    Dim i As Integer
    With msglist
        .Cols = 4
        .TextMatrix(0, 1) = ""
        .TextMatrix(0, 2) = "WSType"
        .TextMatrix(0, 3) = "VirtualProcID"
        '固定表头
        .FixedRows = 1
        '设置各列的对齐方式
      
        '表头项居中
        .FillStyle = flexFillRepeat
        .Col = 0
        .Row = 0
        .RowSel = 1
        .ColSel = .Cols - 1
        .CellAlignment = 4
        '设置单元大小
        .ColWidth(0) = 1000
        .ColWidth(1) = 0
        .ColWidth(2) = 1000
        .ColWidth(3) = 1000
        .Row = 1
    End With
End Sub

里面的程序都是我从别的地方COPY来的,摸索着前进啊,现在我的对象里有一个MSflexgrid
但是我查询的纪录不能显示出来,目前运行到Private Sub ShowTitle()里面的 Do While Not mrc.EOF
这一步就报错“ 3704 数据库对象关闭时不允许操作“。
我再把模块的数据也贴出来啊
Public cnn As New ADODB.Connection
Public rst As ADODB.Recordset


Public Function connecttoserver() As Boolean
cnn.ConnectionString = "DRIVER={SQL SERVER};SERVER=YANG-CEBD011996;UID=sa;PWD=;Database=BAM"
cnn.ConnectionTimeout = 30
cnn.Open
connecttoserver = True
End Function
Public Function disconnect() As Boolean
    If cnn.State = adStateOpen Then
        cnn.Close
    End If
    disconnect = True
End Function


Public Function ExecuteSQL(ByVal SQL _
   As String, MsgString As String) _
   As ADODB.Recordset
'executes SQL and returns Recordset
   Dim cnn As ADODB.Connection
   Dim rst As ADODB.Recordset
   Dim sTokens() As String
   
   On Error GoTo ExecuteSQL_Error
   
   sTokens = Split(SQL)
   Set cnn = New ADODB.Connection
   cnn.Open ConnectString
   If InStr("INSERT,DELETE,UPDATE", _
      UCase$(sTokens(0))) Then
      cnn.Execute SQL
      MsgString = sTokens(0) & _
         " query successful"
   Else
      Set rst = New ADODB.Recordset
      rst.Open Trim$(SQL), cnn, _
         adOpenKeyset, _
         adLockOptimistic
      'rst.MoveLast     'get RecordCount
      Set ExecuteSQL = rst
      MsgString = "查询到" & rst.RecordCount & _
         " 条记录 "
   End If
ExecuteSQL_Exit:
   Set rst = Nothing
   Set cnn = Nothing
   Exit Function
   
   
ExecuteSQL_Error:
   MsgString = "查询错误: " & _
      Err.Description
   Resume ExecuteSQL_Exit
End Function
2008-01-22 17:30
ccc111898
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-1-22
收藏
得分:0 
上面写错一个地方
现在报错的地方是show data()里的,不是show title里的语句
2008-01-22 18:13
快速回复:要将在SQL里的查询结果,在VB界面中显示出来,该VB语句怎样编写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016348 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved