How to: Bulk Copy Without a Format File (ODBC)

New: 14 April 2006

A complete sample shows how to use bulk copy functions to create a native mode data file. The complete sample code is in the file BulkCopyNativeMode.cpp, which you can download from the SQL Server Downloads page on MSDN. This sample was developed using Microsoft Visual C++ 2005 and was developed for ODBC version 3.0 or later.

ms403300.security(en-US,SQL.90).gifSecurity Note:
When possible, use Windows Authentication. If Windows Authentication is not available, prompt users to enter their credentials at run time. Avoid storing credentials in a file. If you must persist credentials, you should encrypt them with the Win32 crypto API.

To bulk copy without a format file

  1. Allocate an environment handle and a connection handle.

  2. Set SQL_COPT_SS_BCP and SQL_BCP_ON to enable bulk copy operations.

  3. Connect to SQL Server.

  4. Call bcp_init to set the following information:

    • The name of the table or view to bulk copy from or to.
    • The name of the data file that contains the data to copy into the database or that receives data when copying from the database.
    • The name of a data file to receive any bulk copy error messages (specify NULL if you do not want a message file).
    • The direction of the copy: DB_IN from the file to the view or table, or DB_OUT to the file from the table or view.
  5. Call bcp_exec to execute the bulk copy operation.

When DB_OUT is set with these steps, the file is created in native format. The file can then be bulk copied into a server by following these same steps, except that DB_OUT is set instead of DB_IN. This works only if both the source and target tables have exactly the same structure.

See Also

Other Resources

How to: Bulk Copy with the SQL Server ODBC Driver (ODBC)

Help and Information

Getting SQL Server 2005 Assistance