Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Console Application with Database Connectivity

Console Application with Database Connectivity

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestiondata-structureshelptutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Unplugged
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • U Unplugged

      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

      B Offline
      B Offline
      basementman
      wrote on last edited by
      #2

      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

      U 1 Reply Last reply
      0
      • B basementman

        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

        U Offline
        U Offline
        Unplugged
        wrote on last edited by
        #3

        I can't get the files to compile. :~ ICXC NIKA

        B 1 Reply Last reply
        0
        • U Unplugged

          I can't get the files to compile. :~ ICXC NIKA

          B Offline
          B Offline
          basementman
          wrote on last edited by
          #4

          Why not?

          U 1 Reply Last reply
          0
          • B basementman

            Why not?

            U Offline
            U Offline
            Unplugged
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups