bcp_moretext
將 long、variable-length 資料類型值的一部分傳送至 SQL Server。
語法
RETCODE bcp_moretext (
HDBC
hdbc
,
DBINT
cbData
,
LPCBYTE
pData
);
引數
hdbc
這是已啟用大量複製的 ODBC 連接控制代碼。
cbData
這是從pData所參考的資料複製到SQL Server的資料位元組數目。 SQL_NULL_DATA 的值表示 NULL。
pData
這是要傳送至SQL Server的支援、長、可變長度資料區塊的指標。
傳回
SUCCEED 或 FAIL。
備註
此函式可以與bcp_bind和bcp_sendrow搭配使用,以一些較小的區塊將長、可變長度的資料值複製到SQL Server。 bcp_moretext可以搭配具有下列SQL Server資料類型的資料行使用: text
、 ntext
、、 image
、 varchar(max)
、、 nvarchar(max)
varbinary(max)
、使用者定義類型 (UDT) 和 XML。 bcp_moretext 不支援資料轉換,提供的資料必須符合目標資料行的資料類型。
如果使用非Null pData參數呼叫bcp_moretext支援的資料類型bcp_bind,則不論長度為何, bcp_sendrow
都會傳送整個資料值。 不過,如果 bcp_bind 具有支援的資料類型的 Null pData 參數, bcp_moretext 就可以在成功傳回 bcp_sendrow
之後立即複製資料,指出已處理任何具有資料系結資料行的資料行。
如果您使用 bcp_moretext 在資料列中傳送一個支援的資料類型資料行,您也必須使用它來傳送資料列中所有其他支援的資料類型資料行。 不能略過任何資料行。 支援的資料類型為 SQLTEXT、SQLNTEXT、SQLIMAGE、SQLUDT 和 SQLXML。 如果此資料行分別為 varchar(max)、nvarchar(max) 或 varbinary(max),則 SQLCHARACTER、SQLVARCHAR、SQNCHAR、SQLBINARY 和 SQLVARBINARY 也會列入這個類別目錄。
呼叫bcp_bind或bcp_collen會將所有資料部分的總長度設定為複製到SQL Server資料行。 嘗試傳送SQL Server的位元組數超過呼叫bcp_bind或 bcp_collen
產生錯誤。 例如,在應用程式中,用來 bcp_collen
將SQL Server text
資料行的可用資料長度設定為 4500,然後在每次呼叫資料緩衝區長度為 1000 個位元組時呼叫bcp_moretext五次。
如果複製的資料列包含一個以上的長、可變長度的資料行, bcp_moretext 會先將其資料傳送到最低序號資料行,後面接著下一個最低序數編號的資料行,依此類故。 更正預期資料的總長度設定是很重要的一件事。 除了使用長度設定以外,沒有任何方法可以指示大量複製已經收到資料行的所有資料。
使用 bcp_sendrow 和 bcp_moretext 將值傳送至伺服器時 var(max)
,就不需要呼叫 bcp_collen 來設定資料行長度。 相反地,針對這些類型,值會藉由呼叫長度為零的bcp_sendrow來終止。
應用程式通常會在迴圈內呼叫 bcp_sendrow
和 bcp_moretext ,以傳送一些資料列。 以下概述如何針對包含兩 text
個數據行的資料表執行這項操作:
while (there are still rows to send)
{
bcp_sendrow(...);
for (all the data in the first varbinary(max) column)
bcp_moretext(...);
bcp_moretext(hdbc, 0, NULL);
for (all the data in the second varbinary(max) column)
bcp_moretext(...);
bcp_moretext(hdbc, 0, NULL);
}
範例
此範例示範如何使用 bcp_moretext 搭配 bcp_bind 和 bcp_sendrow
:
// Variables like henv not specified.
HDBC hdbc;
DBINT idRow = 5;
char* pPart1 = "This text value isn't very long,";
char* pPart2 = " but it's broken into three parts";
char* pPart3 = " anyhow.";
DBINT cbAllParts;
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, "comdb..articles", NULL, NULL, DB_IN) == FAIL)
{
// Raise error and return.
return;
}
// Bind program variables to table columns.
if (bcp_bind(hdbc, (LPCBYTE) &idRow, 0, SQL_VARLEN_DATA, NULL, 0,
SQLINT4, 1) == FAIL)
{
// Raise error and return.
return;
}
cbAllParts = (DBINT) (strnlen(pPart1, sizeof(pPart1) + 1) + strnlen(pPart2, sizeof(pPart2) + 1) + strnlen(pPart3, sizeof(pPart3) + 1));
if (bcp_bind(hdbc, NULL, 0, cbAllParts, NULL, 0, SQLTEXT, 2) == FAIL)
{
// Raise error and return.
return;
}
// Send this row, with the text value broken into three chunks.
if (bcp_sendrow(hdbc) == FAIL)
{
// Raise error and return.
return;
}
if (bcp_moretext(hdbc, (DBINT) strnlen(pPart1, sizeof(pPart1) + 1), pPart1) == FAIL)
{
// Raise error and return.
return;
}
if (bcp_moretext(hdbc, (DBINT) strnlen(pPart2, sizeof(pPart2) + 1), pPart2) == FAIL)
{
// Raise error and return.
return;
}
if (bcp_moretext(hdbc, (DBINT) strnlen(pPart3, sizeof(pPart3) + 1), pPart3) == FAIL)
{
// Raise error and return.
return;
}
// All done. Get the number of rows processed (should be one).
nRowsProcessed = bcp_done(hdbc);
// Carry on.