locked
Sql Query into VB.Net within 2 databases RRS feed

  • Question

  • Is it possible to query like this?

    I need to link between 2 databases.

        Public Function GetMaterialFinishType()
            Dim dt As New DataTable
            Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                Using cmd As New OleDb.OleDbCommand With
                    {
                        .Connection = cn,
                        .CommandText = "SELECT FinishTypeFace, FinishTypeBack, FinishTypeEdge FROM '" & OS6432.CVPath & "material.cvm'([tbl Material].ID) WHERE SourceDBID IN (SELECT MatDBID, SourceDBID FROM Materials) RIGHT JOIN '" & OS6432.CVPath & "material.cvm'([tbl Material].ID) = Materials.SourceDBID;"
                    }
                    '.CommandText = "SELECT MatDBID, SourceDBID FROM Materials"
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
    
                End Using
            End Using
    
            Return dt
    
        End Function
    

    Thank you in advance


    Newbie

    Thursday, June 19, 2014 9:23 PM

Answers

  • You should be using SQLclient class (not oledb) and inport the two databases into a datatable.   Then use Linq JOIN to merge the two tables as required.

    jdweng

    Friday, June 20, 2014 12:39 PM

All replies

  • Hi Martin,

    No, you can't do this...

    First, you have to query one Command and then you can fill the second command.

    After that you can do anything you want with the data (using linq, etc)

    See ya !


    Diego Murakami View Diego Murakami's LinkedIn profile - MCP, MS, MCSD
    * Por favor "Marcar como Resposta" caso esta for útil para sua dúvida.

    Thursday, June 19, 2014 10:01 PM
  • Are you connecting to two different Databases, or two different Tables in the same Database?

    If you are using the same connection string then it is possible provided you have the crentials to access the two tables with the same username and password.


    jdweng

    Friday, June 20, 2014 4:13 AM
  • I need to connect to 2 différent databases not in the same folder.

    Both are using same credential.

    I'm using same connection

       

    PrivateBuilder AsNewOleDb.OleDbConnectionStringBuilderWith


         {

             .Provider =

    "Microsoft.Jet.OLEDB.4.0",

             .DataSource = IO.

    Path.Combine(PathDatabase, JobNamepnc)

         }


    VBScript Visual Studio 2013 CNC Machine


    • Edited by Martin P Friday, June 20, 2014 12:07 PM
    Friday, June 20, 2014 12:03 PM
  • You should be using SQLclient class (not oledb) and inport the two databases into a datatable.   Then use Linq JOIN to merge the two tables as required.

    jdweng

    Friday, June 20, 2014 12:39 PM