Home > database >  C language always access database via ODBC connection is not successful, please everyone a great god
C language always access database via ODBC connection is not successful, please everyone a great god

Time:10-03

 # include & lt; Stdio. H> 
# include & lt; stdlib.h>

# include & lt; Windows. H>
# include & lt; Sqlext. H>//this should be a must, links require odbc32. Lib

Int main () {

SQLHENV henv=NULL;//environment handle
SQLHDBC HDBC=NULL;//the odbc connection handle
SQLHSTMT HSTMT=NULL;//statement handle

SQLRETURN retcode;
SQLCHAR connout [256].
SQLSMALLINT connout_len;
SQLCHAR connstr []="Driver={Microsoft Access Driver (*. MDB)}; Dbq=E: \ \ Room AC thermal dynamic calculation \ \ RTS Data File \ \ Envelope_Database MDB. Uid=admin; The Pwd=; CharSet=GBK; ";

//distribution environment
Retcode=SQLAllocHandle (SQL_HANDLE_ENV SQL_NULL_HANDLE, & amp; Henv);
//set up the environment attribute
Retcode=SQLSetEnvAttr (henv SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0).
//distribution link
Retcode=SQLAllocHandle (SQL_HANDLE_DBC henv, & amp; HDBC);
//create a database link
Retcode=SQLDriverConnect (HDBC, NULL, connstr, SQL_NTS, connout, sizeof (connout), & amp; Connout_len SQL_DRIVER_NOPROMPT);

//retcode=0;

If (retcode==SQL_SUCCESS | | retcode==SQL_SUCCESS_WITH_INFO) {

Printf (" connnect access ok! \n");
Printf (" % s \ n ", connout);

}

The else {

Printf (" a failure! \n");
}

}


Everybody is good, the younger brother is a novice programming, encountered some problems in c language calling access database can't solve, I hope you help me, thank you!
I am a reference to this blog http://blog.csdn.net/jueane/article/details/12034571 directly copy into the VS 2013, but retcode=SQLDriverConnect this period of the return value is always 1, database calls always is not successful,
Database I had put in the system DSN (actually I don't understand the aim)

No such things as a password

This is the database

Hope you help me solve this problem, thank you! (before the plate in the C language, but apparently no one, so the plate to database, hope to have a great god help me to solve it)

CodePudding user response:

Finally my own solution, but call is SQL server database, not access database,
Steps:
1. Put the database in the system DSN;
2. Write programs;
3. The project is the "set" instead of "not set" (must change, otherwise, the return value is always 1), as shown in the figure below,

Attach the code I wrote (actually copying someone else's)
 # include & lt; Windows. H> 
# include & lt; Sqlext. H>//this should be a must, links require odbc32. Lib
# include & lt; Stdio. H>
# include & lt; stdlib.h>


Void main ()
{

SQLHENV henv;
SQLHDBC HDBC;
SQLHSTMT HSTMT;
SQLRETURN retcode;

/* the Allocate the environment handle */
Retcode=SQLAllocHandle (SQL_HANDLE_ENV SQL_NULL_HANDLE, & amp; Henv);

If (retcode==SQL_SUCCESS | | retcode==SQL_SUCCESS_WITH_INFO) {
/* Set the ODBC version environment attribute */
Retcode=SQLSetEnvAttr (henv SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0).

If (retcode==SQL_SUCCESS | | retcode==SQL_SUCCESS_WITH_INFO) {
/* the Allocate the connection handle */
Retcode=SQLAllocHandle (SQL_HANDLE_DBC henv, & amp; HDBC);

If (retcode==SQL_SUCCESS | | retcode==SQL_SUCCESS_WITH_INFO) {
/* Set the login timeout to 5 seconds. */
SQLSetConnectAttr (HDBC/* (void *) */SQL_LOGIN_TIMEOUT, (void *) 5, 0).

/* Connect to data source */
Retcode=SQLConnect (HDBC (SQLCHAR *) "Wall_PRF", SQL_NTS, (SQLCHAR *) "ABCD", SQL_NTS, (SQLCHAR *) "123456789", SQL_NTS);

If (retcode==SQL_SUCCESS | | retcode==SQL_SUCCESS_WITH_INFO) {
/* the Allocate statement handle */
Retcode=SQLAllocHandle (HDBC SQL_HANDLE_STMT, & amp; HSTMT);
Printf (" Connect Wall_PRF database success! \n");
Retcode=SQLPrepare (HSTMT, (SQLCHAR *) "select * from Wall_PRF where ID=1", SQL_NTS);//to SQL query
Retcode=SQLExecute (HSTMT);//execute SQL command

SQLINTEGER cbsatid=SQL_NTS;

While (SQLFetch (HSTMT)!=SQL_NO_DATA_FOUND)//cycle each row from the result set data
{
SQLINTEGER id;
SQLDOUBLE PRF0;
SQLDOUBLE PRF1;
SQLCHAR Weight_Type [42].

SQLGetData (HSTMT, 1, SQL_C_LONG, & amp; Id, sizeof (SQLINTEGER), & amp; Cbsatid);
SQLGetData (HSTMT, 2, SQL_C_DOUBLE, & amp; PRF0, sizeof (SQLINTEGER), & amp; Cbsatid);
SQLGetData (HSTMT, 3, SQL_C_DOUBLE, & amp; PRF1, sizeof (SQLINTEGER), & amp; Cbsatid);
SQLGetData (HSTMT, 26, SQL_C_CHAR Weight_Type, 10, & amp; Cbsatid);

Printf (" id is: % ld, PRF [0] is: % lf, PRF [1] is: % lf, Weight_Type is: % s \ n ", id, PRF0, PRF1, Weight_Type);//print the value}
}

If (retcode==SQL_SUCCESS | | retcode==SQL_SUCCESS_WITH_INFO) {
/* the Process data */

SQLFreeHandle (SQL_HANDLE_STMT HSTMT);
}
SQLDisconnect (HDBC);
}
SQLFreeHandle (SQL_HANDLE_DBC HDBC);
}
}
SQLFreeHandle (SQL_HANDLE_ENV henv);
}

system("pause");
}

CodePudding user response:

Hello, I now also in the same situation, link the access is not on, do you try to connect the access successful last?
  • Related