vb中查询oracle的表

qq_34601088 2016-04-11 07:54:10
oracle和vb6通过adobc控件连接完成
现在想实现,插入一个text控件,在text中输入oracle的某一个表名,点击command就能在datagrid中显示整张表的内容。
请问能否实现,如果不行,该用什么控件组合实现
...全文
525 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
A0-Miss?? 2020-03-12
  • 打赏
  • 举报
回复
数据库已经有了,里面也有表格,我这边有个接口输入字段DH,返回值为BM,可以通过VB调用,不用他这个接口实现吗?谢谢 数据库地址:192.168.1.101 数据库类型:orcl 用户名: DZHT 密码:DZHT 数据表: partdic 输入的代号字段:dh (自动获取VB中的代号值) 生成的编码字段:bm 下面是SolidWorks里面的宏文件: '定义SolidWorks Dim strmat As String Dim a() As String Dim b As Integer Dim c As String Dim d As String Dim e As String Dim f As String Dim g() As String Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim tempvalue As String Dim part As Object Dim Part1 As Object Dim swApp As SldWorks.SldWorks Dim swModelDoc As SldWorks.ModelDoc2 Dim swConfig As SldWorks.Configuration Dim CustPropMgr As SldWorks.CustomPropertyManager Dim swModel As SldWorks.ModelDoc2 Sub main() Set swApp = Application.SldWorks Set swModelDoc = swApp.ActiveDoc Set part = swApp.ActiveDoc Set SelMgr = part.SelectionManager swApp.ActiveDoc.ActiveView.FrameState = 1 Set swModelDoc = swApp.ActiveDoc Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration Set swModel = swApp.ActiveDoc Set CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguration.Name) ' 配置特定延伸 '设定变量 Set part = swApp.ActiveDoc Set SelMgr = part.SelectionManager swApp.ActiveDoc.ActiveView.FrameState = 1 Set swModelDoc = swApp.ActiveDoc Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration Set swModel = swApp.ActiveDoc Set CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguration.Name) ' 配置特定延伸 '设定变量 c = swApp.ActiveDoc.GetTitle() ' 零件名 b = swApp.ActiveDoc.GetType() '零件类型 a = Split(c, " ") '重点:分隔标识符,这里是一个空格,也可换成其他符号 d = a(0)'(这个是想输入数据库的值也就是dh,想得到返回值bm,填到备注里) e = a(1) f = Right(c, 7) If b = 1 Then strmat1 = Chr(34) + Trim("SW-Mass" + "@" + "@默认@") + c + ".SLDPRT" + Chr(34) ' Weight strmat = Chr(34) + Trim("SW-Material" + "@" + "@默认@") + c + ".SLDPRT" + Chr(34) ' Material w = "/" Else strmat = "附图" strmat1 = Chr(34) + Trim("SW-Mass" + "@" + "@默认@") + c + ".SLDASM" + Chr(34) ' Weight w = "组件" End If If f = ".SLDPRT" Then g = Split(e, ".") e = g(0) End If If f = ".sldprt" Then g = Split(e, ".") e = g(0) End If If f = ".SLDASM" Then g = Split(e, ".") e = g(0) End If If f = ".sldasm" Then g = Split(e, ".") e = g(0) End If blnretval = part.DeleteCustomInfo2("", "代号") blnretval = part.DeleteCustomInfo2("", "设计") blnretval = part.DeleteCustomInfo2("", "名称") 'blnretval = part.DeleteCustomInfo2("", "编码") blnretval = part.DeleteCustomInfo2("", "材料") blnretval = part.DeleteCustomInfo2("", "规格") blnretval = part.DeleteCustomInfo2("", "数量") blnretval = part.DeleteCustomInfo2("", "Specification") blnretval = part.DeleteCustomInfo2("", "单重") blnretval = part.DeleteCustomInfo2("", "质量") blnretval = part.DeleteCustomInfo2("", "绘图") blnretval = part.DeleteCustomInfo2("", "批准") blnretval = part.DeleteCustomInfo2("", "日期") blnretval = part.DeleteCustomInfo2("", "校核") blnretval = part.DeleteCustomInfo2("", "替代") blnretval = part.DeleteCustomInfo2("", "图幅") blnretval = part.DeleteCustomInfo2("", "版本") blnretval = part.DeleteCustomInfo2("", "备注") blnretval = part.DeleteCustomInfo2("", "审核") blnretval = part.DeleteCustomInfo2("", "Material") b lnretval = part.DeleteCustomInfo2("", "零件号") blnretval = part.DeleteCustomInfo2("", "校核") blnretval = part.DeleteCustomInfo2("", "主管设计") blnretval = part.DeleteCustomInfo2("", "校对") blnretval = part.DeleteCustomInfo2("", "Weight") blnretval = part.DeleteCustomInfo2("", "Description") blnretval = part.DeleteCustomInfo2("", "标准审查") blnretval = part.DeleteCustomInfo2("", "工艺审查") blnretval = part.DeleteCustomInfo2("", "审定") blnretval = part.DeleteCustomInfo2("", "阶段标记S") blnretval = part.DeleteCustomInfo2("", "阶段标记A") blnretval = part.DeleteCustomInfo2("", "阶段标记B") blnretval = part.DeleteCustomInfo2("", "阶段标记") blnretval = part.DeleteCustomInfo2("", "共X张") blnretval = part.DeleteCustomInfo2("", "第X张") '删除栏 CustPropMgr.Delete ("名称") CustPropMgr.Delete ("设计") CustPropMgr.Delete ("绘图") CustPropMgr.Delete ("材料") CustPropMgr.Delete ("代号") CustPropMgr.Delete ("设备名称") CustPropMgr.Delete ("数量") CustPropMgr.Delete ("单重") CustPropMgr.Delete ("规格") CustPropMgr.Delete ("备注") CustPropMgr.Delete ("质量") '新增 CustPropMgr.Add2 "代号/名称", swCustomInfoText, d + e ' 设计 'CustPropMgr.Add2 "绘图", swCustomInfoText, "姓名" ' 设计 'CustPropMgr.Add2 "零件号", swCustomInfoText, "" ' 零件号 'CustPropMgr.Add2 "校对", swCustomInfoText, " " ' 校对 'CustPropMgr.Add2 "审核", swCustomInfoText, " " ' 审核 'custPropMgr.Add2 "批准", swCustomInfoText, " " ' 批准 'CustPropMgr.Add2 "标准化", swCustomInfoText, " " ' 标准化 CustPropMgr.Add2 "代号", swCustomInfoText, d ' 代号 CustPropMgr.Add2 "名称", swCustomInfoText, e ' 名称 CustPropMgr.Add2 "备注", swCustomInfoText, "" ' 备注 CustPropMgr.Add2 "编码", swCustomInfoText, "" ' 编码 CustPropMgr.Add2 "规格", swCustomInfoText, w ' 规格 CustPropMgr.Add2 "变更单号", swCustomInfoText, "" ' 变更单号 'CustPropMgr.Add2 "阶段标记", swCustomInfoText, "" ' 阶段标记 'CustPropMgr.Add2 "阶段标记S", swCustomInfoText, "" '阶段标记S 'CustPropMgr.Add2 "阶段标记A", swCustomInfoText, "" ' 阶段标记A 'CustPropMgr.Add2 "阶段标记B", swCustomInfoText, "" ' 阶段标记B 'CustPropMgr.Add2 "日期", swCustomInfoText, "" ' 日期 'CustPropMgr.Add2 "共X张", swCustomInfoText, "" ' 共X张 'CustPropMgr.Add2 "第X张", swCustomInfoText, "" ' 第X张 'CustPropMgr.Add2 "比例", swCustomInfoText, " " ' 比例 CustPropMgr.Add2 "质量", swCustomInfoText, strmat1 '质量 CustPropMgr.Add2 "单重", swCustomInfoText, strmat1 '单量 CustPropMgr.Add2 "材料", swCustomInfoText, strmat '材料 '加入所需语句 End Sub
qq_34601088 2016-04-13
  • 打赏
  • 举报
