How to connect to DB2 database using VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VietPP
    New Member
    • Dec 2007
    • 17

    How to connect to DB2 database using VB.NET

    Hi all,

    I trying IBM DB2 Database. I want to connect to DB2 database with these code:

    Code:
    Imports IBM.Data.DB2
    
    Module mdMAIN
    
        Public db2CNN As New DB2Connection
    
        Public strSRV As String = "xxx.xxx.xxx.xxx"
        Public strUSR As String = "xxx"
        Public strPASS As String = "xxx"
    
        Public Sub getCNN()
            [B]db2CNN.ConnectionString[/B] = "Provider=IBMDA400.DataSource.1;" _
                                        & "Persist Security Info=False;" _
                                        & "User ID=" & strUSR & ";" _
                                        & "password=" & strPASS & ";" _
                                        & "Data Source=" & strSRV & ";"
            db2CNN.Open()
        End Sub
    
    End Module
    And I get the error at db2CNN.Connecti onString.
    The error message is "Invalid argument"

    Could you just help me explain and show me some small samples.

    Thank you so much!
  • VietPP
    New Member
    • Dec 2007
    • 17

    #2
    Helloooo. Anybody home?
    Please, help! :(

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      Hi VietPP,
      You could use ODBC.
      Check this link
      Connect to DB2 from Microsoft .NET

      PRR

      Comment

      • VietPP
        New Member
        • Dec 2007
        • 17

        #4
        thank you so much but...

        Thank you so much for the reply. I've got a sample from the link you gave but it still return the same error message: "Invalid argument" when I call the connectionStrin g.

        Here the code it is. Please help me to fix the problem:
        - IBM Data Server Driver for ODBC, CLI, .NET - DSDRIVER32 (installed)
        - VS Add-Ins for DB2 9.5.2 (installed)

        Code:
        Imports IBM.Data.DB2
        Imports Microsoft.VisualBasic
        Imports System
        Imports System.Data
        
        Module Module1
        
            Public Sub Main()
        
                Dim dbsrv As String
                Dim dbname As String
                Dim pwd As String
                Dim uid As String
        
                Dim cmd As DB2Command
                Dim con As DB2Connection
                Dim connectString As String
                Dim i As Integer
                Dim rdr As DB2DataReader
                Dim strMsg As String
                Dim v_IBMREQD As String = ""
        
                dbsrv = "xxx.xxx.xxx.xxx"
                dbname = "myDB"
                uid = "myUSR"
                pwd = "myPASS"
        
                connectString = "Database=" + dbname + ";UID=" + uid + ";PWD=" + pwd + ";"
                connectString += "Provider='IBMDADB2'"
                connectString += "Driver={IBM DB2 ODBC DRIVER};"
                connectString += "Server=" + dbsrv + ";"
                connectString += "Connect Timeout=60;"
                connectString += "Pooling=true;"
                connectString += "Min Pool Size=0;"
                connectString += "Max Pool Size=12;"
                connectString += "Connection Reset=true;"
                connectString += "Connection Lifetime=15;"
        
                Try
                    'initialize connection object
                    con = New DB2Connection(connectString)
        
                    'initialize command object
                    cmd = New DB2Command()
                    'set up command object properties
                    cmd.Connection = con
                    cmd.CommandText = "SELECT * FROM SYSIBM.SYSDUMMY1"
                    cmd.CommandTimeout = 20
        
                    'open a new connection
                    con.Open()
                    strMsg = "  Successful connection to '" + dbname + "' db using IBM DB2 .NET Data Provider"
                    Console.WriteLine(strMsg)
        
                    'get info about the connection
                    strMsg = vbTab + "Database: " + con.Database.ToString() + vbNewLine _
                            + vbTab + "ServerType: " + con.ServerType + vbNewLine _
                            + vbTab + "ServerVersion: " + con.ServerVersion.ToString()
                    Console.WriteLine(strMsg)
        
                    'execute the SQL statement
                    rdr = cmd.ExecuteReader(CommandBehavior.SingleResult)
        
                    'get value from retrieved records
                    While rdr.Read()
                        v_IBMREQD = rdr.GetString(0)
                    End While
                    strMsg = "  Successful retrieval of record.  Column 'IBMREQD' has a value of '" + v_IBMREQD + "'"
                    Console.WriteLine(strMsg)
        
                    'close the reader
                    rdr.Close()
        
                    'close the connection object
                    con.Close()
                    strMsg = "  Successful Disconnection from database '" + dbname + "'"
                    Console.WriteLine(strMsg)
        
                Catch myException As DB2Exception
                    For i = 0 To myException.Errors.Count - 1
                        strMsg = "Index " + i.ToString() + ControlChars.Cr _
                           + "Message: " + myException.Errors(i).Message + ControlChars.Cr _
                           + "Native: " + myException.Errors(i).NativeError.ToString() + ControlChars.Cr _
                           + "Source: " + myException.Errors(i).Source + ControlChars.Cr _
                           + "SQL: " + myException.Errors(i).SQLState + ControlChars.Cr
                        Console.WriteLine(strMsg)
                    Next i
                Catch e As Exception
                    Console.WriteLine(e.Message)
                Finally
                    'close the reader
                    If Not (rdr Is Nothing) Then
                        If rdr.IsClosed() = False Then rdr.Close()
                    End If
                    'close the connection
                    If Not (con Is Nothing) Then
                        If con.State <> ConnectionState.Closed Then con.Close()
                    End If
                End Try
        
                'to stop the console window from closing too quickly
                Console.WriteLine("Press ENTER to exit...")
                i = Console.Read()
            End Sub ' Main
        
        End Module
        Thank you!

        Comment

        • MrMancunian
          Recognized Expert Contributor
          • Jul 2008
          • 569

          #5
          Perhaps that this can help you?

          Steven

          Comment

          • VietPP
            New Member
            • Dec 2007
            • 17

            #6
            Thank you! I also usually visit this website anytime I work with a new db. But this time, it's not help much. I've tried some model but the error message always is "Invalid argument", really dont know why.

            Now I'm trying to review all my step, may be the problem is not because of my ConnectionStrin g. Could you just give me some infos, like for the environment, which programe or lib I have to install first.

            Comment

            • payork
              New Member
              • Jul 2009
              • 2

              #7
              private _dbConnectionIB M As IBM.Data.DB2.iS eries.iDB2Conne ction

              .....

              _dbConnectionIB M = New IBM.Data.DB2.iS eries.iDB2Conne ction

              _dbConnectionIB M.ConnectionStr ing = "DataSource=<yo ur iSeriesIPAddres s>;DefaultColle ction=<yourLibN ame>;User Id=<userid>;Pas sword=<password >;"
              _dbConnectionIB M.Open()

              Comment

              • VietPP
                New Member
                • Dec 2007
                • 17

                #8
                Thank you! I'll try it.

                Comment

                • sajithfx
                  New Member
                  • Sep 2013
                  • 1

                  #9
                  Hi VietPP
                  Did you fix this problem?

                  Comment

                  Working...