Console Application with Database Connectivity
-
Here is my problem. I am writting a console application and I need to connect to a database in order to either execute a stored procedure or to execute a query. Then I want to store the results into an array and use it for other purposes throughout the program. My problem is how do I connect, and how to I execute and retreive the results. I've tried using a wizard, but it creates so much code that I don't understand. :(( Is there a library that I can pull in and use it? My database is accessable via ODBC, I've tested the connection and it works. Assuming the ODBC name for the database is "Database1" and the stored procedure name is "sp1", how would I code it? :confused: ICXC NIKA
-
Here is my problem. I am writting a console application and I need to connect to a database in order to either execute a stored procedure or to execute a query. Then I want to store the results into an array and use it for other purposes throughout the program. My problem is how do I connect, and how to I execute and retreive the results. I've tried using a wizard, but it creates so much code that I don't understand. :(( Is there a library that I can pull in and use it? My database is accessable via ODBC, I've tested the connection and it works. Assuming the ODBC name for the database is "Database1" and the stored procedure name is "sp1", how would I code it? :confused: ICXC NIKA
The following classes should help you. The first is the .H file, followed by the .cpp. #if !defined(_DBCONN_H_) #define _DBCONN_H_ #ifdef RETCODE #undef RETCODE #endif #ifndef FAR #define FAR __far #endif #ifdef ODBCVER #undef ODBCVER #endif #define ODBCVER 0x0100 #include #include #include #define STATIC_BUFFER_SIZE 1024 class DBStatement; class AFX_EXT_CLASS DBConnection { protected: HDBC m_hConnHdl; long m_lErrorCode; char m_caErrorMsg[SQL_MAX_MESSAGE_LENGTH]; BOOL m_bDisableStatementScanning; public: static HENV m_hEnvironment; DBConnection(); virtual ~DBConnection(); HDBC GetConnHdl() { return m_hConnHdl; } BOOL IsDisableStatementScanning() { return m_bDisableStatementScanning; } void DisableStatementScanning(BOOL bDisable = TRUE) { m_bDisableStatementScanning = bDisable; } // startup/shutdown functions static void InitODBC(); static void DeInitODBC(); // connection functions BOOL Connect(LPCSTR cpDSN, LPCSTR cpUID, LPCSTR cpPWD); BOOL Disconnect(); // statement functions DBStatement *ExecSQL(LPCTSTR cpSQLText, DBStatement *pStmt = NULL); // Error Functions long GetErrorCode() { return m_lErrorCode; } LPSTR GetErrorMessage() { return m_caErrorMsg; } long GetErrorInfo(LPSTR cpErrorText) { if (cpErrorText) strcpy(cpErrorText,m_caErrorMsg); return m_lErrorCode; } void SetErrorCode(long lErrorCode = 0) { m_lErrorCode = lErrorCode; } void SetErrorText(LPCSTR cpErrorText) { strcpy(m_caErrorMsg,cpErrorText); } void SetErrorInfo(long lErrorCode, LPCSTR cpErrorText) { m_lErrorCode = lErrorCode; strcpy(m_caErrorMsg,cpErrorText); } void ResetError() { if (m_lErrorCode) { m_lErrorCode = 0; m_caErrorMsg[0] = 0; } } void UpdateDBError(HSTMT pStmt = NULL); }; class AFX_EXT_CLASS DBStatement { protected: HSTMT m_hStmtHdl; DBConnection *m_pDBConn; char m_caDataBuf[STATIC_BUFFER_SIZE+1]; BOOL m_bIsDataNull; BOOL m_bStatementActive; SYSTEMTIME m_sDateTime; void InitDataBuffer() { m_caDataBuf[0] = 0; } public: DBStatement(DBConnection *pDBConn); virtual ~DBStatement(); HSTMT GetStmtHdl() { return m_hStmtHdl; } DBConnection *GetDBConn() { return m_pDBConn; } BOOL AllocStatement(); BOOL EndSQL(); BOOL IsStatementActive() { return m_bStatementActive; } void SetStatementActive() { m_bStatementActive = TRUE; } // data retrieval functions char
-
The following classes should help you. The first is the .H file, followed by the .cpp. #if !defined(_DBCONN_H_) #define _DBCONN_H_ #ifdef RETCODE #undef RETCODE #endif #ifndef FAR #define FAR __far #endif #ifdef ODBCVER #undef ODBCVER #endif #define ODBCVER 0x0100 #include #include #include #define STATIC_BUFFER_SIZE 1024 class DBStatement; class AFX_EXT_CLASS DBConnection { protected: HDBC m_hConnHdl; long m_lErrorCode; char m_caErrorMsg[SQL_MAX_MESSAGE_LENGTH]; BOOL m_bDisableStatementScanning; public: static HENV m_hEnvironment; DBConnection(); virtual ~DBConnection(); HDBC GetConnHdl() { return m_hConnHdl; } BOOL IsDisableStatementScanning() { return m_bDisableStatementScanning; } void DisableStatementScanning(BOOL bDisable = TRUE) { m_bDisableStatementScanning = bDisable; } // startup/shutdown functions static void InitODBC(); static void DeInitODBC(); // connection functions BOOL Connect(LPCSTR cpDSN, LPCSTR cpUID, LPCSTR cpPWD); BOOL Disconnect(); // statement functions DBStatement *ExecSQL(LPCTSTR cpSQLText, DBStatement *pStmt = NULL); // Error Functions long GetErrorCode() { return m_lErrorCode; } LPSTR GetErrorMessage() { return m_caErrorMsg; } long GetErrorInfo(LPSTR cpErrorText) { if (cpErrorText) strcpy(cpErrorText,m_caErrorMsg); return m_lErrorCode; } void SetErrorCode(long lErrorCode = 0) { m_lErrorCode = lErrorCode; } void SetErrorText(LPCSTR cpErrorText) { strcpy(m_caErrorMsg,cpErrorText); } void SetErrorInfo(long lErrorCode, LPCSTR cpErrorText) { m_lErrorCode = lErrorCode; strcpy(m_caErrorMsg,cpErrorText); } void ResetError() { if (m_lErrorCode) { m_lErrorCode = 0; m_caErrorMsg[0] = 0; } } void UpdateDBError(HSTMT pStmt = NULL); }; class AFX_EXT_CLASS DBStatement { protected: HSTMT m_hStmtHdl; DBConnection *m_pDBConn; char m_caDataBuf[STATIC_BUFFER_SIZE+1]; BOOL m_bIsDataNull; BOOL m_bStatementActive; SYSTEMTIME m_sDateTime; void InitDataBuffer() { m_caDataBuf[0] = 0; } public: DBStatement(DBConnection *pDBConn); virtual ~DBStatement(); HSTMT GetStmtHdl() { return m_hStmtHdl; } DBConnection *GetDBConn() { return m_pDBConn; } BOOL AllocStatement(); BOOL EndSQL(); BOOL IsStatementActive() { return m_bStatementActive; } void SetStatementActive() { m_bStatementActive = TRUE; } // data retrieval functions char
-
Why not?
-
Why not?
below are the errors it gives me. Don't worry about it. I'm sick of trying to figure this out. If they want it done, they can do it themselves. :rolleyes: c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : error C2146: syntax error : missing ';' before identifier 'SQLHWND' c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : fatal error C1004: unexpected end of file found PointerTestMain.cpp c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : error C2146: syntax error : missing ';' before identifier 'SQLHWND' c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : fatal error C1004: unexpected end of file found dbconn.cpp c:\parsells\c++ testing\ddl checker db connect\dbconn.h(54) : error C2143: syntax error : missing ';' before '*' c:\parsells\c++ testing\ddl checker db connect\dbconn.h(54) : error C2501: 'DBStatement' : missing storage-class or type specifiers c:\parsells\c++ testing\ddl checker db connect\dbconn.h(54) : error C2061: syntax error : identifier 'DBStatement' c:\parsells\c++ testing\ddl checker db connect\dbconn.h(54) : error C2501: 'ExecSQL' : missing storage-class or type specifiers c:\parsells\c++ testing\ddl checker db connect\dbconn.cpp(952) : error C2511: 'ExecSQL' : overloaded member function 'class DBStatement *(const char *,class DBStatement *)' not found in 'DBConnection' c:\parsells\c++ testing\ddl checker db connect\dbconn.h(24) : see declaration of 'DBConnection' ICXC NIKA