| cyq84 (2) | |
|
Hi all, May i know how to establish a ODBC connection to SQL sever 2005 via c++ and retrieve the data from the database at the console application? I had written the following codes: try { CDatabase cdb; CString sSQLStmt ="SELECT sequenceId FROM AuthEvents"; int bconnect; CString str = "ODBC;DSN=EARTH;UID=sa;PWD=pass#word1;" ; bconnect=cdb.Open(str); if(!bconnect) printf("failed"); else printf("connection open successfully"); CRecordset rs(&cdb); rs.m_strFilter =sSQLStmt; rs.Open(); } catch(_com_error ) { printf("Error"); } Currently, the program was able to open the connection successfuly but there is an error (runtime error) when it runs the line rs.Open().Could someone help me with this? Thanks | |
|
|
|
| SteakRider (110) | |||||
|
How did you know it is connected or not? use this class to connect dbms I know it does not look nice, but it works, just change connection string, and run sql
| |||||
|
Last edited on
|
|||||
| cyq84 (2) | |
|
Hi, I had solved it. It is calling using ODBC. Thanks anyway. CString sSQLStmt ="SELECT * FROM aaa"; int bconnect; CString str = "ODBC;DSN=xxxxx;UID=sa;PWD=pass;" ; bconnect=cdb.Open(str); if(!bconnect) printf("failed"); else printf("connection open successfully\n"); CRecordset rs(&cdb); CString varValue; CDBVariant varValue1; _bstr_t eventTime; rs.Open( CRecordset::forwardOnly, sSQLStmt ); short nFields = rs.GetODBCFieldCount( ); while( !rs.IsEOF( ) ) { rs.GetFieldValue( "[column name]", varValue); eventTime = varValue; printf("eventTime: %s\n\n", varValue); rs.MoveNext( ); } rs.Close( ); | |
|
|
|