回复
引用 1 楼 of123 的回复:
可以。 其实,用 ComboBox 更好一些,不会输入错误,操作也方便。首先将数据库中的表名都填入 Combo 的列表。 Private Sub Form_Load() Dim rstSchema As Recordset Set rstSchema = Adodc1.Recordset.ActiveConnection.OpenSchema(adSchemaTables) rstSchema.MoveNext Do Until rstSchema.EOF Debug.Print "Table name: " & _ rstSchema!TABLE_NAME & vbCr & _ "Table type: " & rstSchema!TABLE_TYPE & vbCr If rstSchema!TABLE_TYPE = "TABLE" Then Combo1.AddItem rstSchema!TABLE_NAME rstSchema.MoveNext Loop If Combo1.ListCount Then Combo1.ListIndex = 0 End Sub 其次,在用户用鼠标选择一个新表时,触发 ComboBox 的 Click 事件。在事件代码中更新 ADODC 的记录源,并刷新: Private Sub Combo1_Click() Adodc1.RecordSource = "SELECT * FROM " & Combo1.List(Combo1.ListIndex) Adodc1.Refresh End Sub
本人实在小白,我在这里加的,还需要在哪里改@of123 Private Sub Form_Load() Dim rstSchema As Recordset Set rstSchema = Adodc1.Recordset.ActiveConnection.OpenSchema(adSchemaTables) rstSchema.MoveNext Do Until rstSchema.EOF Debug.Print "Table name: " & 以色列无人机_ & "" rstSchema!TABLE_NAME & vbCr & "Table type: " & rstSchema!TABLE_TYPE & vbCr If rstSchema!TABLE_TYPE = "TABLE" Then Combo1.AddItem rstSchema!TABLE_NAME rstSchema.MoveNext Loop If Combo1.ListCount Then Combo1.ListIndex = 0 End Sub
qq_34601088 2016-04-13
  • 打赏
  • 举报
