2.2.4.46.97 Worksheet::WriteRecordset

Description

Write data from worksheet into recordset.

Syntax

int WriteRecordset( Object & object, DWORD dwCntrl = LAYWKSETRECORDSET_REPLACE, int nRowBegin = 0, int nNumRows = -1, int nColBegin = 0, int nNumCols = -1 )

Parameters

object
[input] The recordset.
dwCntrl
[input] Options replace recordset or append data to it. default is LAYWKSETRECORDSET_REPLACE
nRowBegin
[input] The starting row in the worksheet. default is 0.
nNumRows
[input] Total number of worksheet to write recordset. default is -1.
nColBegin
[input] The starting in worksheet from which to begin. default is 0.
nNumCols
[input] The number of columns to use, beginning with nColBegin, or -1 to use all columns to the right of nColBegin.

Return

error message, 0 if no error

Examples

EX1

#define DB_SETUP_STR            "Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source="
#define DB_FILE_FULLPATH        GetAppPath(TRUE) + "Samples\Import and Export\stars.mdb"
#define TABLE_NAME                 "Stars"
 
// when bReplace is true, it will delete all entry firstly then copy data from current worksheet to table.
// NOTE if you wants to use append, and there is a Key field which not allow duplicate data, then you have to make sure after appending no duplicate data in key field/column. 
int  WriteRecordset_ex1(bool bReplace = TRUE)
{
    Worksheet wks = Project.ActiveLayer();
    if(!wks)
    {
        // assume use active wks
        out_str("NO active wks!");
        return -1;
    }
 
    Object ocrs;
    ocrs = CreateObject("ADODB.Recordset");
    if( !ocrs )
    {
        out_str(" ADO init error!");
        return -1;
    }
 
    // Get Access file name
    string strDBFileName = DB_FILE_FULLPATH;
    if ( !strDBFileName.IsFile() )
    {
        out_str("Invalid database file path!");
        return -1; 
    }
 
    // prepare and open database recordset
    string strConn = DB_SETUP_STR + strDBFileName; 
    ocrs.CursorLocation = 3;
    string strQuery = "Select * From "+ TABLE_NAME  + ";";
    ocrs.open( strQuery, strConn, 0, 3);
 
    int nOption = LAYWKSETRECORDSET_APPEND;    
    if ( bReplace )
        nOption = LAYWKSETRECORDSET_REPLACE;
 
    //write worksheet data to recordset.
    int nRet =  wks.WriteRecordset(ocrs,nOption );
    out_int("nRet = ", nRet);
    if ( nRet )
        return nRet;
    if (ocrs.State == 1 ) //adStateOpen
         ocrs.Close();
    return 0;
}

Remark

See Also

Worksheet::ReadRecordset, Worksheet::PutRecordset

Header to Include

origin.h