Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [C]Connettersi ad un database access

    Ciao a tutti,
    potreste dirmi come posso fare in c a connettermi ad un database access? (e se avete anche qualche link o info per la trattazione dei recordset ve ne sarei grato)
    xxx

  2. #2
    potreste dirmi come posso fare in c a connettermi ad un database access?
    dipende dal compilatore e/o ambiente C/C++ che utilizzi.
    ...Terrible warlords, good warlords, and an english song

  3. #3
    Programmo in C (anche se "programmo" al momento è una parola grossa), i compilatori che uso sono devC++ o il borland
    xxx

  4. #4
    P.s. io utilizzo (e mi trovo molto bene con)
    Microsoft C++ (di Visual Studio),
    anche se gioco spesso con altri compilatori/ambienti C/C++.

    Ho fatto una ricerca su internet, per cercare qualche thread o tutorial che spiegasse come connettersi ad un database (qualsiasi)
    utilizzando DEV C++, ma non ho trovato NULLA!!! (nemmeno sul sito ufficiale).

    Stessa cosa dicesi per Borland BCC32, non ho trovato nulla di specifico, ci sono centinaia di link riferiti a "C++ BUILDER" (sempre di borland). P.S. alcune versioni di Borland C/C++ non supportano
    i driver ODBC leggi: http://www.cs.umu.se/kurser/TDBC86/H...11_oh_odbc.pdf

    Il codice (C generico) che ho trovato, mi sembra "abbastanza"
    standardizzato nel senso che utilizza tre header presenti
    in quasi tutti i compilatori C/C++ per sistemi windows.

    Prima di utilizzarlo, devi creare un DSN di sistema (da "Pannello di Controllo\Origine dati ODBC") e farlo puntare ad un database ACCESS
    il DSN l'ho chiamato "ACCESSdb".
    codice:
    #include <windows.h>       
    #include <sqlext.h>        // interfaccia ODBC con il Database
    #include <stdio.h>         
    int main(void)
    {
    
        HENV     hEnv = NULL;                              // Env Handle from SQLAllocEnv()
        HDBC     hDBC = NULL;                              // Connection handle
        HSTMT    hStmt = NULL;                             // Statement handle
        UCHAR    szDSN[SQL_MAX_DSN_LENGTH] = "ACCESSdb";   // Data Source Name buffer "ACCESSdb"
        UCHAR*   szUID = NULL;                             // User ID buffer 
        UCHAR*   szPasswd = NULL;                          // Password buffer
        UCHAR    szModel[128];                             // Model buffer
        SDWORD   cbModel;                                  // Model buffer bytes recieved
        UCHAR    szSqlStr[] = "Select * From tabella1";    // SQL string
        RETCODE  retcode;                                  // Return code
    
        // Allocate memory for ODBC Environment handle
        SQLAllocEnv (&hEnv);
    
        // Allocate memory for the connection handle
        SQLAllocConnect (hEnv, &hDBC);
    
        // Connect to the data source "ACCESSdb" using userid and password.
        retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);
    
        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
        {
            // Allocate memory for the statement handle
            retcode = SQLAllocStmt (hDBC, &hStmt);           
    
            // Prepare the SQL statement by assigning it to the statement handle
            retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
    
            // Execute the SQL statement handle
            retcode = SQLExecute (hStmt);                
    
            // Project only column 1 which is the models
            SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);
    
            // Get row of data from the result set defined above in the statement
            retcode = SQLFetch (hStmt);
    
            while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
            {
                printf ("\t%s\n", szModel);      // Print row (model)
                retcode = SQLFetch (hStmt);      // Fetch next row from result set
            }
    
            // Free the allocated statement handle
            SQLFreeStmt (hStmt, SQL_DROP);
    
            // Disconnect from datasource
            SQLDisconnect (hDBC);
        }   
    
        // Free the allocated connection handle
        SQLFreeConnect (hDBC);
    
        // Free the allocated ODBC environment handle
        SQLFreeEnv (hEnv);
    
        system("pause");
        return 0;
    
    }
    P.S. controlla se nella cartella "INCLUDE" del tuo compilatore
    sono presenti gli header "sql.h", "sqlext.h", "sqlucode.h", "sqltypes.h"
    e se in essi sono definite le funzioni SQLConnect() e tutte
    le altre che nel codice riportato sopra, iniziano per SQL...
    ...Terrible warlords, good warlords, and an english song

  5. #5
    Grazie, sei stato infinitamente gentile!
    Appena arrivo a casa (sono al lavoro) provo tutto.

    Solo una cosa: esiste la possibilità di connessioni dsn-less, specificando il path del db?
    Grazie ancora, ciao
    xxx

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.