🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.database.ado
Article #5263

Re: How to execute a stored procedure multiple times using arrays?

#5263
From: Yuancai (Charlie) Ye
Date: Wed, 27 Jun 2001 18:46
126 lines
4650 bytes
Hi,
    You can use a parameterized statement or stored procedure to add bulk
records with a single call (net trip). For details, see the examples
MultiProcs and OleDBExcel without involvement of a rowset at the site
http://www.geocities.com/oledbpro. you can't see the method from ADO or
ADOExpress. See the following code.

#define DBINITCONSTANTS
#include "OleDB25.h"
#include <iostream.h>

const LPOLESTR strMakeDept=OLESTR("Create Table Dept(DeptNo short, Name
char(50))");
const LPOLESTR strMakeEmpInfo=OLESTR("CREATE TABLE EmpInfo (EmpID Integer,
LastName CHAR(25), HireDate DATETIME)");
const LPOLESTR strInsert=L"Insert into Dept values(?, ?)";

void main()
{
 CoInitialize(NULL);
 {
  USES_CONVERSION;
  CODataSource  ODataSource;
  COSession   OSession;
  CBatchParam<CParamBase>   BatchParam;
  CPInfo    pPInfo[2];
  CPData    pPData[6];
  short    pnDeptNo[3]={1,2,3};

  long    *pnDept=NULL;
  char    *strDeptName=NULL;
  long    nIndex=0;

  DeleteFile(_T("xlsTest.xls"));
  ODataSource.SetProviderString(L"Excel 8.0;");
  ODataSource.SetServer(L"xlsTest");

  //create an excel file
  if(!ODataSource.Open(L"Provider=Microsoft.Jet.OLEDB.4.0;"))
  {
   cout<<"Error information: "<<W2T(ODataSource.m_strError)<<endl;
   return;
  }

  if(!OSession.Open(ODataSource.m_pIDBInitialize))
  {
   cout<<"Error information: "<<W2T(OSession.m_strError)<<endl;
   return;
  }

  //Begin two-phase transaction
  if(!OSession.BeginTrans())
  {
   cout<<"Error Information: "<<W2T(OSession.m_strError)<<"  ";
   return;
  }

  //The following build two tables and define fields
  if(!OSession.ExecuteSQL(strMakeDept))
   goto ROLLBACK;
  if(!OSession.ExecuteSQL(strMakeEmpInfo))
   goto ROLLBACK;

  //if successful, commit the above transaction
  OSession.Commit();

  //it is a must to set a data type for each of parameters
  pPInfo[0].m_nDBType=DBTYPE_I2;
  pPInfo[1].m_nDBType=DBTYPE_STR;

  if(!BatchParam.Open(OSession.m_pIOpenRowset, strInsert, pPInfo))
  {
   cout<<"Error Information: "<<W2T(BatchParam.m_strError)<<"  ";
   return;
  }

  //Prepare an array of parameter data
  pPData[0].m_pData=&(pnDeptNo[0]);
  pPData[2].m_pData=&(pnDeptNo[1]);
  pPData[4].m_pData=&(pnDeptNo[2]);

  pPData[1].m_pData="Sales Dept";
  pPData[3].m_pData="Software Development";
  pPData[5].m_pData="Admin Dept";

  //set 3 sets of parameter data into a data source by a single call at the
fastest speed! Reduce the number of trips
  if(!BatchParam.DoBatch(6, pPData))
  {
   cout<<"Error Information: "<<W2T(BatchParam.m_strError)<<"  ";
   return;
  }
    return;

ROLLBACK:
  OSession.Rollback();
}
 CoUninitialize();
}

    The code can't be simpler, faster and more elegant!

    Cheers!!!

--
Yuancai (Charlie) Ye
See 28 OLEDB examples at http://www.geocities.com/oledbpro


"Changsong Bu" <cbu@idax.com> wrote in message news:3b39dc7d$1_1@dnews...
> I wonder whether you can execute a stored procedure multiple times by
using
> arrays of parameters? The procedure will be very simple, just an insert
and
> error checking. However it needs to be called very frequently, like once
> every second. I hope to be able to call it once every 15 minutes,
supplying
> all the data in an array. In SQL Server books on line they talk about
> column-wise parameter binding or row-wise parameter bing, which you can
use
> to execute a statement multiple times, in ODBC. But the syntax doesn't
work
> in C++ builder. Anyone has any idea about it? Thanks very much in advance.
> Changsong
>
>
Technical Details
Message-ID: <3b3a26cc$1_2@dnews>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed.google.com!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!sunqbc.risq.qc.ca!cpk-news-hub1.bbnplanet.com!news.gtei.net!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews
References: <3b39dc7d$1_1@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10698b,4239602cd947ba7e
X-Google-Attributes: gid10698b,public
X-Google-ArrivalTime: 2001-06-27 11:48:25 PST
Reply-To: "Yuancai \(Charlie\) Ye" <yekerui@yahoo.com>
From: "Yuancai \(Charlie\) Ye" <yekerui@yahoo.com>
Newsgroups: borland.public.cppbuilder.database.ado
References: <3b39dc7d$1_1@dnews>
Subject: Re: How to execute a stored procedure multiple times using arrays?
Date: Wed, 27 Jun 2001 14:46:13 -0400
Lines: 128
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
NNTP-Posting-Host: 209.134.172.51
Message-ID: <3b3a26cc$1_2@dnews>
X-Trace: dnews 993666764 209.134.172.51 (27 Jun 2001 11:32:44 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed.google.com!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!sunqbc.risq.qc.ca!cpk-news-hub1.bbnplanet.com!news.gtei.net!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews