To bulk copy by using a format file
The following example shows using bulk copy functions with both an data file and format file. Error-checking code was removed to simplify this example.
// Sample showing ODBC BCP_IN using a format file.
//
// Assumes server has:
// CREATE TABLE BCPDate (cola int, colb datetime)
// Assumes you have the format file and datafile from the example
// in How to create a bulk copy format file.
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>
SQLHENV henv = SQL_NULL_HENV;
HDBC hdbc1 = SQL_NULL_HDBC;
int main() {
RETCODE retcode;
// BCP variables.
SDWORD cRows;
// Allocate the ODBC environment and save handle.
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
// Let ODBC know this is an ODBC 3.0 app.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
// Allocate ODBC connection handle, set BCP mode, and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
retcode = SQLSetConnectAttr(hdbc1, SQL_COPT_SS_BCP,
(void *)SQL_BCP_ON, SQL_IS_INTEGER);
retcode = SQLConnect(hdbc1, "MyDSN", SQL_NTS,
"sa", SQL_NTS, "MyPassWord", SQL_NTS);
// Initialize the bulk copy.
retcode = bcp_init(hdbc1, "pubs..BCPDate", "c:\\BCPODBC.bcp",
NULL, DB_IN);
// Read the format file.
retcode = bcp_readfmt(hdbc1, "c:\\BCPFMT.fmt");
// Execute the bulk copy.
retcode = bcp_exec(hdbc1, &cRows);
printf("Number of rows bulk copied in = %d.\n", cRows);
/* Clean up. */
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return(0);
}
bcp_exec | How to bulk copy by using a format file |
bcp_init | Using Data Files and Format Files |
bcp_readfmt |