'****************************************************************************
' (c) Copyright IBM Corp. 2007 All rights reserved.
'
' The following sample of source code ("Sample") is owned by International
' Business Machines Corporation or one of its subsidiaries ("IBM") and is
' copyrighted and licensed, not sold. You may use, copy, modify, and
' distribute the Sample in any form without payment to IBM, for the purpose of
' assisting you in the development of your applications.
'
' The Sample code is provided to you on an "AS IS" basis, without warranty of
' any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
' not allow for the exclusion or limitation of implied warranties, so the above
' limitations or exclusions may not apply to you. IBM shall not be liable for
' any damages you suffer as a result of using, copying, modifying or
' distributing the Sample, even if IBM has been advised of the possibility of
' such damages.
'****************************************************************************
'
' SOURCE FILE NAME: SpReturn.vb
'
' SAMPLE: How to call a stored procedure that has a return value
' with the DB2 .Net Data Provider
'
' DB2 .NET Data Provider Classes USED:
' DB2Connection
' DB2Command
' DB2Transaction
'
'
'****************************************************************************
'
' Building and Running the sample program
'
' 1. Catalog the stored procedure Emp_Details in the CLP script file
' EmpDetails.db2 by entering the following at the command prompt:
'
' empcat
'
' 2. Compile the SpReturn.vb file with bldapp.bat by entering the following
' at the command prompt:
'
' bldapp spReturn
'
' or compile SpReturn.vb with the makefile by entering the following at
' the command prompt:
'
' nmake SpReturn
'
' 3. Run the SpReturn program by entering the program name at the command
' prompt:
'
' SpReturn
'
'****************************************************************************
'
' For more information on the sample programs, see the README file.
'
' For information on developing applications, see the Application
' Development Guide.
'
' For information on using SQL statements, see the SQL Reference.
'
' For the latest information on programming, compiling, and running DB2
' applications, visit the DB2 Information Center at
' http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
'
'****************************************************************************
Imports System
Imports System.Data
Imports IBM.Data.DB2
Imports Microsoft.VisualBasic
Public Class SpReturn
Public Shared Sub Main(args() As String)
' Declare a DB2Connection and a DB2Transaction
Dim conn As DB2Connection
Dim trans As DB2Transaction
Try
Console.WriteLine( _
vbNewLine & " THIS SAMPLE DEMONSTRATES HOW TO CALL A STORED" & _
" PROCEDURE THAT" & vbNewLine & " HAS A RETURN VALUE")
Console.WriteLine()
' Connect to a database
Console.WriteLine(" Connecting to a database ...")
conn = ConnectDb(args)
' Obtain and display the details of the employee with
' ID = 310 from table 'STAFF'
trans = conn.BeginTransaction()
CallEmpDetails(conn, trans, 310)
' Disconnect from the database
Console.WriteLine(vbNewLine & _
" Disconnect from the database")
conn.Close()
Catch e As Exception
Try
conn.Close()
Catch x As Exception
Console.WriteLine(x.Message)
End Try
Console.WriteLine(e.Message)
End Try
End Sub ' Main
' This method establishes a connection to a database
Public Shared Function ConnectDb(argv() As String) As DB2Connection
Dim server As String
Dim dbalias As String
Dim userId As String
Dim password As String
Dim portNumber As Int32 = -1
Dim connectString As String
If (argv.Length > 5) Then
Throw new Exception( _
"Usage: prog_name [dbAlias] [userId passwd]" & vbNewLine & _
" prog_name [dbAlias] server portNum userId passwd")
Else
If (argv.Length = 1) Then
If( String.Compare(argv(0),"?") = 0 Or _
String.Compare(argv(0),"-?") = 0 Or _
String.Compare(argv(0),"/?") = 0 Or _
String.Compare(argv(0),"-h",true) = 0 Or _
String.Compare(argv(0),"/h",true) = 0 Or _
String.Compare(argv(0),"-help",true) = 0 Or _
String.Compare(argv(0),"/help",true) = 0 ) Then
Throw new Exception( _
"Usage: prog_name [dbAlias] [userId passwd]" & vbNewLine & _
" prog_name [dbAlias] server portNum userId passwd")
End If
End If
End If
Select Case (argv.Length)
Case 0 ' Use all defaults
dbalias = "sample"
userId = ""
password = ""
Case 1 ' dbAlias specified
dbalias = argv(0)
userId = ""
password = ""
Case 2 ' userId & passwd specified
dbalias = "sample"
userId = argv(0)
password = argv(1)
Case 3 ' dbAlias, userId & passwd specified
dbalias = argv(0)
userId = argv(1)
password = argv(2)
Case 4 ' use default dbAlias
dbalias = "sample"
server = argv(0)
portNumber = Convert.ToInt32(argv(1))
userId = argv(2)
password = argv(3)
Case 5 ' everything specified
dbalias = argv(0)
server = argv(1)
portNumber = Convert.ToInt32(argv(2))
userId = argv(3)
password = argv(4)
End Select
If(portNumber = -1) Then
connectString = "Database=" & dbalias
Else
connectString = "Server=" & server & ":" & portNumber & _
";Database=" & dbalias
End If
If (userId <> "")
connectString += ";UID=" & userId & ";PWD=" & password
End If
Dim conn As DB2Connection = new DB2Connection(connectString)
conn.Open()
Console.WriteLine(" Connected to the " & dbalias & " database")
Return conn
End Function ' ConnectDb
' This method obtains and displays the details of the employee with
' ID = empId from table 'STAFF'
Public Shared Sub CallEmpDetails(conn As DB2Connection, _
trans As DB2Transaction, _
empId As Int16)
Try
' Create a DB2Command to run the stored procedure EMP_DETAILS
Dim procName As String = "EMP_DETAILS"
Dim cmd As DB2Command = conn.CreateCommand()
cmd.Transaction = trans
cmd.CommandText = procName
cmd.CommandType = CommandType.StoredProcedure
' Register output parameters for the DB2Command
' Register parameter '@empyears' to store the return value
Dim parm As DB2Parameter = cmd.Parameters.Add("@empyears", _
DB2Type.Integer)
parm.Direction = ParameterDirection.ReturnValue
parm = cmd.Parameters.Add("@empid", DB2Type.SmallInt)
parm.Direction = ParameterDirection.Input
parm.Value = empId
parm = cmd.Parameters.Add("@empsalary", DB2Type.Double)
parm.Direction = ParameterDirection.Output
parm = cmd.Parameters.Add("@empname", DB2Type.VarChar,9)
parm.Direction = ParameterDirection.Output
parm = cmd.Parameters.Add("@empjob", DB2Type.Char,5)
parm.Direction = ParameterDirection.Output
' Call the stored procedure
Console.WriteLine()
Console.WriteLine(" Call stored procedure named " & _
procName & _
vbNewLine)
cmd.ExecuteNonQuery()
' Retrieve output parameters
Dim salary As Double = CType(cmd.Parameters("@empsalary").Value, _
Double)
Dim years As Int32 = CType(cmd.Parameters("@empyears").Value, Int32)
Dim name As String = CType(cmd.Parameters("@empname").Value, String)
Dim job As String = CType(cmd.Parameters("@empjob").Value, String)
' Display details of the employee
Console.WriteLine(" Details of Employee Id = " & empId)
Console.WriteLine(" _____________________________________" & _
vbNewLine)
Console.WriteLine(" Employee Name : " & name)
Console.WriteLine(" Employee Job : " & job)
Console.WriteLine(" Employee Salary : " & _
String.Format("{0:f2}",salary))
Console.WriteLine(" Years Served : " & years)
trans.Rollback()
Catch e As Exception
trans.Rollback()
Console.WriteLine(e)
End Try
End Sub ' CallEmpDetails
End Class ' SpReturn