Solved

How to access an sql database from a c++ program

Posted on 2011-02-13
15
373 Views
Last Modified: 2012-05-11
I am a beginner programmer. So please, keep it simple.
I use MS VS.
I need to access an sql table and print (cout<<) the first 5 lines of the table in a table format.
the sql table has 6 columns and a very large number of rows.

Thanks for the help.
0
Comment
Question by:pgmerLA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
15 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 34884194
Generally, the simplest way to access a database from C++ is to use an ODBC driver. All the popular databases have drivers available.

http://en.wikipedia.org/wiki/Open_Database_Connectivity
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34884206
Check the following article it has a working sample code:

http://www.codeproject.com/KB/mcpp/adodemo.aspx
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34884217
@wdosanjos, that example is for managed c++. The question has been asked in the non-managed C++ zone.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34884224

Here is another set of tutorials
http://www.caspercomsci.com/pages/mfcsource.htm#10
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34884231
Additional info on using ODBC to access MS SQL.
http://msdn.microsoft.com/en-us/library/ms811006.aspx

General info on using ODBC
http://support.microsoft.com/kb/110093

More info on developing with ODBC
http://msdn.microsoft.com/en-us/library/aa198030(v=sql.80).aspx
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34884241
ewangoya, again that documentation is for managed C++ and yet the question is not in a managed C++ zone. It is also for Access databases yet the question is in the SQL 2008 zone.

Please think before you just Google for links and post them.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34884311

@evilrix
The zone is C++ Programming Language, Unless I don't know how to read which I think I do,there is nothing that says unmanaged code

The tutorial is on MS Access, so what?
This is generally how to connect to a database. It does not matter which database you connect to, you learn how to use ODBC or OLEDB, you will be fine for any kind of database.

I don't like being rude
Be open minded, there are many ways to look at solutions, not just what you think is right
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34884359
>>  I do,there is nothing that says unmanaged code
The "C++ Programming" zone is unmanaged C++. There is a separate managed C++ zone.
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CPP/

Absolutely nothing about this question indicates a managed solution is required.

>> The tutorial is on MS Access, so what?
The question is in the SQL Server 2008 zone so providing links on how to connect to an Access database is a little of the mark specially when there is plenty of documentation in the MSDN on how to connect to an MS SQL database using ODDB or OLEDB.
0
 
LVL 8

Expert Comment

by:lomo74
ID: 34924084
ok the first post was a copy paste from MSDN -
this one was written from scratch -
I cannot understand why you insist on removing my posts, since this is the most *basic* example I can imagine.
so any attempt to write down a simple example of connect to SQL/fetch data/print out must be considered plagiarism?
can you write down a simple example that does not look like the one found on MSDN?
0
 
LVL 8

Expert Comment

by:lomo74
ID: 34924392
put it this way.
anyone writing

#include <stdio.h>
int main(void)
{
  printf("Hello, World!\n");
}

is plagiarizing Mr Kernighan and Mr Ritchie.
0
 
LVL 3

Expert Comment

by:gopisera
ID: 34933338
In order to access the sql table from c.

See the proc  document of mircosoft vc++.

'PROC'  helps you to connect to the database and acess the tables.
0
 

Author Comment

by:pgmerLA
ID: 34934202
could you give me a simple sample code using 'PROC'?

Thanks.
0
 
LVL 3

Expert Comment

by:gopisera
ID: 34942765
Sure .....  

Here is the sample code for proc

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>


EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[80];
VARCHAR pwd[20];
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA.H;

void main()
{

   int option,tmp;

   extern int openaccount();
   extern void display();
   extern int deposit();


   strcpy(uid.arr,"home");
   uid.len =strlen(uid.arr);
   strcpy(pwd.arr,"ramesh");
   pwd.len = strlen(pwd.arr);

   EXEC SQL WHENEVER SQLERROR GOTO errexit;
   EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;

   printf("Connected to Oracle");
      
      
   
   do
      {
            printf("\n            Main Menu              \n");
            printf("1.Opening an Account\n");
            printf("2.Display Account Details \n");
            printf("3.Deposit\n");
            printf("4.With Draw\n");
            printf("5.Exit");
            printf("Enter your option \t:\t");
            scanf("%d",&option);
            switch(option)
            {

                  case 1:

                                    tmp=openaccount();
                                    if(tmp==1)
                                           printf("You Have Not Opened the Account");
                                    else
                                          printf("Successfully you hv created the Account");
                                    break;

                  case  2:

                                    display();
                                    break;

                  case 3:
                                    tmp=deposit();
                                    if(tmp==1)
                                           printf("Not Deposited the money");
                                    else
                                          printf("Successfully you hv Deposited the money in the Account");

                                    break;

            /*      case 4:
                                    
                                    withdraw();
                                    break;          */
                  
                  case 5:
                                    exit(0);


                  default:
                                    printf("\nYou Have Entered the wrong option\n");
                                    printf("\n Enter the current option");
            }

      }while(1);

 
   goto end;
errexit:

   printf("Error: %70s", sqlca.sqlerrm.sqlerrmc);

end:
   printf("Program completed");




}


Will give you more information.....  with in an hour
0
 
LVL 3

Accepted Solution

by:
gopisera earned 500 total points
ID: 34942969
Please find more details about ProC in the documents.

Which give you a clear cut idea.

Let me know if you faces any issues still....

Will clear your doubts....
#include <stdio.h>
#include <stdlib.h>

EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[80];
VARCHAR pwd[20];
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA.H;
void main()
{
   strcpy(uid.arr,"SCOTT");
   uid.len =strlen(uid.arr);
   strcpy(pwd.arr,"TIGER");
   pwd.len = strlen(pwd.arr);

   EXEC SQL WHENEVER SQLERROR GOTO errexit;
   EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;

   printf("Connected to Oracle");
   goto end;

errexit:
   printf("Error: %70s", sqlca.sqlerrm.sqlerrmc);
end:
   printf("Program completed");
}

Open in new window

ProC.pdf
Compiling-ProC-Programs.doc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

726 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