如何在Visual Basic中连接两个或多个数据库? [英] How to Connect two or more Databases in Visual Basic?

查看:45
本文介绍了如何在Visual Basic中连接两个或多个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在VB中对连接两个或多个mdb数据库的东西进行编程,但是我不知道该怎么做.我想我必须加载所有数据库才能逐表连接.并将其再次保存为".mdb"(我不知道该命令-我想我可以找到它们,但是如果您可以在此处发布它们,那将是很好的).我用谷歌搜索,但是找不到正确的答案-就像我为问题使用了错误的字眼...

I need to programm something in VB that connects two or more mdb databases but I don't know how I am supposed to do that. I guess that I have to load all Databases to connect table by table. And save it again as ".mdb" (I don't know the commands for that - I think I can find them, but it would be nice if you could post them here). I googled but I can't find the right answer - like I am using the wrong words for my problem...

如果您能帮助我,那将非常好:)

It would be very nice if you could help me :)

数据库示例:
DB1:
-DB1_Table1
-DB1_Table2
-DB1_Table3

Database example:
DB1:
-DB1_Table1
-DB1_Table2
-DB1_Table3

DB2:
-DB2_Table1
-DB2_Table2
-DB2_Table3

DB2:
-DB2_Table1
-DB2_Table2
-DB2_Table3

ConnectedDB(DB1 =主/DB2 =从属):
-DB1_Table1 + DB2_Table1
-DB1_Table2
-DB1_Table3 + DB2_Table3

ConnectedDB(DB1 = master / DB2 = slave):
-DB1_Table1 + DB2_Table1
-DB1_Table2
-DB1_Table3 + DB2_Table3

推荐答案

在"C:\ Users \ Public \ test \ DB1.mdb"中,我有[DB1_Table1]包含

In "C:\Users\Public\test\DB1.mdb" I have [DB1_Table1] containing

ID  DB1_Text
--  --------------
 1  value from DB1

在"C:\ Users \ Public \ test \ DB2.mdb"中,[DB2_Table1]包含

In "C:\Users\Public\test\DB2.mdb" I have [DB2_Table1] containing

ID  DB2_Text
--  --------------
 1  value from DB2

在VB.NET中,以下代码...

In VB.NET the following code ...

Using con As New OleDbConnection
    con.ConnectionString =
            "Provider=Microsoft.ACE.OLEDB.12.0;" &
            "Data Source=C:\Users\Public\test\DB1.mdb;"
    con.Open()
    Using cmd As New OleDbCommand
        cmd.Connection = con
        cmd.CommandText =
                "SELECT " &
                    "DB1_Table1.DB1_Text, " &
                    "DB2_Table1.DB2_Text " &
                "FROM " &
                    "DB1_Table1 " &
                    "INNER JOIN " &
                    "[;Database=C:\Users\Public\test\DB2.mdb].DB2_Table1 " &
                        "ON DB1_Table1.ID = DB2_Table1.ID"
        Dim rdr As OleDbDataReader = cmd.ExecuteReader
        Do While rdr.Read
            Console.WriteLine(rdr("DB1_Text") & " | " & rdr("DB2_Text"))
        Loop
    End Using
    con.Close()
End Using

...产生

value from DB1 | value from DB2

这篇关于如何在Visual Basic中连接两个或多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