回复
引用 1 楼 of123 的回复:
可以。 其实,用 ComboBox 更好一些,不会输入错误,操作也方便。首先将数据库中的表名都填入 Combo 的列表。 Private Sub Form_Load() Dim rstSchema As Recordset Set rstSchema = Adodc1.Recordset.ActiveConnection.OpenSchema(adSchemaTables) rstSchema.MoveNext Do Until rstSchema.EOF Debug.Print "Table name: " & _ rstSchema!TABLE_NAME & vbCr & _ "Table type: " & rstSchema!TABLE_TYPE & vbCr If rstSchema!TABLE_TYPE = "TABLE" Then Combo1.AddItem rstSchema!TABLE_NAME rstSchema.MoveNext Loop If Combo1.ListCount Then Combo1.ListIndex = 0 End Sub 其次,在用户用鼠标选择一个新表时,触发 ComboBox 的 Click 事件。在事件代码中更新 ADODC 的记录源,并刷新: Private Sub Combo1_Click() Adodc1.RecordSource = "SELECT * FROM " & Combo1.List(Combo1.ListIndex) Adodc1.Refresh End Sub
数据库表名在哪里添加呢?
vansoft 2016-04-13
  • 打赏
  • 举报
回复
可以从Oracle系统表里查出所有的表名,加载到下拉列表中。
arthaslsq 2016-04-12
  • 打赏
  • 举报
回复
建议不使用adobc控件,因为adobc控件比较死 可以使用ado库方法,比较灵活。 连接数据库以后,增删改直接用存储过程操作数据。 查询的一个例子如下: 其中gcnOracle是全局变量相当于数据库连接 有其他问题可以追问 ‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘ Private Sub UpdateTableChecking(ByVal lgpatientID As Long) '更新检查表信息,参数为患者ID On Error GoTo errline: Dim cmdData As New ADODB.Command Dim cmdPara As ADODB.Parameter Dim strSQL As String Dim str As String Dim rs As New ADODB.Recordset Dim i As Long mlgCheckID = 0 txtResult = "请选择一个检查以查看" If gcnOracle.State = adStateOpen Then strSQL = "select a.患者ID,a.姓名,b.信息ID,b.患者ID,b.检查项目,b.检查部位,b.报到时间,b.检查时间 from 检查信息表 b,患者信息表 a where" & _ " a.患者ID=b.患者ID And b.患者ID=?" Set cmdPara = cmdData.CreateParameter("ID", adVarNumeric, adParamInput, 6, lgpatientID) Call cmdData.Parameters.Append(cmdPara) Set cmdData.ActiveConnection = gcnOracle cmdData.CommandText = strSQL Set rs = cmdData.Execute clrlvwChecking If rs.RecordCount > 0 Then While Not rs.EOF str = "ID_C_" & rs("信息ID").Value Set mitmX = lvwChecking.ListItems.Add(1, str, rs("姓名").Value) mitmX.SubItems(lvwChecking.ColumnHeaders("检查项目").SubItemIndex) = rs("检查项目").Value mitmX.SubItems(lvwChecking.ColumnHeaders("检查部位").SubItemIndex) = rs("检查部位").Value If Not (IsNull(rs("报到时间").Value)) Then mitmX.SubItems(lvwChecking.ColumnHeaders("报到时间").SubItemIndex) = rs("报到时间").Value Else mitmX.SubItems(lvwChecking.ColumnHeaders("报到时间").SubItemIndex) = G_STR_NOREG End If If Not (IsNull(rs("检查时间").Value)) Then mitmX.SubItems(lvwChecking.ColumnHeaders("检查时间").SubItemIndex) = rs("检查时间").Value Else mitmX.SubItems(lvwChecking.ColumnHeaders("检查时间").SubItemIndex) = G_STR_NORESULT End If rs.MoveNext Wend setLVcolor Else End If rs.Close End If Exit Sub errline: MsgBox "err12-获得检查信息错误" & " --" & Err.Description, vbInformation End Sub ’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’
qq_34601088 2016-04-12
  • 打赏
  • 举报
回复
由于是刚来这个bbs,不太会操作,就在这统一回复吧,谢谢大家帮忙,问题已经解决了,就是一个小程序,用了of123大神的方案,@of123 。万分感谢!
of123 2016-04-12
  • 打赏
  • 举报
