Imports System.data
Imports System.Data.SqlClient
Imports System.Threading
'穷人的多线程查询处理器
Public Class PoorMansQueryProcess
'Global DataSet Obiect
Dim ds As DataSet
Public Sub main()
'create the new data set here,
'otherwise then threads willtru to create it
'this is "Shared",so you need to be careful
'that the threads do nto hose the instance
ds = New DataSet("Customers")
'create t1 and t2 threads
Dim t1 As New Thread(AddressOf Get_Suppliers)
Dim t2 As New Thread(AddressOf Get_Products)
'start new threads
t1.Start()
t2.Start()
'Wait for threads to finish up
t1.Join() '阻塞调用线程,直到到某个线程终止为止
'create the relationship between the two data sources with the data relation object
Dim dr As DataRelation
Dim dc1, dc2 As DataColumn
'get tje parent and child columns of the two tables.
dc1 = ds.Tables("Suppliers").Columns("SupplierID")
dc2 = ds.Tables("Products").Columns("SupplierID")
dr = New System.Data.DataRelation("match", dc1, dc2)
'add the relationship
ds.Relations.Add(dr)
ds.WriteXml("e:\netstudy\ado.xml")
MsgBox("Finito")
End Sub
Sub Get_Suppliers()
'***
'Connection to Server A
'***
Dim strCN As String
strCN = "uid=sa;pwd=;database=northwind;server="
Dim cn As SqlConnection = New SqlConnection(strCN)
Dim adpSuppliers As SqlDataAdapter = New SqlDataAdapter
adpSuppliers.TableMappings.Add("Table", "Suppliers")
Dim cmdSuppliers As SqlCommand = New SqlCommand("select * from suppliers", cn)
cmdSuppliers.CommandType = CommandType.Text
adpSuppliers.SelectCommand = cmdSuppliers
adpSuppliers.Fill(ds)
cn.Open()
End Sub
Sub Get_Products()
'***
'Connection to Server A
'***
Dim strCN As String
strCN = "uid=sa;pwd=;database=northwind;server="
Dim cn As SqlConnection = New SqlConnection(strCN)
Dim adpProducts As SqlDataAdapter = New SqlDataAdapter
adpProducts.TableMappings.Add("Table", "Products")
Dim cmdSuppliers As SqlCommand = New SqlCommand("select * from suppliers", cn)
cmdSuppliers.CommandType = CommandType.Text
adpProducts.SelectCommand = cmdSuppliers
adpProducts.Fill(ds)
cn.Close()
End Sub