다음을 통해 공유


IBCPSession2::BCPSetBulkMode

IBCPSession2::BCPSetBulkMode는 열 형식을 지정하기 위한 IBCPSession::BCPColFmt(OLE DB) 에 대한 대안을 제공합니다. 개별 열 형식 특성을 설정하는 IBCPSession::BCPColFmt와 달리 IBCPSession2::BCPSetBulkMode는 모든 특성을 설정합니다.

문법

  
HRESULT BCPSetBulkMode (  
      int property,  
   void * pField,  
   int cbField,  
   void * pRow,  
   int cbRow  
);  

주장들

속성
BYTE 형식의 상수입니다. 상수 목록은 주의 섹션의 표를 참조하세요.

pField
필드 종결자 값에 대한 포인터입니다.

cbField
필드 종결자 값의 길이(바이트)입니다.

이물
행 종결자 값에 대한 포인터입니다.

cbRow
행 종결자 값의 길이(바이트)입니다.

반품

IBCPSession2::BCPSetBulkMode는 다음 중 하나를 반환할 수 있습니다.

S_OK 메서드가 성공했습니다.
E_FAIL 공급자 관련 오류가 발생했습니다. 자세한 내용은 ISQLServerErrorInfo 인터페이스를 사용합니다.
E_UNEXPECTED 메서드 호출이 예기치 않은 경우 예를 들어 IBCPSession2::BCPInit IBCPSession2::BCPSetBulkMode를 호출하기 전에 메서드가 호출되지 않았습니다.
E_INVALIDARG 인수가 잘못되었습니다.
E_OUTOFMEMORY 메모리 부족 오류입니다.

비고

IBCPSession2::BCPSetBulkMode를 사용하여 쿼리 또는 테이블에서 대량 복사할 수 있습니다. IBCPSession2::BCPSetBulkMode를 사용하여 쿼리 문을 대량 복사하는 경우 쿼리 문을 지정하기 위해 호출하기 전에 호출 IBCPSession::BCPControl(BCP_OPTIONS_HINTS, ...) 해야 합니다.

단일 명령 텍스트에서 RPC 호출 구문을 일괄 처리 쿼리 구문({rpc func};SELECT * from Tbl예: )과 결합하지 않아야 합니다. 이로 인해 ICommandPrepare::P repare가 오류를 반환하고 메타데이터를 검색하지 못하게 됩니다. 저장 프로시저 실행과 일괄 처리 쿼리를 단일 명령 텍스트로 결합해야 하는 경우 ODBC CALL 구문({call func}; SELECT * from Tbl예: )을 사용합니다.

다음 표에서는 속성 매개 변수의 상수를 나열합니다.

재산 설명
BCP_OUT_CHARACTER_MODE 문자 출력 모드를 지정합니다.

BCP.EXE -c 옵션과 eUserDataType 속성 BCP_TYPE_SQLCHARACTER이 설정된 IBCPSession::BCPColFmt에 해당합니다.
BCP_OUT_WIDE_CHARACTER_MODE 유니코드 출력 모드를 지정합니다.

eUserDataType 속성이 설정된 BCP.EXE 및 IBCPSession::BCPColFmt의 -w 옵션에 해당합니다BCP_TYPE_SQLNCHAR.
BCP_OUT_NATIVE_TEXT_MODE 문자가 아닌 형식의 네이티브 형식과 문자 형식의 유니코드를 지정합니다.

열 형식이 문자열이거나 BCP_TYPE_DEFAULT 문자열이 아닌 경우 eUserDataType 속성이 설정된 BCP_TYPE_SQLNCHAR BCP.EXE 및 IBCPSession::BCPColFmt의 -N 옵션에 해당합니다.
BCP_OUT_NATIVE_MODE 네이티브 데이터베이스 형식을 지정합니다.

eUserDataType 속성이 설정된 BCP.EXE 및 IBCPSession::BCPColFmt의 -n 옵션에 해당합니다BCP_TYPE_DEFAULT.

