ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaik_5.4.0.1/rzaikodbcvbex2.htm

136 lines
7.2 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Visual Basic - Access and return data by a call to a stored procedure" />
<meta name="abstract" content="Visual Basic is able to call external functions that are found in DLLs. Since all ODBC drivers are DLLs, Visual Basic can be used to code directly to the ODBC APIs. By coding directly to the ODBC APIs a Visual Basic application can call an iSeries server stored procedure and return result values." />
<meta name="description" content="Visual Basic is able to call external functions that are found in DLLs. Since all ODBC drivers are DLLs, Visual Basic can be used to code directly to the ODBC APIs. By coding directly to the ODBC APIs a Visual Basic application can call an iSeries server stored procedure and return result values." />
<meta name="DC.Relation" scheme="URI" content="rzaikodbcprogexamples.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="odbcvbex2" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Visual Basic - Access and return data by a call to a stored
procedure</title>
</head>
<body id="odbcvbex2"><a name="odbcvbex2"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Visual Basic - Access and return data by a call to a stored
procedure</h1>
<div><p>Visual Basic is able to call external functions that are found
in DLLs. Since all ODBC drivers are DLLs, Visual Basic can be used to code
directly to the ODBC APIs. By coding directly to the ODBC APIs a Visual Basic
application can call an iSeries™ server stored procedure and return result
values.</p>
<div class="section"><p>See <a href="rzaikdirectcodeodbcapis.htm#directcodeodbcapis">Code directly to ODBC APIs</a> for
more information. See <a href="rzaikodbcrpghostex.htm#odbcrpghostex">Examples: RPG - Host code for ODBC stored procedures</a> for
the source code for the stored procedure.</p>
</div>
<div class="example"><h4 class="sectiontitle">Creating the stored procedure</h4><pre> ' This statement will drop an existing stored procedure
szDropProc = "drop procedure apilib.partqry2"
'* This statement is used to create a stored procedure
'* Unless the
'* procedure is destroyed, this statement need never be re-created
szCreateProc = "CREATE PROCEDURE APILIB.PARTQRY2 (INOUT P1 INTEGER,"
szCreateProc = szCreateProc &amp; "INOUT P2 INTEGER)"
szCreateProc = szCreateProc &amp; "EXTERNAL NAME APILIB.SPROC2 LANGUAGE RPG GENERAL"
'* Allocate statement handle
rc = SQLAllocHandle(SQL_HANDLE_STMT, ghDbc, hStmt)
If rc &lt;&gt; SQL_SUCCESS Then
Call DisplayError(rc, "SQLAllocStmt failed.")
Call DspSQLError(henv, SQL_NULL_HDBC, SQL_NULL_HSTMT)
End If
'* Drop the old Procedure
rc = SQLExecDirect(hstmt, szDropProc, SQL_NTS)
' Create the new Procedure
rc = SQLExecDirect(hstmt, szCreateProc, SQL_NTS)
If rc &lt;&gt; SQL_SUCCESS And rc &lt;&gt; SQL_SUCCESS_WITH_INFO Then
Call DisplayError(rc, "SQLCreate failed.")
Call DspSQLError(henv, hdbc, hstmt)
End If</pre>
</div>
<div class="example"><h4 class="sectiontitle">Preparing the statements</h4><pre> '* This statement will be used to call the stored procedure
szStoredProc = "call partqry2(?, ?)"
'* Prepare the stored procedure call statement
rc = SQLPrepare(hstmt, szStoredProc, Len(szStoredProc))
If rc &lt;&gt; SQL_SUCCESS And rc &lt;&gt; SQL_SUCCESS_WITH_INFO Then
Call DisplayError(rc, "SQLPrepare failed.")
Call DspSQLError(henv, hdbc, hstmt)
End If</pre>
</div>
<div class="example"><h4 class="sectiontitle">Binding the parameters</h4><pre>
'Bind the parameters for the stored procedure
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, _
SQL_INTEGER, lLen1, 0, sFlag, lLen1, lCbValue)
If rc &lt;&gt; SQL_SUCCESS Then
Call DisplayError(rc, "Problem binding parameter ")
End If
rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG, _
SQL_INTEGER, 4, 0, lPartNumber, lLen2, lCbValue)
If rc &lt;&gt; SQL_SUCCESS Then
Call DisplayError(rc, "Problem binding parameter ")
End If</pre>
</div>
<div class="example"><h4 class="sectiontitle">Calling the stored procedure</h4><pre> rc = SQLExecute(hstmt)
If lRc &lt;&gt; SQL_SUCCESS Then
' Free the statement handle for repeated processing
rc = SQLFreeHandle(
Call DspSQLError(henv, hdbc, hstmt)
End If
rc = SQLFetch(hstmt)
If rc = SQL_NO_DATA_FOUND Then
mnuClear_Click 'Clear screen
txtPartNumber = lPartNumber 'Show the part number not found
Call DisplayMessage("RECORD NOT FOUND")
.
.
Else
'Get Description
rc = SQLGetData(hstmt, 2, SQL_C_CHAR, sSDescription, _
25, lcbBuffer)
'Get Quantity. SQLGetLongData uses alias SQLGetData
rc = SQLGetLongData(hstmt, 3, SQL_C_SLONG, lSQuantity, _
Len(lSQuantity), lcbBuffer)
'Get Price. SQLGetDoubleData uses alias SQLGetData
rc = SQLGetDoubleData(hstmt, 4, SQL_C_DOUBLE, dSPrice, _
Len(dSPrice), lcbBuffer)
'Get Received date
rc = SQLGetData(hstmt, 5, SQL_C_CHAR, sSReceivedDate, _
10, lcbBuffer)
txtDescription = sSDescription 'Show description
txtQuantity = lSQuantity 'Show quantity
txtPrice = Format(dSPrice, "currency") 'Convert dSPrice to
txtReceivedDate = CDate(sSReceivedDate) 'Convert string to d
Call DisplayMessage("Record found")
End If</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzaikodbcprogexamples.htm" title="The following ODBC programming examples demonstrate simple queries, and accessing and returning data by calling stored procedures. C/C++, Visual Basic and RPG programming language versions are provided.">ODBC program examples</a></div>
</div>
</div>
</body>
</html>