C ++ SQLBindParameter
-
05-07-2019 - |
Domanda
Ecco le dichiarazioni delle variabili:
string strFirstName;
string strLastName;
string strAddress;
string strCity;
string strState;
double dblSalary;
string strGender;
int intAge;
... Fai un po 'di " cin " dichiarazioni per ottenere dati ...
retcode = SQLPrepare(StatementHandle, (SQLCHAR *)"INSERT INTO EMPLOYEE ([FirstName], [LastName], [Address], [City], [State], [Salary], [Gender],[Age]) VALUES (?,?,?,?,?,?,?,?)", SQL_NTS);
retcode = SQLBindParameter(StatementHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 50, 0 &strFirstName,0, NULL);
retcode = SQLBindParameter(StatementHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 50, 0, &strLastName,0, NULL);
retcode = SQLBindParameter(StatementHandle, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 30, 0, &strAddress,0, NULL);
retcode = SQLBindParameter(StatementHandle, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 30, 0, &strCity,0, NULL);
retcode = SQLBindParameter(StatementHandle, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 3, 0, &strState,0, NULL);
retcode = SQLBindParameter(StatementHandle, 6, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0, &dblSalary,0, NULL);
retcode = SQLBindParameter(StatementHandle, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 2, 0, &strGender,0, NULL);
retcode = SQLBindParameter(StatementHandle, 8, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &intAge,0, NULL);
retcode = SQLExecute(StatementHandle);
L'int e il doppio funzionano bene e vengono memorizzati nella tabella ... ma non riesco a capire come ottenere le stringhe da memorizzare ...
Soluzione
La documentazione MSDN per SQLBindParameter afferma che devi passare un buffer contenente i dati per ParameterValuePtr
e la lunghezza del buffer in byte per BufferLength
:
retcode = SQLBindParameter(StatementHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_LONGVARCHAR, 50, 0, strFirstName.c_str(), strFirstName.length(), NULL);
ParameterValuePtr [Ingresso differito] A puntatore a un buffer per il dati del parametro. Per più informazioni, consultare " ParameterValuePtr Argomento " in " Commenti. "
BufferLength [Input / Output] Lunghezza di il buffer ParameterValuePtr in byte. Per ulteriori informazioni, vedere " Argomento BufferLength " in " Commenti. "
Altri suggerimenti
Sembra l'API, vuole un carattere senza segno * prova a passare una stringa c usando la chiamata del metodo c_str () .