Share via


bcp_writefmt

Gilt für:SQL ServerAzure SQL-DatenbankAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)

Erstellt eine Formatdatei, die eine Beschreibung des Formats der aktuellen Datendatei für das Massenkopieren enthält.

Syntax

  
RETCODE bcp_writefmt (  
        HDBC hdbc,  
        LPCTSTR szFormatFile);  

Argumente

hdbc
Das für den Massenkopiervorgang aktivierte ODBC-Verbindungshandle.

szFormatFile
Pfad und Dateiname der Benutzerdatei zum Abrufen der Formatwerte für die Datendatei.

Gibt zurück

SUCCEED oder FAIL.

Bemerkungen

Die Formatdatei gibt das Datenformat einer durch Massenkopieren erstellten Datendatei an. Durch Aufrufen von bcp_columns und bcp_colfmt wird das Format der Datendatei definiert. bcp_writefmt speichert diese Definition in der Datei, auf die von szFormatFileverwiesen wird. Weitere Informationen finden Sie unter bcp_init.

Weitere Informationen zur Struktur von bcp-Datenformatdateien finden Sie unter Importieren und Exportieren von Massendaten mithilfe des bcp-Hilfsprogramms (SQL Server).

Verwenden Sie bcp_readfmt, um eine gespeicherte Formatdatei zu laden.

Hinweis

Die von bcp_writefmt erzeugte Formatdatei wird nur von Versionen des Hilfsprogramms bcp unterstützt, die mit SQL Server Version 7.0 und höher verteilt sind.

Beispiel

// Variables like henv not specified.  
HDBC      hdbc;  
DBINT      nRowsProcessed;  
  
// Application initiation, get an ODBC environment handle, allocate the  
// hdbc, and so on.  
...   
  
// Enable bulk copy prior to connecting on allocated hdbc.  
SQLSetConnectAttr(hdbc, SQL_COPT_SS_BCP, (SQLPOINTER) SQL_BCP_ON,  
   SQL_IS_INTEGER);  
  
// Connect to the data source, return on error.  
if (!SQL_SUCCEEDED(SQLConnect(hdbc, _T("myDSN"), SQL_NTS,  
   _T("myUser"), SQL_NTS, _T("myPwd"), SQL_NTS)))  
   {  
   // Raise error and return.  
   return;  
   }  
  
// Initialize bulk copy.   
if (bcp_init(hdbc, _T("myTable"), _T("myData.csv"),  
   _T("myErrors"),    DB_OUT) == FAIL)  
   {  
   // Raise error and return.  
   return;  
   }  
  
if (bcp_columns(hdbc, 3) == FAIL)  
   {  
   // Raise error and return.  
   return;  
   }  
  
bcp_colfmt(hdbc, 1, SQLCHARACTER, 0, SQL_VARLEN_DATA, '\t', 1, 1);  
bcp_colfmt(hdbc, 2, SQLCHARACTER, 0, SQL_VARLEN_DATA, '\t', 1, 2);  
bcp_colfmt(hdbc, 3, SQLCHARACTER, 0, SQL_VARLEN_DATA, '\t', 1, 3);  
  
if (bcp_writefmt(hdbc, _T("myFmtFile.fmt")) == FAIL)  
   {  
   // Raise error and return.  
   return;  
   }  
  
if (bcp_exec(hdbc, &nRowsProcessed) == SUCCEED)  
   {  
   printf_s("%ld rows copied from SQL Server\n", nRowsProcessed);  
   }  
  
// Carry on.  

Weitere Informationen

Bulk Copy Functions