IBCPSession::BCPControl 및 IBCPSession2::BCPSetBulkMode for IBCPSession::BCPSession2::BCPSetBulkMode와 충돌하지 않는 BCPControl 옵션을 호출할 수 있습니다. 예를 들어 IBCPSession::BCPControl with BCP_OPTION_FIRST and IBCPSession2::BCPSetBulkMode를 호출할 수 있습니다.

IBCPSession::BCPControl with BCP_OPTION_TEXTFILE and IBCPSession2::BCPSetBulkMode를 호출할 수 없습니다.

IBCPSession::BCPColFmt, IBCPSession::BCPControl 및 IBCPSession::BCPReadFmt를 포함하는 함수 호출 시퀀스를 사용하여 IBCPSession2::BCPSetBulkMode를 호출하려고 하면 함수 호출 중 하나가 시퀀스 오류 오류를 반환합니다. 오류를 수정하도록 선택한 경우 IBCPSession::BCPInit를 호출하여 설정을 다시 시작하고 다시 시작합니다.

다음 표에서는 함수 시퀀스 오류를 발생시키는 함수 호출의 몇 가지 예를 제공합니다.

호출 시퀀스

BCPInit("table", "dataFile", "errorFile", BCP_DIRECTION_IN);  
BCPSetBulkMode();  
BCPInit("table", "dataFile", "errorFile", BCP_DIRECTION_OUT);  
BCPSetBulkMode();  
BCPReadFmt();  
BCPInit(NULL, "dataFile", "errorFile", BCP_DIRECTION_OUT);  
BCPControl(BCP_OPTION_HINTS, "select ...");  
BCPSetBulkMode();  
BCPInit("table", "dataFile", "errorFile", BCP_DIRECTION_OUT);  
BCPSetBulkMode();  
BCPColFmt();  
BCPInit("table", "dataFile", "errorFile", BCP_DIRECTION_OUT);  
BCPControl(BCP_OPTION_DELAYREADFMT, true);  
BCPReadFmt();  
BCPColFmt();  
BCPInit(NULL, "dataFile", "errorFile", BCP_DIRECTION_OUT);  
BCPControl(BCP_OPTION_DELAYREADFMT, true);  
BCPSetBulkMode();  
BCPControl(BCP_OPTION_HINTS, "select ...");  
BCPReadFmt();  
BCPInit("table", "dataFile", "errorFile", BCP_DIRECTION_OUT);  
BCPControl(BCP_OPTION_DELAYREADFMT, true);  
BCPColumns();  
BCPInit("table", "dataFile", "errorFile", BCP_DIRECTION_OUT);  
BCPControl(BCP_OPTION_DELAYREADFMT, true);  
BCPSetColFmt();  

예시

다음 샘플에서는 IBCPSession2::BCPSetBulkMode의 다른 설정을 사용하여 4개의 파일을 만듭니다.

  
// compile with: sqlncli11.lib oleaut32.lib ole32.lib  
  
#include <stdio.h>  
#include "sqlncli.h"  
  