回复
可以。 其实,用 ComboBox 更好一些,不会输入错误,操作也方便。首先将数据库中的表名都填入 Combo 的列表。 Private Sub Form_Load() Dim rstSchema As Recordset Set rstSchema = Adodc1.Recordset.ActiveConnection.OpenSchema(adSchemaTables) rstSchema.MoveNext Do Until rstSchema.EOF Debug.Print "Table name: " & _ rstSchema!TABLE_NAME & vbCr & _ "Table type: " & rstSchema!TABLE_TYPE & vbCr If rstSchema!TABLE_TYPE = "TABLE" Then Combo1.AddItem rstSchema!TABLE_NAME rstSchema.MoveNext Loop If Combo1.ListCount Then Combo1.ListIndex = 0 End Sub 其次,在用户用鼠标选择一个新表时,触发 ComboBox 的 Click 事件。在事件代码中更新 ADODC 的记录源,并刷新: Private Sub Combo1_Click() Adodc1.RecordSource = "SELECT * FROM " & Combo1.List(Combo1.ListIndex) Adodc1.Refresh End Sub
of123 2016-04-12
  • 打赏
  • 举报
回复
用 TextBox 和 Command 按钮的方案,同理: Private Sub Command1_Click() Adodc1.RecordSource = "SELECT * FROM " & Text1.Text Adodc1.Refresh End Sub
ZHRXJR 2016-04-12
  • 打赏
  • 举报
回复
使用ADO对象,不使用ADO控件,程序明朗,比较易懂,而且运行十分流畅。
在程序中添加二个引用:
在程序的通用部分定义二个对象,cn 数据库连接对象,RS 记录集对象
Dim cn As New ADODB.Connection, RS As New ADODB.Recordset

设置一个过程 KKK 是数据库连接的过程:
Private Sub KKK(cn)
cn.ConnectionString = "driver={SQL Server};server=.;uid=sa;pwd=;database=驾校管理"
cn.Open
End Sub

在 Form_Load 过程中对Combox1控件添加数据库包含的用户数据表,不添加系统数据表。
Private Sub Form_Load()
Label1.Caption = ""
Call KKK(cn)
Combo1.Clear
Set RS = cn.OpenSchema(adSchemaTables)
Do Until RS.EOF
If Left(RS!TABLE_NAME, 3) <> "sys" And Left(RS!TABLE_NAME, 3) <> "dtp" Then
Combo1.AddItem RS!TABLE_NAME '在 Combo1 加载所有用户数据表,不加载系统数据表
End If
RS.MoveNext
Loop
RS.Close
cn.Close
Label2.Visible = False
End Sub

在Combo1的单击事件中就可以显示所有数据了,显示数据使用的是 MSFlexGrid 控件。
Private Sub Combo1_Click()
Dim SSA As Integer, SQLA As String, PP As Integer, ZDM() As String, PP1 As Integer
Label1.Caption = "显示" & Combo1.Text & "数据表的所有数据"
Label2.Caption = ""
Label2.Visible = True
MSFlexGrid1.Clear
MSFlexGrid1.Visible = True
Call KKK(cn)
SQLA = "Select * From " & Combo1.Text
RS.Open SQLA, cn, 3, 2
SSA = RS.RecordCount
Set fld = RS.Fields
For Each fld In RS.Fields
PP = PP + 1 '得到字段的总数
Next
ReDim ZDM(1 To PP)
For Each fld In RS.Fields
PP1 = PP1 + 1
ZDM(PP1) = fld.Name '向 ZDM 数组中添加字段
Next
With MSFlexGrid1
.Cols = PP
.Rows = 1 + SSA
For I = 1 To PP
.TextMatrix(0, I - 1) = ZDM(I) '在 MSFlexGrid1 中添加标题,即字段名
Next I
For I = 1 To SSA
For J = 0 To PP - 1
.TextMatrix(I, J) = RS.Fields(J) '在 MSFlexGrid1 中添加内容,即显示数据
Next J
RS.MoveNext
Next I
End With
RS.Close
cn.Close
Label2.Caption = "“" & Combo1.Text & "”数据表有" & PP & "个字段,有" & SSA & "条记录!"
End Sub

下面是程序运行图

不是特别复杂的,很简单。
ZHRXJR 2016-04-12
  • 打赏
  • 举报
回复
引用 楼主 qq_34601088 的回复:
oracle和vb6通过adobc控件连接完成现在想实现,插入一个text控件,在text中输入oracle的某一个表名,点击command就能在datagrid中显示整张表的内容。
使用ComboBox控件比使用TextBox控件要好得多,使用ComboBox的单击事件Click,就可以省略command按钮,并且在ComboBox控件中提前操作数据库,就数据库中的数据表加载在ComboBox控件中,这样一方面使得界面比较简略,另一方面可以避免在文本框输入数据表名称时的错误。 我没有oracle数据库,但与SQL数据库的操作基本一样,等一会给你一个例子。

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