0

I am using the following code to select from one database and need to insert into another database.. Please suggest me the code:

Code :

'Connection for Original database from where i have to import

Dim constrOrg As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath
Dim conn_OrgDB As New OleDb.OleDbConnection(constrOrg)

'Connection for my database to where i have to import

Dim App_Path = System.AppDomain.CurrentDomain.BaseDirectory()
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + App_Path + "Mydb.accdb"

Dim cnnOLEDB As New OleDb.OleDbConnection(constr)

    Dim strSelInv_one As String = ("SELECT * FROM INV_ONE where (DOCU_DT>=@stdt AND DOCU<=@enddt) ")
    Dim comm_inv_one As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSelInv_one, conn_OrgDB )

    comm_inv_one.Parameters.AddWithValue("@stdt", StTime)
    comm_inv_one.Parameters.AddWithValue("@enddt", Endtime)

    dr = comm_inv_one.ExecuteReader

    Do While dr.Read = True

    Dim strInsInv1 As String = "INSERT INTO INVOICE_ONE(CODE_NO,LAY,..) SELECT CODE_NO,LAY,... FROM INV_ONE where (CODE_NO=@code)"
        Dim comm_Insinv1 As OleDb.OleDbCommand = New OleDb.OleDbCommand(strInsInv1, cnnOLEDB)

        comm_Insinv1.Parameters.AddWithValue("@code", Code_no)
       comm_Insinv1.ExecuteNonQuery()

      Loop


       'Here INVOICE_ONE table belongs cnnOLEDB connection obj of one database and INV_ONE table belongs to conn_OrgDB  connection object of another database..


    ' How to use the 2 connection object ? If i use only one connection object i.e. cnnOLEDB it gives the following Error:

    " The Microsoft Office Access database engine cannot find the input table or query 'INV_ONE'.  Make sure it exists and that its name is spelled correctly.     "

Please suggest me the code.. Thank you

4

2 回答 2

2

试试这段代码,显然不要像他的那样长(基本上把你不需要的东西扔掉并输入你的代码,现在......

此外 :

Dim connectionStr = Constants.Input.MDB.CONNECTION_STRING & _
    "Data Source=" & dbFullPath & ";"
Dim connection As New System.Data.OleDb.OleDbConnection(connectionStr)

另外使用

Dim connectionStr2 = "Other conection string"
Dim connection2 As New System.Data.OleDb.OleDbConnection(connectionStr)

并将其导入到他的代码中。

链接: 使用 vb.net 在 MS Access 中选择数据非常慢。我做对了吗?

如果这没有帮助,我很抱歉,因为我没有访问数据库,我无法进一步测试。警戒。

于 2013-04-25T13:29:12.673 回答
1

我的建议是使用 Access 在目标数据库中创建指向源(生产)数据库中相应表的链接表。然后,您至少可以使用相同的连接操作两组表,从而使您可以选择执行

INSERT INTO INVOICE_1 (codeno, lchlndt, ...)
SELECT codeno, lchlndt, ... FROM INV_ONE ...
于 2013-04-25T14:21:13.033 回答