简体   繁体   English

在 C/C++ 程序中作为 ODBC SQLBindParameter 的 ColumnSize 参数传递什么?

[英]What to pass as the ColumnSize argument of ODBC SQLBindParameter in a C/C++ program?

I am learning how to use the SQLBindParameter function.我正在学习如何使用SQLBindParameter函数。 I was going through a couple of examples on the Internet and it is not clear what I should be passing as the 6th argument to the SQLBindParameter function.我正在浏览 Internet 上的几个示例,但不清楚我应该将什么作为第 6 个参数传递给SQLBindParameter函数。

The example at http://msdn.microsoft.com/en-us/library/ms710963(v=vs.85).aspx passes the size of the character array when the C type is SQL_C_CHAR and 0 when the C type is SQL_C_SSHORT . http://msdn.microsoft.com/en-us/library/ms710963(v=vs.85).aspx 上的示例在 C 类型为SQL_C_CHAR时传递字符数组的大小,当 C 类型为SQL_C_SSHORT 0 .

SQLSMALLINT sCustID;    
SQLCHAR szEmployeeID[EMPLOYEE_ID_LEN];
SQL_DATE_STRUCT dsOrderDate;
SQLINTEGER cbCustID = 0, cbOrderDate = 0, cbEmployeeID = SQL_NTS;

... ...

retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, EMPLOYEE_ID_LEN, 0, szEmployeeID, 0, &cbEmployeeID);
retcode = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &sCustID, 0, &cbCustID);

However, an example at http://publib.boulder.ibm.com/infocenter/db2e/v8r2/index.jsp?topic=%2Fcom.ibm.db2e.doc%2Fdbeapc1702.html passes 0 for SQL_C_TCHAR and some positive integers for SQL_C_LONG.但是, http://publib.boulder.ibm.com/infocenter/db2e/v8r2/index.jsp?topic =%2Fcom.ibm.db2e.doc%2Fdbeapc1702.html 上的一个示例为 SQL_C_TCHAR 传递 0,为 SQL_C_TCHAR 传递一些正整数SQL_C_LONG。

long p1 = 10; 
short p2 = 100; 
TCHAR p3[100];

... ...

// bind input parameters 
rc = SQLBindParameter(hstmt, (unsigned short)1, SQL_PARAM_INPUT,
                        SQL_C_LONG, SQL_INTEGER, 4, 0, &p1, sizeof(p1), &len); 
// check return code ... 

rc = SQLBindParameter(hstmt, (unsigned short)2, SQL_PARAM_INPUT, SQL_C_LONG,
                        SQL_SMALLINT, 2, 0, &p2, sizeof(p2), &len); 
// check return code ... 

len = SQL_NTS; 
rc = SQLBindParameter(hstmt, (unsigned short)3, SQL_PARAM_INPUT, SQL_C_TCHAR,
                                SQL_CHAR, 0, 0, &p3[0], 100, &len); 

Could someone please clarify how exactly do we decide the parameters to be passed into SQLBindParameter ?有人可以澄清我们究竟如何决定要传递给SQLBindParameter的参数吗?

This is to help determine the byte size of the parameter for certain types it's ignored for others.这是为了帮助确定某些类型的参数的字节大小,而其他类型则忽略它。

Say you had a SQLCHAR[10] parameter you would pass 10 in as the column size:假设您有一个SQLCHAR[10]参数,您将传入10作为列大小:

SQLCHAR empStr[10];
SQLINTEGER len = SQL_NTS;
retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, empStr, 0, &len);

Edit:编辑:

To be more clear variable types need to have sizes passed in especially variable sized types like strings so you know where the data ends in memory.为了更清晰,变量类型需要传递大小,尤其是像字符串这样的可变大小类型,以便您知道数据在内存中的结束位置。 Types such as integers have statically defined sizes based on OS and environment so it's often optional to specify the size or there may be a constant defined that you can use instead of calculating.诸如整数之类的类型具有基于操作系统和环境静态定义的大小,因此指定大小通常是可选的,或者可能定义了一个常量,您可以使用它来代替计算。

Consider:考虑:

int smallInt = 5;
int bigInt = 234872634872;
char oneChar = 'A';
char charArray[128] = "CStyle String\0";

int smallIntSize = sizeof(smallInt);
int bigIntSize = sizeof(bigInt);
int oneCharSize = sizeof(oneChar);
int charArraySize = sizeof(charArray);

On my Windows 7 64bit machine using Visual Studio 2010 the size of both smallInt and bigInt are 4 the size of oneChar is 1 and the size of charArray is 128 even though the string it contains is much smaller it still has all that space allocated.在我使用 Visual Studio 2010 的 Windows 7 64 位机器上,smallInt 和 bigInt 的大小都是 4,oneChar 的大小是 1,charArray 的大小是 128,即使它包含的字符串小得多,它仍然分配了所有空间。

ColumnSize argument corresponds to column size in your database scheme ColumnSize参数对应于您的数据库方案中的列大小

All character types所有字符类型

The defined or maximum column size in characters of the column or parameter (as contained in the SQL_DESC_LENGTH descriptor field).以字符为单位的已定义或最大列大小(包含在 SQL_DESC_LENGTH 描述符字段中)。 For example, the column size of a single-byte character column defined as CHAR(10) is 10. ( ODBC Reference )例如,定义为 CHAR(10) 的单字节字符列的列大小为 10。ODBC 参考

The ColumnSize argument of SQLBindParameter is ignored for fixed length ( SQL_INTEGER , etc) data types.对于固定长度( SQL_INTEGER等)数据类型,将忽略 SQLBindParameter 的ColumnSize参数。 ( ODBC Reference ) ( ODBC 参考)

When SQL_PARAM_INPUT it probably ignored by some drivers, but other may truncate your buffer to ColumnSize For variable-length data types, Size describes the maximum amount of data to transmit to the server.SQL_PARAM_INPUT它可能被某些驱动程序忽略,但其他驱动程序可能会将您的缓冲区截断为ColumnSize对于可变长度数据类型,Size 描述了要传输到服务器的最大数据量。 For example, for a Unicode string value, Size could be used to limit the amount of data sent to the server to the first one hundred characters.例如,对于 Unicode 字符串值,可使用 Size 将发送到服务器的数据量限制为前一百个字符。 ( .NET ODBC reference ) .NET ODBC 参考

When SQL_PARAM_*OUTPUT some drivers failing to find column when ColumnSize is not specified (especially [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error ) even when sending NULL.SQL_PARAM_*OUTPUT某些驱动程序在未指定ColumnSize时无法找到列(尤其是[Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error ),即使发送 NULL 也是如此。 For bidirectional and output parameters, and return values, you must set the value of Size.对于双向和输出参数以及返回值,必须设置Size 的值。 ( .NET ODBC reference ) .NET ODBC 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM