473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to connect to DB2 database using VB.NET

VietPP
17
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!
Jul 6 '09 #1
8 22582
VietPP
17
Helloooo. Anybody home?
Please, help! :(
Jul 7 '09 #2
PRR
750 Expert 512MB
Hi VietPP,
You could use ODBC.
Check this link
Connect to DB2 from Microsoft .NET

PRR
Jul 7 '09 #3
VietPP
17
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!
Jul 7 '09 #4
MrMancunian
569 Expert 512MB
Perhaps that this can help you?

Steven
Jul 9 '09 #5
VietPP
17
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.
Jul 10 '09 #6
payork
2
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()
Jul 17 '09 #7
VietPP
17
Thank you! I'll try it.
Jul 17 '09 #8
Hi VietPP
Did you fix this problem?
Sep 18 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Kate Perry | last post by:
I am trying to execute a query on a db2 database using the dbi module. I'm wondering if someone can take a look at my code and tell me what I'm doing wrong. I'm assuming it's a problem with my...
6
by: Jeff Sandler | last post by:
I have a database I created in mySQL. I've been entering data every day into the database using a Java application that I wrote. The database and the Java program are on the same Win 98 SE...
5
by: Maurice LING | last post by:
Hi, I've been using FB1.5 and access the database using Kinterbasdb + Python. My connection is established using kinterbasdb.connect() method and the parameters host, dns, database, user,...
4
by: vee10 | last post by:
hi, I need help to connect to database(SQLSERVER2005) using javascript please help me
1
by: Claudia Fong | last post by:
Hello I'm trying to connect to a sql database using C# console application but I'm having trouble to connect.. can someone help me? // Create an empty SqlConnection object. using...
7
by: mahipalerasani | last post by:
I am trying to connect to the Access database for querying, inserting and updating the data using 'C' API's. If somebody has code to connect to Access Database using C API's can you you please...
8
by: menmysql | last post by:
i am not bale to solve this problem since two weeks i am trying to access records from mysql database using jsp. inside this jsp program i wrote all my JDBC code. it is working very nicely and...
3
by: arasub | last post by:
ep 20, 2007 11:25:57 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found...
0
by: moyoal | last post by:
Dear All, this is my first experience to connect forms and database using VPN / DSL from other location. we have client/server architectures, .FMX are available in 212.147.1.63 server. ...
0
by: akshalika | last post by:
I am new to Biztalk. In my project we need to connect oracle database and insert data into oracle table using BizTalk project. I use WCF Adapter pack(SP2). I create biztalk project then using Consume...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.