Can ColumnSize argument of ODBC SQLBindParameter be strlen(param) + 1 for SQLCHAR type parameter?

空扰寡人 提交于 2019-12-24 03:24:09

问题


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_PARAM_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);

回答1:


The answer to this question is "Yes".



来源:https://stackoverflow.com/questions/5636712/can-columnsize-argument-of-odbc-sqlbindparameter-be-strlenparam-1-for-sqlcha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!