Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

db2 connection string

Status
Not open for further replies.

handle1969

Programmer
Mar 9, 2006
2
IT
Hello,
I am very new to db2 and visual basic .net.
I have installed db2 in my local machine.
I have installed .net

All that I need is to know the correct components to be used and the correct connection string.

Thanks a lot in advance.
Ladis.
 
There are many different ways to connect. Here is an example of an oledb connection string for db2 v8.2.
Code:
Provider=IBMDADB2.1;Mode=ReadWrite;User ID=prod;Data Source=PRODDB;Persist Security Info=True;Password=mypassword
You will need to modify user id, password, and Data Source to match your database.
 
I noticed in your other thread that you want to use odbc.
Code:
ODBC;DSN=PRODDB;UID=prod;PWD=mypassword;MODE=SHARE;
You will need to plug in your own values for DSN, UID, and PWD.
 
I don't know vb.net, but I can give you a c# example. The vb.net version should be similar.

Code:
using System.Data.Odbc;
Code:
OdbcConnection cn = new OdbcConnection();
cn.ConnectionString = "DSN=PRODDB;UID=prod;PWD=mypassword;MODE=SHARE;";
cn.Open();
Would anyone care to translate this into vb.net?
 
Ladis,
Follows a piece of code that I used in an Access db to connect to a DB2 table via ODBC. Code is VBA, so might be close to what you require. You'll need to insert your own SQL, and also to set up the variables with values applicable for your system.
Code:
Sub call_initial_signon_PSR()
  On Error GoTo error_exit

' ====================================================================================================
   ' Build a query to check the user is allowed to use LAN PSR DB
   ' ====================================================================================================
    Dim db As Database
    Dim qdf As QueryDef
    Dim strSQL As String
    Set db = CurrentDb()
    Set qdf = db.CreateQueryDef("")
  ' here follows the OBDC connection string to hook up to DB2
    qdf.Connect = "ODBC;DSN=" & strPSRDSN & ";DBALIAS=" & strPSRDBAlias & ";UID=" & strUserID & ";PWD=" & strLANPassword & ";"
  ' set timeout equal to mainframe timeout
    qdf.ODBCTimeout = 600
  ' Build the SQL into the query defined as a dynamic query above
    strSQL = "SELECT COUNT(*) "
    strSQL = strSQL & "FROM " & strPSRTableID & ".TSPRSEMP "
    qdf.SQL = strSQL
  ' run the query and store the result
    Dim rstUPSRdets As Recordset
    
    Set rstUPSRdets = qdf.OpenRecordset()
    
    DoCmd.Hourglass False
    If rstUPSRdets.RecordCount = 0 Then
       MsgBox "You are not authorised to use this system"
       DoCmd.Quit
    End If

  Exit Sub
error_exit:
  Beep
  DoCmd.Hourglass False
  MsgBox "Unable to connect to the PSR System - You may have mis-typed your LAN password. Please try again or report this error message to the help desk@@" & ERR.Number & " : " & ERR.Description, vbCritical
  strErrorFound = "Y"
  Exit Sub
    
End Sub

Hope this helps

Marc
 
Marc,

What does DBALIAS do? I've seen it in examples, but I've never used it.

- Dan
 
Dan,

I'm at work at the moment, and the example I cribbed from is at home, so I'm going to have to go on memory here.....

I think it was a long name that described the connection in more detail. We used to have various DB2 subsystems and connections that related to Prod, systest, integration test, development etc. etc, and these were all defined as DB2A, DB2B, DB2C etc. The DBALIAS gave them more of a recognisable tag when choosing them. I have a feeling that it is an optional parameter, but we coded it as a belt and braces in order to make sure we were not accidently running our developement queries against production!

Marc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top