Frage: Multi Zeile in MS Access hinzufügen mit ODBC Funktionen SQLBindParameter, SQLPrepare, SQLExecute



  • Hallo,
    ich versuche eine mit ODBC API Funktionen mehrere Daten Zeile in den MS Access hinzufügen, ich verwende eine SQL INSERT statement und die ODBC Funktionen: SQLBindParameter, SQLPrepare, SQLExecute.
    Bisher kann ich nur eine Zeile jedes mal hinzufügen, aber nicht mehrere, ich weiss nicht 😕 ob diese Methode auch für multi Row Insert geeignet ist.

    Follgende code läuft schon, das ist aber nur um eine neue Zeile hinzufügen:

    int InsertDbEntry()
    {   
       int         iInsertStatus = 0;
       CRecordset  oTableSet(&oDatabase);
       SQLHSTMT    hstmt = NULL;
       SQLRETURN   retcode;
       SQLSMALLINT iID;
       SQLCHAR     sField[255];
       SQLINTEGER  cbID = 0, cbField = SQL_NTS;
    
       /* Check if database is opened */
       if (boDBOpen != true)
          return iInsertStatus = -1;
    
       /* Allocate Statement Handle */
       retcode = SQLAllocHandle(SQL_HANDLE_STMT, oDatabase.m_hdbc, &hstmt);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -2;
    
       /* Bind buffer to a parameter marker in an SQL statement */
       retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &iID, 0, &cbID);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -30;   
    
       retcode = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 255, 0, sField, 0, &cbField);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -31;   
    
       /* Prepare a SQL string for execution */
       retcode = SQLPrepare(hstmt, (SQLCHAR *)"INSERT INTO tblTesten1 (ID, Field1) VALUES (?, ?)", SQL_NTS);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -4;   
    
       iID = 15;
       strcpy((char*)sField, "Testen3");
    
       retcode = SQLExecute(hstmt);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -5;  
    
       iInsertStatus = 1;
    	return(iInsertStatus);
    }
    

    Und hier noch meine Versuch um mehrere Zeile auf einmal hinzufügen mit Hilfe von Array:

    int InsertDbEntry()
    {   
       int        iInsertStatus = 0;
       CRecordset oTableSet(&oDatabase);
       SQLHSTMT   hstmt = NULL;
       SQLRETURN  retcode;
       SQLSMALLINT iID[2];
       SQLCHAR sField[2][255];
       SQLINTEGER cbID = 0, cbField = SQL_NTS;
    
       #ifdef DEBUG
        mexPrintf("InsertDbEntry:\n");
       #endif
    
       /* Check if database is opened */
       if (boDBOpen != true)
          return iInsertStatus = -1;
    
       /* Allocate Statement Handle */
       retcode = SQLAllocHandle(SQL_HANDLE_STMT, oDatabase.m_hdbc, &hstmt);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -2;
    
    /* Bind buffer to a parameter marker in an SQL statement */
       retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &iID, 2, &cbID);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -30;   
    
       retcode = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 255, 0, &sField, 2, &cbField);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -31;   
    
       /* Prepare a SQL string for execution */
       retcode = SQLPrepare(hstmt, (SQLCHAR *)"INSERT INTO tblTesten1 (ID, Field1) VALUES (?,?)(?,?)", SQL_NTS);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -4;   
    
       iID[0] = 12;
       iID[1] = 13;
       strcpy((char*)sField[0], "Testen1");
       strcpy((char*)sField[1], "Testen2"); 
    
       retcode = SQLExecute(hstmt);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -5; 
    
       retcode = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
       if(retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
          return iInsertStatus = -6;
    
       iInsertStatus = 1;
    	return(iInsertStatus);
    }
    

    Ergebnis ist nicht erfolgreich: 'iInsertStatus = -5', also ERROR in SQLExecute,
    kann mir jemand weiterhelfen?? 😕


Anmelden zum Antworten