Connecting Tech Pros Worldwide Forums | Help | Site Map

How to connect to DB2 database using VB.NET

VietPP's Avatar
Newbie
 
Join Date: Dec 2007
Location: Vietnam
Posts: 17
#1: Jul 6 '09
Hi all,

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

Expand|Select|Wrap|Line Numbers
  1. Imports IBM.Data.DB2
  2.  
  3. Module mdMAIN
  4.  
  5.     Public db2CNN As New DB2Connection
  6.  
  7.     Public strSRV As String = "xxx.xxx.xxx.xxx"
  8.     Public strUSR As String = "xxx"
  9.     Public strPASS As String = "xxx"
  10.  
  11.     Public Sub getCNN()
  12.         db2CNN.ConnectionString = "Provider=IBMDA400.DataSource.1;" _
  13.                                     & "Persist Security Info=False;" _
  14.                                     & "User ID=" & strUSR & ";" _
  15.                                     & "password=" & strPASS & ";" _
  16.                                     & "Data Source=" & strSRV & ";"
  17.         db2CNN.Open()
  18.     End Sub
  19.  
  20. End Module
  21.  
And I get the error at db2CNN.ConnectionString.
The error message is "Invalid argument"

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

Thank you so much!

VietPP's Avatar
Newbie
 
Join Date: Dec 2007
Location: Vietnam
Posts: 17
#2: Jul 7 '09

re: How to connect to DB2 database using VB.NET


Helloooo. Anybody home?
Please, help! :(
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Posts: 717
#3: Jul 7 '09

re: How to connect to DB2 database using VB.NET


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

PRR
VietPP's Avatar
Newbie
 
Join Date: Dec 2007
Location: Vietnam
Posts: 17
#4: Jul 7 '09

re: How to connect to DB2 database using VB.NET


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 connectionString.

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)

Expand|Select|Wrap|Line Numbers
  1. Imports IBM.Data.DB2
  2. Imports Microsoft.VisualBasic
  3. Imports System
  4. Imports System.Data
  5.  
  6. Module Module1
  7.  
  8.     Public Sub Main()
  9.  
  10.         Dim dbsrv As String
  11.         Dim dbname As String
  12.         Dim pwd As String
  13.         Dim uid As String
  14.  
  15.         Dim cmd As DB2Command
  16.         Dim con As DB2Connection
  17.         Dim connectString As String
  18.         Dim i As Integer
  19.         Dim rdr As DB2DataReader
  20.         Dim strMsg As String
  21.         Dim v_IBMREQD As String = ""
  22.  
  23.         dbsrv = "xxx.xxx.xxx.xxx"
  24.         dbname = "myDB"
  25.         uid = "myUSR"
  26.         pwd = "myPASS"
  27.  
  28.         connectString = "Database=" + dbname + ";UID=" + uid + ";PWD=" + pwd + ";"
  29.         connectString += "Provider='IBMDADB2'"
  30.         connectString += "Driver={IBM DB2 ODBC DRIVER};"
  31.         connectString += "Server=" + dbsrv + ";"
  32.         connectString += "Connect Timeout=60;"
  33.         connectString += "Pooling=true;"
  34.         connectString += "Min Pool Size=0;"
  35.         connectString += "Max Pool Size=12;"
  36.         connectString += "Connection Reset=true;"
  37.         connectString += "Connection Lifetime=15;"
  38.  
  39.         Try
  40.             'initialize connection object
  41.             con = New DB2Connection(connectString)
  42.  
  43.             'initialize command object
  44.             cmd = New DB2Command()
  45.             'set up command object properties
  46.             cmd.Connection = con
  47.             cmd.CommandText = "SELECT * FROM SYSIBM.SYSDUMMY1"
  48.             cmd.CommandTimeout = 20
  49.  
  50.             'open a new connection
  51.             con.Open()
  52.             strMsg = "  Successful connection to '" + dbname + "' db using IBM DB2 .NET Data Provider"
  53.             Console.WriteLine(strMsg)
  54.  
  55.             'get info about the connection
  56.             strMsg = vbTab + "Database: " + con.Database.ToString() + vbNewLine _
  57.                     + vbTab + "ServerType: " + con.ServerType + vbNewLine _
  58.                     + vbTab + "ServerVersion: " + con.ServerVersion.ToString()
  59.             Console.WriteLine(strMsg)
  60.  
  61.             'execute the SQL statement
  62.             rdr = cmd.ExecuteReader(CommandBehavior.SingleResult)
  63.  
  64.             'get value from retrieved records
  65.             While rdr.Read()
  66.                 v_IBMREQD = rdr.GetString(0)
  67.             End While
  68.             strMsg = "  Successful retrieval of record.  Column 'IBMREQD' has a value of '" + v_IBMREQD + "'"
  69.             Console.WriteLine(strMsg)
  70.  
  71.             'close the reader
  72.             rdr.Close()
  73.  
  74.             'close the connection object
  75.             con.Close()
  76.             strMsg = "  Successful Disconnection from database '" + dbname + "'"
  77.             Console.WriteLine(strMsg)
  78.  
  79.         Catch myException As DB2Exception
  80.             For i = 0 To myException.Errors.Count - 1
  81.                 strMsg = "Index " + i.ToString() + ControlChars.Cr _
  82.                    + "Message: " + myException.Errors(i).Message + ControlChars.Cr _
  83.                    + "Native: " + myException.Errors(i).NativeError.ToString() + ControlChars.Cr _
  84.                    + "Source: " + myException.Errors(i).Source + ControlChars.Cr _
  85.                    + "SQL: " + myException.Errors(i).SQLState + ControlChars.Cr
  86.                 Console.WriteLine(strMsg)
  87.             Next i
  88.         Catch e As Exception
  89.             Console.WriteLine(e.Message)
  90.         Finally
  91.             'close the reader
  92.             If Not (rdr Is Nothing) Then
  93.                 If rdr.IsClosed() = False Then rdr.Close()
  94.             End If
  95.             'close the connection
  96.             If Not (con Is Nothing) Then
  97.                 If con.State <> ConnectionState.Closed Then con.Close()
  98.             End If
  99.         End Try
  100.  
  101.         'to stop the console window from closing too quickly
  102.         Console.WriteLine("Press ENTER to exit...")
  103.         i = Console.Read()
  104.     End Sub ' Main
  105.  
  106. End Module
  107.  
Thank you!
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 311
#5: Jul 9 '09

re: How to connect to DB2 database using VB.NET


Perhaps that this can help you?

Steven
VietPP's Avatar
Newbie
 
Join Date: Dec 2007
Location: Vietnam
Posts: 17
#6: Jul 10 '09

re: How to connect to DB2 database using VB.NET


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 ConnectionString. Could you just give me some infos, like for the environment, which programe or lib I have to install first.
Newbie
 
Join Date: Jul 2009
Posts: 2
#7: Jul 17 '09

re: How to connect to DB2 database using VB.NET


private _dbConnectionIBM As IBM.Data.DB2.iSeries.iDB2Connection

.....

_dbConnectionIBM = New IBM.Data.DB2.iSeries.iDB2Connection

_dbConnectionIBM.ConnectionString = "DataSource=<your iSeriesIPAddress>;DefaultCollection=<yourLibName>; User Id=<userid>;Password=<password>;"
_dbConnectionIBM.Open()
VietPP's Avatar
Newbie
 
Join Date: Dec 2007
Location: Vietnam
Posts: 17
#8: Jul 17 '09

re: How to connect to DB2 database using VB.NET


Thank you! I'll try it.
Reply