如何在visual basic 6.0中使用primary key在一个表中使用外键获取另一个表中的记录 [英] How to get records by using foreign key in one table using primery key another table in visual basic 6.0
本文介绍了如何在visual basic 6.0中使用primary key在一个表中使用外键获取另一个表中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在同一个数据库中有两个表:
i have two tables in same database:
- table1 包含
order_no(primery key)
和cust_name
- table2 包含
order_no(foreign key)
(每个单点订购的食品也有重复条目)、table_no
、items
、date
、cust_name
等等...
- table1 contains
order_no(primery key)
andcust_name
- table2 contains
order_no(foreign key)
(which also have duplicates entry per food item ordered in single order),table_no
,items
,date
,cust_name
and so on...
所以我的问题是如何使用 table1 的主键 order_no
在列表框和数据网格或文本框中显示记录,以便我可以获取分配了具有相同订单号的外键的所有记录表2
so my question is how can i show the records in listbox and datagrid or in textbox using table1's primery key order_no
so i can get all the records which have assigned the foreign key with same order number in table2
我使用的是visual basic 6.0
i am using visual basic 6.0
推荐答案
尝试这样的事情
Private Sub mLoadData(lOrder_no As Long)
' add a reference to Microsoft ActiveX Data Objects 2.8 Library
' add a MSHFLXGD (Microsoft Hierarchical FlexGrid) control named grData to form
Dim rc As ADODB.Recordset
Dim db As New ADODB.Connection
Dim sConnString As String, sSQL As String
'sConnString = create a connection string according to your database - https://www.connectionstrings.com/
db.Open sConnString
sSQL = "SELECT * FROM table2 WHERE order_no =" & lOrder_no
Set rc = db.Execute(sSQL)
Set grData.DataSource = rc
End Sub
这篇关于如何在visual basic 6.0中使用primary key在一个表中使用外键获取另一个表中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文