ODBC SQLBindParameter for string array

Regan Heath regan at netmail.co.nz
Mon Jan 27 06:18:17 PST 2014


On Sat, 25 Jan 2014 11:05:11 -0000, Andre <andre at s-e-a-p.de> wrote:
> // CREATE TABLE demo(name VARCHAR(1))
> // INSERT INTO demo (name) VALUES (?)
>
> string[] stringArr = ["A","B","C"];
>
> SQLSetStmtAttr(hStmt, SQL_ATTR_PARAMSET_SIZE,
>     cast(SQLPOINTER) stringArr.length, 0);
> SQLSetStmtAttr(hStmt, SQL_ATTR_PARAM_BIND_TYPE,  cast(SQLPOINTER)
>     SQL_PARAM_BIND_BY_COLUMN, 0);
> SQLINTEGER[] lengIndArr = new SQLINTEGER[](table.rowCount);
>
> // Get max length, in this example always: 1
> int maxLength = 0;
> foreach(str;stringArr){
>     if(str.length > maxLength){
>       maxLength = str.length;
>     }
> }

Try this:

// SQLCHAR is defined as ubyte
SQLCHAR*[] charArr = new SQLCHAR*[](stringArr.length);

foreach(i, str;stringArr){
     charArr[i] =   cast(SQLCHAR*) toStringz(str); // import std.string
     lengIndArr[i] = SQL_NTS; // Null terminated string
}

The SQL API is expecting an array of (C) char* pointers, not an array of  
D's ubyte[] (which is actually twice the size).

> SQLBindParameter(
>   hStmt,  cast(SQLUSMALLINT) 1,  cast(SQLSMALLINT) SQL_PARAM_INPUT, 
>   cast(SQLSMALLINT) SQL_C_CHAR,
>   cast(SQLSMALLINT)SQL_VARCHAR,
>   maxLength,  0,
>   charArr[0].ptr,
     maxLength + 1,   // I don't think the +1 is necessary..
>   lengIndArr.ptr);

Regan

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list