Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
IBCPSession2 ::BCPSetBulkMode fournit une alternative à IBCPSession ::BCPColFmt (OLE DB) pour spécifier le format de colonne. Contrairement à IBCPSession ::BCPColFmt, qui définit des attributs de format de colonne individuels, IBCPSession2 ::BCPSetBulkMode définit tous les attributs.
Syntaxe
HRESULT BCPSetBulkMode (
int property,
void * pField,
int cbField,
void * pRow,
int cbRow
);
Les arguments
propriété
Constante de type BYTE. Consultez le tableau de la section Remarques pour obtenir la liste des constantes.
pField
Pointeur vers la valeur de fin de champ.
cbField
Longueur en octets de la valeur de fin de champ.
proue
Pointeur vers la valeur de fin de ligne.
cbRow
Longueur en octets de la valeur de fin de ligne.
Retours
IBCPSession2 ::BCPSetBulkMode peut retourner l’une des options suivantes :
S_OK |
S_OK |
E_FAIL |
Une erreur spécifique au fournisseur s’est produite, pour obtenir des informations détaillées, utilisez l’interface ISQLServerErrorInfo. |
E_UNEXPECTED |
L'appel à la méthode était inattendu. Par exemple, la IBCPSession2::BCPInit méthode n’a pas été appelée avant d’appeler IBCPSession2 ::BCPSetBulkMode. |
E_INVALIDARG |
L’argument n’était pas valide. |
E_OUTOFMEMORY |
Erreur de mémoire insuffisante. |
Remarques
IBCPSession2 ::BCPSetBulkMode peut être utilisé pour copier en bloc une requête ou une table. Lorsque IBCPSession2 ::BCPSetBulkMode est utilisé pour copier en bloc une instruction de requête, il doit être appelé avant d’appeler IBCPSession::BCPControl(BCP_OPTIONS_HINTS, ...) pour spécifier l’instruction de requête.
Vous devez éviter de combiner la syntaxe d’appel RPC avec la syntaxe de requête par lots ({rpc func};SELECT * from Tblpar exemple) dans un texte de commande unique. Cela entraîne le retour d’une erreur ICommandPrepare ::P repare et vous empêche de récupérer des métadonnées. Utilisez la syntaxe ODBC CALL ({call func}; SELECT * from Tblpar exemple) si vous devez combiner l’exécution de procédure stockée et la requête par lots dans un texte de commande unique.
Le tableau suivant répertorie les constantes du paramètre de propriété .
| Propriété | Descriptif |
|---|---|
| BCP_OUT_CHARACTER_MODE | Spécifie le mode de sortie des caractères. Correspond à l’option -c dans BCP.EXE et à IBCPSession ::BCPColFmt avec la propriété eUserDataType définie sur BCP_TYPE_SQLCHARACTER. |
| BCP_OUT_WIDE_CHARACTER_MODE | Spécifie le mode de sortie Unicode. Correspond à l’option -w dans BCP.EXE et IBCPSession ::BCPColFmt avec la propriété eUserDataType définie sur BCP_TYPE_SQLNCHAR. |
| BCP_OUT_NATIVE_TEXT_MODE | Spécifie les types natifs pour les types non-caractères et Unicode pour les types de caractères. Correspond à l’option -N dans BCP.EXE et IBCPSession ::BCPColFmt avec la propriété eUserDataType définie BCP_TYPE_SQLNCHAR si le type de colonne est une chaîne ou BCP_TYPE_DEFAULT s’il ne s’agit pas d’une chaîne. |
| BCP_OUT_NATIVE_MODE | Spécifie les types de base de données natifs. Correspond à l’option -n dans BCP.EXE et IBCPSession ::BCPColFmt avec la propriété eUserDataType définie sur BCP_TYPE_DEFAULT. |
Vous pouvez appeler IBCPSession ::BCPControl et IBCPSession2 ::BCPSetBulkMode pour IBCPSession ::BCPControl options qui ne sont pas en conflit avec IBCPSession2 ::BCPSetBulkMode. Par exemple, vous pouvez appeler IBCPSession ::BCPControl avec BCP_OPTION_FIRST et IBCPSession2 ::BCPSetBulkMode.
Vous ne pouvez pas appeler IBCPSession ::BCPControl avec BCP_OPTION_TEXTFILE et IBCPSession2 ::BCPSetBulkMode.
Si vous tentez d’appeler IBCPSession2 ::BCPSetBulkMode avec une séquence d’appels de fonction qui inclut IBCPSession ::BCPColFmt, IBCPSession ::BCPControl et IBCPSession ::BCPReadFmt, l’un des appels de fonction retourne un échec d’erreur de séquence. Si vous choisissez de corriger l’échec, appelez IBCPSession ::BCPInit pour réinitialiser les paramètres et recommencer.
Le tableau suivant présente quelques exemples d’appels de fonction qui entraînent une erreur de séquence de fonctions :
Séquence d’appels
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();
Exemple :
L’exemple suivant crée quatre fichiers à l’aide de différents paramètres d’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");
}