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 |
指定 Unicode 输出模式。 对应于 BCP.EXE 中的 –w 选项,并对应于 eUserDataType 属性设置为 BCP_TYPE_SQLNCHAR 的 IBCPSession::BCPColFmt。 |
BCP_OUT_NATIVE_TEXT_MODE |
指定对非字符类型使用本机类型,对字符类型使用 Unicode。 对应于 BCP.EXE 中的 –N 选项,并对应于 eUserDataType 属性设置为 BCP_TYPE_SQLNCHAR(如果列类型为 string)或设置为 BCP_TYPE_DEFAULT(如果列类型不是 string)的 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 的不同设置创建了四个文件。
// 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");
}