IDBInitialize*  g_pIDBInitialize = NULL;  
IBCPSession2 * g_pIBcpSession = NULL;  
class COLEDBPropSet : public DBPROPSET {  
public:  
   COLEDBPropSet() {  
      rgProperties = NULL;  
      cProperties = 0;  
   };  
   COLEDBPropSet(const GUID& guid) {  
      rgProperties = NULL;  
      cProperties = 0;  
      guidPropertySet = guid;  
   };  
   ~COLEDBPropSet() {  
      for ( ULONG i = 0 ; i < cProperties ; i++ )  
         VariantClear(&rgProperties[i].vValue);  
      CoTaskMemFree(rgProperties);  
   }  
   void SetGUID(const GUID& guid) {  
      guidPropertySet = guid;  
   };  
   bool AddProperty(DWORD dwPropertyID, bool bValue) {  
      if (!Add())  
         return false;  
      rgProperties[cProperties].dwPropertyID = dwPropertyID;  
      rgProperties[cProperties].vValue.vt = VT_BOOL;  
      rgProperties[cProperties].vValue.boolVal = (bValue) ? VARIANT_TRUE : VARIANT_FALSE;  
      cProperties++;  
      return true;  
   };  
   bool AddProperty(DWORD dwPropertyID, long nValue) {  
      if (!Add())  
         return false;  
      rgProperties[cProperties].dwPropertyID  = dwPropertyID;  
      rgProperties[cProperties].vValue.vt     = VT_I4;  
      rgProperties[cProperties].vValue.lVal   = nValue;  
      cProperties++;  
      return true;  
   };  
   bool AddProperty(DWORD dwPropertyID,LPCWSTR szValue) {  
      if (!Add())  
         return false;  
      rgProperties[cProperties].dwPropertyID = dwPropertyID;  
      rgProperties[cProperties].vValue.vt = VT_BSTR;  
      rgProperties[cProperties].vValue.bstrVal = SysAllocString(szValue);  
      cProperties++;  
      return true;  
   };  
   bool Add() {  
      DBPROP* p = (DBPROP*)CoTaskMemRealloc(rgProperties, (cProperties + 1) * sizeof(DBPROP));  
      if (p != NULL) {  
         rgProperties = p;  
         rgProperties[cProperties].dwOptions = DBPROPOPTIONS_REQUIRED;  
         rgProperties[cProperties].colid = DB_NULLID;  
         rgProperties[cProperties].vValue.vt = VT_EMPTY;  
         return true;  
      }  
      else  
         return false;  
   };  
};  
  
void OLEDBCleanUp() {  
   if (g_pIDBInitialize) {  
      g_pIDBInitialize->Release();  
      g_pIDBInitialize = NULL;  
   }  
   if (g_pIBcpSession) {  
      g_pIBcpSession->Release();  
      g_pIBcpSession = NULL;  
   }  
}  
  
BOOL MakeOLEDBConnect(LPWSTR  pServer) {  
   BOOL ret = true;  
   IDBProperties * pIDBProperties = NULL;  
   IDBCreateSession * pIDBCreateSession = NULL;  
   COLEDBPropSet PropSet(DBPROPSET_DBINIT);  
   COLEDBPropSet BcpProperty(DBPROPSET_SQLSERVERDATASOURCE);  
   try {  
      HRESULT hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);   
      hr = CoCreateInstance(SQLNCLI_CLSID, NULL, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (LPVOID *)&g_pIDBInitialize);  
      if (FAILED(hr)) {  
         printf("CoCreateInstance failed\n");  
         return false;  
      }  
      PropSet.AddProperty(DBPROP_INIT_DATASOURCE, (LPWSTR)pServer);  
      PropSet.AddProperty(DBPROP_AUTH_INTEGRATED, L"SSPI");  
      hr = g_pIDBInitialize->QueryInterface(IID_IDBProperties, (void**) &pIDBProperties);  
      if (FAILED(hr)) {  
         printf("g_pIDBInitialize->->QueryInterface(IID_IDBProperties...) failed\n");  
         throw false;  
      }  
      hr = pIDBProperties->SetProperties(1, &PropSet);  
      if (FAILED(hr)) {  
         printf("g_pIDBInitialize->->SetProperties(...) failed\n");  
         throw false;  
      }  
      hr = g_pIDBInitialize->Initialize();  
      if (FAILED(hr)) {  
         printf("g_pIDBInitialize->->Initialize() failed\n");  
         throw false;  
      }  
      BcpProperty.AddProperty(SSPROP_ENABLEFASTLOAD, true);  
      BcpProperty.AddProperty(SSPROP_ENABLEBULKCOPY, true);  
      hr = pIDBProperties->SetProperties(1, &BcpProperty);  
      if (FAILED(hr)) {  
         printf("g_pIDBInitialize->->SetProperties() for bcp failed\n");  
         throw false;  
      }  
      hr = g_pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void**) &pIDBCreateSession);  
      if (FAILED(hr)) {  
         printf("g_pIDBInitialize->QueryInterface(IID_IDBCreateSession..) failed\n");  
         throw false;  
      }  
  
      hr = pIDBCreateSession->CreateSession(NULL, IID_IBCPSession2, (IUnknown**) &g_pIBcpSession);  
      if (FAILED(hr)) {  
         printf("g_pIDBCreateSession->CreateSession() failed\n");  
         throw false;  
      }  
   }  
   catch(...) {  
      ret = false;  
   }  
   if (pIDBProperties)  
      pIDBProperties->Release();  
   if (pIDBCreateSession)  
      pIDBCreateSession->Release();  
   return ret;  
}  
  
