Binds data from a program variable to a table column for bulk copy into Microsoft® SQL Server™.
RETCODE bcp_bind (
PDBPROCESS dbproc,
LPCBYTE varaddr,
INT prefixlen,
DBINT varlen,
LPCBYTE terminator,
INT termlen,
INT type,
INT table_column );
For fixed-length data types, such as integers, the data type itself indicates to the system the length of the data. Therefore, for fixed-length data types, varlen must always be -1 except when the data is NULL, in which case varlen must be 0.
For character, text, binary, and image data, varlen can be -1, 0, or some positive value. If varlen is -1, the system uses either a length prefix or a terminator sequence to determine the length. (If both are supplied, the system uses the one that results in the shortest amount of data being copied.) If varlen is -1 and neither a prefix length nor a terminator sequence is specified, the system returns an error message. If varlen is 0, the system assumes the data is NULL. If varlen is some positive value, the system uses varlen as the data length. However, if, in addition to a positive varlen, a prefix length or terminator sequence is provided, the system determines the data length by using the method that results in the shortest amount of data being copied.
bcp_bind (dbproc, co_name, 0, -1, "", 1, 0, 2)
If there is no terminator:
bcp_bind (dbproc, co_name, 0, -1, NULL, 0, 0, 2)
SUCCEED or FAIL.
Use bcp_bind for a fast, efficient way to copy data from a program variable into a table in SQL Server without first putting the data into a user file or using the Transact-SQL INSERT statement.
Call bcp_init before calling this or any other bulk-copy functions.
Make a separate bcp_bind call for every column in the SQL Server table into which you want to copy. After the necessary bcp_bind calls have been made, then call bcp_sendrow to send a row of data from your program variables to SQL Server. Call bcp_init to set the table to be copied into.
Whenever you want SQL Server to checkpoint the rows already received, call bcp_batch. For example, call bcp_batch once for every 1000 rows inserted or at any other interval.
When there are no more rows to be inserted, call bcp_done. Failure to do so results in an error.
When using bcp_bind, the user file name parameter, set hfile, in the call to bcp_init, to NULL, and set direction, the direction parameter, to DB_IN.
Control parameter settings, specified with bcp_control, have no effect on bcp_bind row transfers.
Calling bcp_columns when using bcp_bind results in an error.
The following example shows how to use bcp_bind:
LOGINREC *login;
DBPROCESS *dbproc;
char co_name[MAXNAME];
DBINT co_id;
DBINT rows_sent;
DBBOOL more_data;
char *terminator = "\t\t";
// Install error-handler and message-handler.
dberrhandle(err_handler);
dbmsghandle(msg_handler);
// Open a DBPROCESS structure.
login = dblogin();
DBSETLUSER(login, "user");
DBSETLPWD(login, "my_passwd");
DBSETLAPP(login, "example");
BCP_SETL(login, TRUE);
dbproc = dbopen(login, "my_server");
// Initialize bulk copy.
if (bcp_init(dbproc, "comdb..accounts_info", (BYTE *)NULL,
(BYTE *)NULL, DB_IN) == FAIL)
exit(ERREXIT);
// Bind program variables to table columns.
if (bcp_bind(dbproc, (BYTE *)&co_id, 0, (DBINT)-1, (BYTE *)NULL, 0, 0, 1) == FAIL)
{
fprintf(stderr, "bcp_bind, column 1, failed.\n");
exit(ERREXIT);
}
if (bcp_bind
(dbproc, co_name, 0, (DBINT)-1, terminator, strlen(terminator), 0, 2) == FAIL)
{
fprintf(stderr, "bcp_bind, column 2, failed.\n");
exit(ERREXIT);
}
while (TRUE)
{
// Process and retrieve program data.
more_data = getdata(&co_id, co_name);
if (more_data == FALSE)
break;
// Send the data.
if (bcp_sendrow(dbproc) == FAIL)
exit(ERREXIT);
}
// Terminate the bulk copy operation.
if ((rows_sent = bcp_done(dbproc)) == -1)
printf("Bulk copy unsuccessful.\n");
else
printf("%ld rows copied.\n", rows_sent);
bcp_batch | bcp_exec |
bcp_collen | bcp_init |
bcp_colptr | bcp_moretext |
bcp_columns | bcp_sendrow |
bcp_control | dbconvert |
bcp_done | DB-Library for C Data Types |