IBCPSession2::BCPSetBulkMode
IBCPSession2::BCPSetBulkMode는 열 형식을 지정하는 IBCPSession::BCPColFmt(OLE DB)를 대신하는 방법을 제공합니다. 개별 열 형식 특성을 설정하는 IBCPSession::BCPColFmt와 달리 IBCPSession2::BCPSetBulkMode는 모든 특성을 설정합니다.
구문
HRESULT BCPSetBulkMode (
int property,
void * pField,
int cbField,
void * pRow,
int cbRow
);
인수
property
BYTE 유형의 상수입니다. 상수 목록은 주의 섹션의 표를 참조하십시오.pField
필드 종결자 값에 대한 포인터입니다.cbField
필드 종결자 값의 길이(바이트)입니다.pRow
행 종결자 값에 대한 포인터입니다.cbRow
행 종결자 값의 길이(바이트)입니다.
반환 값
IBCPSession2::BCPSetBulkMode는 다음 중 하나를 반환할 수 있습니다.
S_OK |
메서드가 성공했습니다. |
E_FAIL |
공급자 관련 오류가 발생했습니다. 자세한 내용을 보려면 ISQLServerErrorInfo 인터페이스를 사용하십시오. |
E_UNEXPECTED |
예기치 않은 메서드가 호출되었습니다. 예를 들어 IBCPSession2::BCPSetBulkMode를 호출하기 전에 IBCPSession2::BCPInit 메서드를 호출하지 않았습니다. |
E_INVALIDARG |
잘못된 인수입니다. |
E_OUTOFMEMORY |
메모리 부족 오류가 발생했습니다. |
주의
IBCPSession2::BCPSetBulkMode를 사용하여 쿼리 또는 테이블을 대량 복사할 수 있습니다. 쿼리 문을 대량 복사하는 데 사용되는 IBCPSession2::BCPSetBulkMode는 IBCPSession::BCPControl(BCP_OPTIONS_HINTS, …)를 호출하여 쿼리 문을 지정하기 전에 호출해야 합니다.
단일 명령 텍스트에서 RPC 호출 구문을 일괄 처리 쿼리 구문(예: {rpc func};SELECT * from Tbl)과 결합하지 마십시오. 이렇게 결합하는 경우 ICommandPrepare::Prepare에서 오류를 반환하며 메타데이터를 검색할 수 없습니다. 단일 명령 텍스트에서 저장 프로시저 실행 및 일괄 처리 쿼리를 결합해야 할 경우 ODBC CALL 구문(예: {call func}; SELECT * from Tbl)을 사용합니다.
다음 표에서는 property 매개 변수에 대한 상수를 나열합니다.
속성 |
설명 |
---|---|
BCP_OUT_CHARACTER_MODE |
문자 출력 모드를 지정합니다. BCP.EXE의 –c 옵션 및 eUserDataType 속성이 BCP_TYPE_SQLCHARACTER로 설정된 IBCPSession::BCPColFmt에 해당됩니다. |
BCP_OUT_WIDE_CHARACTER_MODE |
유니코드 출력 모드를 지정합니다. BCP.EXE의 –w 옵션 및 eUserDataType 속성이 BCP_TYPE_SQLNCHAR로 설정된 IBCPSession::BCPColFmt에 해당됩니다. |
BCP_OUT_NATIVE_TEXT_MODE |
비문자 유형의 경우 네이티브 유형을 지정하고 문자 유형의 경우 유니코드를 지정합니다. BCP.EXE의 –N 옵션 및 eUserDataType 속성이 BCP_TYPE_SQLNCHAR(열 유형이 문자열인 경우) 또는 BCP_TYPE_DEFAULT(열 유형이 문자열이 아닌 경우)로 설정된 IBCPSession::BCPColFmt에 해당됩니다. |
BCP_OUT_NATIVE_MODE |
네이티브 데이터베이스 유형을 지정합니다. BCP.EXE의 –n 옵션 및 eUserDataType 속성이 BCP_TYPE_DEFAULT로 설정된 IBCPSession::BCPColFmt에 해당됩니다. |
IBCPSession2::BCPSetBulkMode와 충돌하지 않는 IBCPSession::BCPControl 옵션에 대해 IBCPSession::BCPControl 및 IBCPSession2::BCPSetBulkMode를 호출할 수 있습니다. 예를 들어 BCP_OPTION_FIRST 및 IBCPSession2::BCPSetBulkMode와 함께 IBCPSession::BCPControl을 호출할 수 있습니다.
BCP_OPTION_TEXTFILE 및 IBCPSession2::BCPSetBulkMode와 함께 IBCPSession::BCPControl을 호출할 수 없습니다.
IBCPSession::BCPColFmt, IBCPSession::BCPControl 및 IBCPSession::BCPReadFmt를 포함하는 함수 호출 시퀀스와 함께 IBCPSession2::BCPSetBulkMode를 호출하는 경우 해당 함수 호출 중 하나에서 시퀀스 오류 실패를 반환합니다. 이러한 오류를 해결하도록 선택하는 경우 IBCPSession::BCPInit를 호출하여 설정을 재설정하고 다시 시작합니다.
다음 표에서는 함수 시퀀스 오류를 발생시키는 일부 함수 호출의 예를 제공합니다.
호출 시퀀스 |
---|
|
|
|
|
|
|
|
|
예
다음 예제에서는 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");
}