BOOL BCPSetBulkMode(LPWSTR pszServer, LPTSTR pszQureryOut, char BCPType, LPWSTR pszDataFile) {  
   HRESULThr;  
   if (!MakeOLEDBConnect(pszServer))  
      return false;  
   hr = g_pIBcpSession->BCPInit(NULL, pszDataFile, NULL, BCP_DIRECTION_OUT );   // bcp init for queryout  
   if (FAILED(hr)) {  
      printf("BCP init failed\n");  
      OLEDBCleanUp();  
      return false;  
   }  
   // setbulkmode  
   char ColTerm[] = "\t";  
   char RowTerm[] = "\r\n";  
   wchar_t wColTerm[] = L"\t";  
   wchar_t wRowTerm[] = L"\r\n";  
   BYTE * pColTerm = NULL;  
   int cbColTerm = NULL;  
   BYTE * pRowTerm = 0;  
   int cbRowTerm = 0;  
   int bulkmode = -1;  
  
   if(BCPType == 'c') {   // bcp -c  
      pColTerm = (BYTE*)ColTerm;  
      pRowTerm = (BYTE*)RowTerm;  
      cbColTerm = 1;  
      cbRowTerm = 2;  
      bulkmode = BCP_OUT_CHARACTER_MODE;  
   }  
   else  
      if(BCPType == 'w') {   // bcp -w  
         pColTerm = (BYTE*)wColTerm;  
         pRowTerm = (BYTE*)wRowTerm;  
         cbColTerm = 2;  
         cbRowTerm = 4;  
         bulkmode = BCP_OUT_WIDE_CHARACTER_MODE;  
      }  
      else  
         if (BCPType == 'n')   // bcp -n  
            bulkmode = BCP_OUT_NATIVE_MODE;  
         else  
            if (BCPType == 'N')   // bcp -n  
               bulkmode = BCP_OUT_NATIVE_TEXT_MODE;  
            else {  
               printf("unknown bcp mode\n");  
               OLEDBCleanUp();  
               return false;  
            }  
            hr = g_pIBcpSession->BCPSetBulkMode(bulkmode, pColTerm, cbColTerm, pRowTerm, cbRowTerm);  
            if (FAILED(hr)) {  
               printf("BCPSetBulkMode failed\n");  
               OLEDBCleanUp();  
               return false;  
            }  
  
            // set queryout TSQL statement  
            hr = g_pIBcpSession->BCPControl(BCP_OPTION_HINTS, pszQureryOut);  
            if (FAILED(hr)) {  
               printf("BCPControl failed\n");  
               OLEDBCleanUp();  
               return false;  
            }  
            // bcp copy  
            DBROWCOUNT nRowsInserted = 0;  
            hr = g_pIBcpSession->BCPExec(&nRowsInserted);  
            if (FAILED(hr)) {  
               printf("BCPExec failed\n");  
               OLEDBCleanUp();  
               return false;  
            }  
            printf("bcp done\n");  
            OLEDBCleanUp();  
            return true;  
}  
  
int main() {  
   BCPSetBulkMode(L"localhost", TEXT("SELECT 'this is a bcp -c test', 1,2") , 'c', L"bcpc.dat");  
   BCPSetBulkMode(L"localhost", TEXT("SELECT 'this is a bcp -w test', 1,2") , 'w', L"bcpw.dat");  
   BCPSetBulkMode(L"localhost", TEXT("SELECT 'this is a bcp -c test', 1,2") , 'n', L"bcpn.dat");  
   BCPSetBulkMode(L"localhost", TEXT("SELECT 'this is a bcp -w test', 1,2") , 'N', L"bcp_N.dat");  
}  

또한 참조하십시오

IBCPSession2(OLE DB)