Solved

Database connectivity Through VC++

Posted on 2000-02-22
6
444 Views
Last Modified: 2010-04-10
i want to insert data,retrive data,delete data and updata data into my database using VC++.Please give sample code for the same.i have used VC++ but i have never done database connectivity using VC++.
0
Comment
Question by:mmamta
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2549041
What kind of database you want?
0
 

Author Comment

by:mmamta
ID: 2549201

SQL sever 7.0
0
 
LVL 1

Accepted Solution

by:
JMu earned 10 total points
ID: 2549241
I think that the points are not big enough for this kind of question, but here is the code anyway. I suppose you need ODBC code. This is NOT MFC stuff.

class CDemoODBC
{
      public:
            CDemoODBC();
            ~CDemoODBC();

            bool Init();

            bool ConnectToDatabase( const char* dataSourceName, const char* databaseUser, const char* databasePassword );
            void DisconnectFromDatabase();

            bool Save( const char* paino, const char* halkaisija );

      protected:
      private:
            CDemoODBC( const CDemoODBC& copyFrom );
            CDemoODBC& operator=( const CDemoODBC& copyFrom );

            void Close();

      // data
      public:
      protected:
      private:
            HENV envHandle;
            HDBC connHandle;
            bool connectedODBC;
            HSTMT statementHandle;
};



CDemoODBC::CDemoODBC()
:      envHandle( SQL_NULL_HENV ),
      connHandle( SQL_NULL_HDBC ),
      connectedODBC( false ),
      statementHandle( SQL_NULL_HSTMT )
{
}

CDemoODBC::~CDemoODBC()
{
      DisconnectFromDatabase();

      Close();
}

// -------------------------------------

bool CDemoODBC::Init()
{
      if ( ::SQLAllocEnv( &envHandle ) == SQL_SUCCESS )
      {
            if ( ::SQLAllocConnect( envHandle, &connHandle ) == SQL_SUCCESS )
            {
                  ::SQLSetConnectOption( connHandle, SQL_LOGIN_TIMEOUT, 5 );

                  return true;
            }
      }

      return false;
}

void CDemoODBC::Close()
{
      if ( connHandle != SQL_NULL_HDBC )
      {
            ::SQLFreeConnect( connHandle );
            connHandle = SQL_NULL_HDBC;
      }

      if ( envHandle != SQL_NULL_HENV )
      {
            ::SQLFreeEnv( envHandle );
            envHandle = SQL_NULL_HENV;
      }
}

bool CDemoODBC::ConnectToDatabase( const char* dataSourceName, const char* databaseUser, const char* databasePassword )
{
      RETCODE rc = ::SQLConnect( connHandle,
            (UCHAR*)dataSourceName, SQL_NTS,
            (UCHAR*)databaseUser, SQL_NTS,
            (UCHAR*)databasePassword, SQL_NTS );

      if ( rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO )
      {
            connectedODBC = true;

            if ( ::SQLAllocStmt( connHandle, &statementHandle ) == SQL_SUCCESS )
                  return true;
      }

      return false;
}

void CDemoODBC::DisconnectFromDatabase()
{
      if ( statementHandle != SQL_NULL_HSTMT )
      {
            ::SQLFreeStmt( statementHandle, SQL_DROP );
            statementHandle = SQL_NULL_HSTMT;
      }

      if ( connectedODBC )
      {
            ::SQLDisconnect( connHandle );
            connectedODBC = false;
      }
}

bool CDemoODBC::Save( const char* paino, const char* halkaisija )
{
      SDWORD painoLen = SQL_NTS;
      SDWORD halkaisijaLen = SQL_NTS;

      if ( ::SQLPrepare( statementHandle,
            (unsigned char*)"INSERT INTO Painojono "
            "(Pvmklo, Paino, Halkaisija) "
            "VALUES "
            "(GetDate(), ?, ?)", SQL_NTS ) == SQL_SUCCESS )
      {
            if ( ::SQLBindParameter( statementHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_FLOAT, 0, 0, (char*)paino, 0, &painoLen ) == SQL_SUCCESS )
            {
                  if ( ::SQLBindParameter( statementHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_FLOAT, 0, 0, (char*)halkaisija, 0, &halkaisijaLen ) == SQL_SUCCESS )
                  {
                        if ( ::SQLExecute( statementHandle ) == SQL_SUCCESS )
                        {
                              if ( ::SQLTransact( envHandle, SQL_NULL_HSTMT, SQL_COMMIT ) == SQL_SUCCESS )
                                    return true;
                        }
                  }
            }
      }

      return false;
}

JMu
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mmamta
ID: 2550340
JMU

thnaks for your reply but can you send the code with some explanation then it will be easy for me to understand and also can you send the code with sample database and .cpp or .c file  at my email address mayanks@vsnl.com
0
 

Author Comment

by:mmamta
ID: 2550357
With 'c' i have connected the database using ODBC i would like to connect to database using VC++ and that is MFC class please send the sample code for the same along with sample database which will be easy for me to understand.

Thnaks
Mayank
mayanks@vsnl.com
0
 
LVL 1

Expert Comment

by:JMu
ID: 2550371
OK. I'll send them when I go home. Please, wait few hours.

JMu
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now