The example for SQLBindParameter function at http://msdn.microsoft.com/en-us/library/ms710963(v=vs.85).aspx passes the size of the character array as the ColumnSize argument (6th argument) when the C type is SQL_C_CHAR
.
Quoting parts of the examples from that page:
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);
I want to know if it is okay to pass the length of the string parameter plus 1 as the ColumnSize argument. In other words, I want to know if the following call is okay if we assume that szEmployeeID
contains a null-terminated string.
retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
SQL_C_CHAR, SQL_CHAR, strlen(szEmployeeID) + 1,
0, szEmployeeID, 0, &cbEmployeeID);
I believe this can be very useful in calls like these:
SQLLEN nts = SQL_NTS;
retcode = SQLBindParameter(hstmt, 1, SQL_PAR开发者_如何学编程AM_INPUT,
SQL_C_CHAR, SQL_CHAR, 6,
0, "hello", 0, &nts);
char *domain = "stackoverflow.com";
retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
SQL_C_CHAR, SQL_CHAR, strlen(domain) + 1,
0, domain, 0, &nts);
The answer to this question is "Yes".
精彩评论