ODBC Bulk Copy Sample

ODBC and SQL Server

ODBC and SQL Server

ODBC Bulk Copy Sample

The ODBC bulk copy sample illustrates using Microsoft® SQL Server™ bulk copy functions with the SQL Server ODBC driver.

To build the application, you must ensure that 3.x versions of the ODBC header files and libraries are used, that the SQL Server 2000 version of Odbcss.h is used, and that the linker can find Odbcbcp.lib.

The sample is a Microsoft Foundation Class dialog application. The application allows you to connect to a defined ODBC SQL Server datasource and requires that you enter the name of a command file to process.

Command files allow you to tailor command processing by the application. The application recognizes two commands: ScriptRun, which processes Transact-SQL statements; and BCPData, which performs a bulk copy operation. The application treats strings enclosed in brackets ([]) as progress text and displays them in its progress panel area as the application processes a command file.

Sample data, containing a command file, script file, and character format bcp data files for the Northwind sample database are included.

Command File Syntax

[text]
ScriptRun
"file_name"
BCPData
"database..table", "file_name", "errorfile", direction

Arguments

[text]

Is progress text. Text between the enclosing brackets is displayed in the dialog box within its progress group.

ScriptRun "file_name"

Attempts to open and read the text file indicated in the file_name parameter. The text file must contain ODBC or Transact-SQL. The application processes multiple lines of text as a single batch, using SQLExecDirect to execute the SQL batch when the string "go" is located on a single line of the file.

BCPData "database..table", "file_name", "errorfile", direction

Performs a bcp command to copy data to or from a SQL Server table. The database..table and file_name parameters are required and specify the server table and the local file respectively. The errorfile parameter can be an empty string. No error logging is performed if it is. The direction parameter must be either IN or OUT.

To run the ODBC bulk copy sample

  1. Build a .cmd file containing ScriptRun and DBCData commands. For an example, see the file Bcpnorth.cmd in this directory:
    C:\Program Files\Microsoft SQL Server\80\Tools\Devtools\Samples\ODBC\Data\Northbcp
    
  2. Run the sample by changing to the directory with the command file and specifying the samples name on the command prompt:
    cd C:\Program Files\Microsoft SQL Server\80\Tools\Devtools\Samples\ODBC\bcp\Debug
    Bcpsamp
  3. When Bcpsamp.exe starts, connect to an ODBC data source, specify the location of the command file in the CommandFile window, and then click GO.
Functions Illustrated
Bcp_control SQLDisconnect SQLNumResultCols
Bcp_exec SQLDriverConnect SQLPrepare
Bcp_init SQLExecDirect SQLSetConnectAttr
SQLAllocHandle SQLFreeHandle SQLSetEnvAttr
SQLColAttribute SQLGetDiagField  
SQLDataSources SQLGetDiagRec