简体   繁体   中英

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

I am learning how to use the SQLBindParameter function. 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.

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 .

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.

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 ?

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 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.

ColumnSize argument corresponds to column size in your database scheme

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). For example, the column size of a single-byte character column defined as CHAR(10) is 10. ( ODBC Reference )

The ColumnSize argument of SQLBindParameter is ignored for fixed length ( SQL_INTEGER , etc) data types. ( ODBC Reference )

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. 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. ( .NET ODBC reference )

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. For bidirectional and output parameters, and return values, you must set the value of Size. ( .NET ODBC reference )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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