将长长度可变数据类型值的一部分发送到 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 不支持数据转换,提供的数据必须与目标列的数据类型匹配。
如果使用 bcp_moretext 支持的数据类型使用 nonNULL pData 参数调用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 5 次。
如果复制的行包含多个长、可变长度的列, 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.