senza DSN di sistema
codice:
/* bisogna linkare la libreria: (con DevC++) "libodbc32.a" con gli altri compilatori "odbc32.lib" "*/
#include <windows.h>
#include <sqlext.h> // interfaccia ODBC con il Database
#include <stdio.h>
HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL; // Statement handle
UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "ACCESSdevc"; // Data Source Name buffer "ACCESSdevc"
UCHAR szDSNless[] = "DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=mio_database.mdb";
UCHAR* szUID = NULL; // User ID buffer
UCHAR* szPasswd = NULL; // Password buffer
UCHAR szModelname[50]; // Model buffer field1
UCHAR szModelfirstname[50]; // Model buffer field2
UCHAR szModeltelephone[50]; // Model buffer field3
SDWORD cbModel; // Model buffer bytes recieved
UCHAR szSqlStr[] = "SELECT * FROM mia_tabella"; // SQL string
RETCODE retcode; // Return code
int main()
{
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "ACCESSdevc" using DSN.
//retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);
// Connect to the data source "ACCESS database" using DSN-less.
retcode = SQLDriverConnect(hDBC, NULL, (SQLCHAR *)szDSNless,
SQL_NTS, (SQLCHAR *)szDSNless, sizeof(szDSNless), NULL, SQL_DRIVER_NOPROMPT);
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, szModelname, sizeof(szModelname), &cbModel);
// Project only column 2 which is the models
SQLBindCol (hStmt, 2, SQL_C_CHAR, szModelfirstname, sizeof(szModelfirstname), &cbModel);
// Project only column 3 which is the models
SQLBindCol (hStmt, 3, SQL_C_CHAR, szModeltelephone, sizeof(szModeltelephone), &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 ("%s ", szModelname); // Print row (model)
printf ("%s ", szModelfirstname); // Print row (model)
printf ("%s\n", szModeltelephone); // 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;
}
con DSN di sistema
http://forum.html.it/forum/showthrea...hreadid=669954
altro (solo per Microsoft C++)
http://forum.html.it/forum/showthrea...62#post2943162