473,699 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to connect to DB2 database using VB.NET

VietPP
17 New Member
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.Connecti onString.
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 22687
VietPP
17 New Member
Helloooo. Anybody home?
Please, help! :(
Jul 7 '09 #2
PRR
750 Recognized Expert Contributor
Hi VietPP,
You could use ODBC.
Check this link
Connect to DB2 from Microsoft .NET

PRR
Jul 7 '09 #3
VietPP
17 New Member
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)

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 Recognized Expert Contributor
Perhaps that this can help you?

Steven
Jul 9 '09 #5
VietPP
17 New Member
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.
Jul 10 '09 #6
payork
2 New Member
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()
Jul 17 '09 #7
VietPP
17 New Member
Thank you! I'll try it.
Jul 17 '09 #8
sajithfx
1 New Member
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
4635
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 query, but my query works fine in access, so I don't know. I guess I don't really understand how queries work differently in db2. I typically connect to a sql database. Here is the error I'm getting: Couldn't execute query: SQL0204N "(my userid...
6
4138
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 machine. I've been working on another program (an applet) to send and receive data over the internet. I got the applet to connect with the database and receive data. All the while I was developing the applet, I would tell it to find the database at...
5
4593
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, password are all defaulted to 'None'. On my own machine running Mac OSX 10.3, I can connect using the following: host = 'localhost' database = '<my path to FB database>'
4
5116
by: vee10 | last post by:
hi, I need help to connect to database(SQLSERVER2005) using javascript please help me
1
25628
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 (SqlConnection con = new SqlConnection()) { // Configure the SqlConnection object's connection string.
7
10448
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 paste it here. Thanks in advance.
8
4652
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 displaying records. now i wrote all the JDBC code in .java and i am accessing that code in jsp file. but this time i am getting only exceptions not records. i am keeping my programs here. please crosscheck once and tell where i am wrong code ...
3
2910
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 on the java.library.path: C:\Program Files\Java\jdk1.5.0_12\bin;.;C:\WINDOWS\System32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\j2sdk_nb\j2sdk1.4.2\bin;C:\Program...
0
1460
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. Database is installed in saperate 212.147.1.62 server. Each client has its own forms runtime and reports runtime, reading fmx from 212.147.1.63 server.
0
2729
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 Adapter Service Add-in create schemas to insert data into oracle database and tow-way send port binding file Then create orchestration and logical receive port send port, Request-response port then after correctly connect those port with message...
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9196
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8896
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.