129 lines
6.0 KiB
HTML
129 lines
6.0 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 C++ - Access and return data by a call to a stored procedure" />
|
|
<meta name="abstract" content="This example illustrates using Visual C++ to access and return data by a call to a stored procedure." />
|
|
<meta name="description" content="This example illustrates using Visual C++ to access and return data by a call to a stored procedure." />
|
|
<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="odbcviscex" />
|
|
<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 C++ - Access and return data by a call to a stored
|
|
procedure</title>
|
|
</head>
|
|
<body id="odbcviscex"><a name="odbcviscex"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Visual C++ - Access and return data by a call to a stored
|
|
procedure</h1>
|
|
<div><p>This example illustrates using Visual C++ to access and return
|
|
data by a call to a stored procedure.</p>
|
|
<div class="section"><p>Only the code relevant to the stored procedure call has been included
|
|
here. This code assumes the connection has already been established. 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="section"><h4 class="sectiontitle">Creating the stored procedure</h4><pre> //* Drop the old Procedure
|
|
strcpy(szDropProc,"drop procedure apilib.partqry2");
|
|
|
|
rc = SQLExecDirect(m_hstmt, (unsigned char *)szDropProc, SQL_NTS);
|
|
|
|
// This statement is used to create a stored procedure
|
|
// Unless the
|
|
// procedure is destroyed, this statement need never be re-created
|
|
strcpy(szCreateProc,"CREATE PROCEDURE APILIB.PARTQRY2 (INOUT P1 INTEGER," );
|
|
strcat(szCreateProc,"INOUT P2 INTEGER)");
|
|
strcat(szCreateProc,"EXTERNAL NAME APILIB.SPROC2 LANGUAGE RPG GENERAL")
|
|
|
|
//' Create the new Procedure
|
|
rc = SQLExecDirect(m_hstmt, (unsigned char *)szCreateProc, SQL_NTS);
|
|
if (rc != SQL_SUCCESS &&; rc != SQL_SUCCESS_WITH_INFO) {
|
|
DspSQLError(m_henv, m_hdbc, SQL_NULL_HSTMT);
|
|
return APIS_INIT_ERROR;
|
|
}
|
|
if(rc != SQL_SUCCESS) {
|
|
DspSQLError(m_henv, m_hdbc, SQL_NULL_HSTMT);
|
|
return APIS_INIT_ERROR;
|
|
}
|
|
</pre>
|
|
</div>
|
|
<div class="example"><h4 class="sectiontitle">Preparing the statements</h4><pre>// Prepare the procedure call
|
|
strcpy(szStoredProc, "call partqry2(?, ?)");
|
|
// Prepare the stored procedure statement
|
|
rc = SQLPrepare(m_hstmt, (unsigned char *) szStoredProc, strlen(szStoredProc));
|
|
if(rc != SQL_SUCCESS &&; rc != SQL_SUCCESS_WITH_INFO) {
|
|
DspSQLError(m_henv, m_hdbc, m_hstmt);
|
|
return APIS_INIT_ERROR;
|
|
}</pre>
|
|
</div>
|
|
<div class="example"><h4 class="sectiontitle">Binding the parameters</h4><pre> // Bind the parameters for the stored procedure
|
|
|
|
rc = SQLBindParameter(m_hstmt, 1, SQL_PARAM_INPUT_OUTPUT, SQL_C_LONG,
|
|
SQL_INTEGER, sizeof(m_lOption), 0, &m_lOption, sizeof(m_lOption), &lcbon),
|
|
&lcbOption);
|
|
rc |= SQLBindParameter(m_hstmt, 2, SQL_PARAM_INPUT_OUTPUT, SQL_C_LONG,
|
|
SQL_INTEGER, sizeof(m_lPartNo), 0, &m_lPartNo, sizeof(m_lPartNo), &lcbon),
|
|
&lcbOption);
|
|
|
|
// Bind the Columns
|
|
rc = SQLBindCol(m_hstmt, 1, SQL_C_SLONG, &m_lSPartNo,
|
|
sizeof(m_lSPartNo), &lcbBuffer);
|
|
rc |= SQLBindCol(m_hstmt, 2, SQL_C_CHAR, &m_szSPartDesc,
|
|
26, &lcbBuffer);
|
|
rc |= SQLBindCol(m_hstmt, 3, SQL_C_SLONG, &m_lSPartQty,
|
|
sizeof(m_lSPartQty), &lcbBuffer);
|
|
rc |= SQLBindCol(m_hstmt, 4, SQL_C_DOUBLE, &m_dSPartPrice,
|
|
sizeof(m_dSPartPrice), &lcbBuffer);
|
|
rc |= SQLBindCol(m_hstmt, 5, SQL_C_DATE, &m_dsSPartDate,
|
|
10, &lcbBuffer);</pre>
|
|
</div>
|
|
<div class="example"><h4 class="sectiontitle">Calling the stored procedure</h4><pre> // Request a single record
|
|
m_lOption = ONE_RECORD;
|
|
m_lPartNo = PartNo;
|
|
|
|
// Run the stored procedure
|
|
rc = SQLExecute(m_hstmt);
|
|
if (rc != SQL_SUCCESS) {
|
|
DspSQLError(m_henv, m_hdbc, m_hstmt);
|
|
return APIS_SEND_ERROR;
|
|
}
|
|
|
|
// (Try to) fetch a record
|
|
rc = SQLFetch(m_hstmt);
|
|
if (rc == SQL_NO_DATA_FOUND) {
|
|
// Close the cursor for repeated processing
|
|
rc = SQLCloseCursor(m_hstmt);
|
|
return APIS_PART_NOT_FOUND;
|
|
}
|
|
else if (rc != SQL_SUCCESS) {
|
|
DspSQLError(m_henv, m_hdbc, m_hstmt);
|
|
return APIS_RECEIVE_ERROR;
|
|
}
|
|
|
|
// If we are still here we have some data, so map it back
|
|
// Format and display the data
|
|
.
|
|
.
|
|
.</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> |