Search code examples
vb.netibm-midrangedb2-400

Possible to query As400 DB2 via VB.Net without a PRG?


After spending a few days researching and trying to figure this out solo, I could really use some help.

I'm trying to query the As400's database directly from .Net without the use of a As400 program file. I have very little support other than "go ahead and try" from the As400 administrators (I'm being told what I'm attempting hasn't been done here before).

I'd really like to use CWBX. The code below successfully connects, but I could really use a pointer in the right direction on how to build a query:

Dim As400 As New AS400System
Dim AsProgram As New cwbx.Program
Dim AsCommand As New cwbx.Command
Dim strQuery As String

As400.Define("AS400")
As400.UserID = ""
As400.Password = ""
As400.IPAddress = ""
As400.Connect(cwbcoServiceEnum.cwbcoServiceRemoteCmd)

If As400.IsConnected(cwbcoServiceEnum.cwbcoServiceRemoteCmd) = 1 Then
    MsgBox("Valid Connection")
Else
    MsgBox("Invalid Connection")
    Exit Sub
End If

'-----------------------------------------------------------------------------------------
'Trying to figure out first if this syntax is correct, and if so... where/how to call it??
'-----------------------------------------------------------------------------------------
strQuery = "SELECT * FROM Library.File WHERE FILEFIELD='Criteria'"
' ---

AsProgram.LibraryName = ""
AsProgram.ProgramName = "" '?
AsProgram.system = As400

'Assuming this will end up being a program call?
'AsProgram.Call()

As400.Disconnect(cwbcoServiceEnum.cwbcoServiceRemoteCmd)

I'm very open to any other methods/suggestions. I've looked for guides from IBM's site as well as MSDN and neither actually do direct querying without the use of an As400 program file. Is this even possible?


Solution

  • Here is a simple console app that will retrieve all the records from one table and display the row count.

    Imports System.Data.OleDb
    
    Module Module1
    
      Sub Main()
        Dim cmd As New OleDbCommand
        Dim table As String = "YOUR TABLE"
        cmd.CommandText = "SELECT * FROM " & table
        Dim ip As String = "YOUR AS/400 IP GOES HERE"
        Dim user As String = "YOUR USER ID"
        Dim pass As String = "YOUR PASSWORD"
        Dim defaultLib As String = "YOUR LIBRARY"
        Dim connstring As String = "Provider=IBMDA400;" & _
                                   "Data Source=" & ip & ";" & _
                                   "Force Translate=0;" & _
                                   "Default Collection=" & defaultLib & ";" & _
                                   "User ID=" & user & ";" & _
                                   "Password=" & pass
        cmd.Connection = New OleDbConnection(connstring)
        cmd.Connection.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        Dim dt As New DataTable
        dt.Load(dr)
    
        Console.WriteLine(dt.Rows.Count)
        Console.WriteLine("Press ENTER to close...")
        Console.ReadLine()
      End Sub
    
    End Module