Important alert: (current site time 7/15/2013 5:12:38 PM EDT)
 

VB icon

Access MDB

Email
Submitted on: 3/4/2001 2:55:09 PM
By: Mokarrabin A Rahman  
Level: Beginner
User Rating: By 11 Users
Compatibility: C, C++ (general), Microsoft Visual C++
Views: 72190
(About the author)
 
     To open MS Access or Ms SQL server database using C/C++ or VC++
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Access MDB
// Description:To open MS Access or Ms SQL server database using C/C++ or VC++
// By: Mokarrabin A Rahman
//
// Returns:Shows some recordsets
//
// Assumes:Change the name of the database to any database and place it in the same directory as the .exe. You shoul also change the SQL to your particular database. You may change the connect string to connect to MS SQL Server.
//
// Side Effects:Great
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1371&lngWId=3//for details.//**************************************

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
// This code comes from : www.geocities.com/mokarrabin
#include <stdio.h>
#include <iostream.h>
void main(void)
{
CoInitialize(NULL);
try 
{
_RecordsetPtr pRst("ADODB.Recordset");
 // Connection String
_bstr_t strCnn("DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;DBQ=GBOOK.mdb");
	 // Open table
	pRst->Open("SELECT * FROM ProductService where ProductService like '%samir%';", strCnn, adOpenStatic, adLockReadOnly, adCmdText);
	 
	 pRst->MoveFirst();
	 while (!pRst->EndOfFile) {
		 cout<<(char*) ((_bstr_t) pRst->GetFields()->GetItem("ProductService")->GetValue())<<endl;
		 pRst->MoveNext();
	 }
	 pRst->Close(); 
	 
}
catch (_com_error &e)
{
 cout<<(char*) e.Description();
}
::CoUninitialize();
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/10/2001 9:49:37 AMBrent Humber

#import, can this be used to open any program? My email address is brentthumber@netzero.net
(If this comment was disrespectful, please report it.)

 
4/17/2001 12:38:37 PMben tot

Why am I getting this error? How do I fix it?

LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/ADO Application.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
(If this comment was disrespectful, please report it.)

 
5/4/2001 1:40:17 PMVandana Anandan

Thanks a lot. It was very nice of you to give me all this information.
In general I have understood the logic and method. Now where can I get more information on the syntax used. For eg.:
UID=admin;DBQ=GBOOK.mdb
_bstr_t strCnn
MoveFirst();
_RecordsetPtr pRst(
(If this comment was disrespectful, please report it.)

 
9/24/2001 3:39:18 AMPavan

I am trying to run this program with borland c 5.5 and using MS-Access 2000 but this is the first error i get.all other errors r related to this only.kindly please help me in this regard my id is nvspavano@rediffmail.comBorland C 5.5.1 for Win32 Copyright (c) 1993, 2000 Borlanddatatest.cpp:Error E2503 datatest.cpp 25: Missing or incorrect version of TypeLibImport.dllError E2268 datatest.cpp 36: Call to undefined function 'CoInitialize' in function main().............
(If this comment was disrespectful, please report it.)

 
2/21/2002 5:55:25 PMHasan Rashid

Can i use ... C++ builder command line tool to compile it?
(If this comment was disrespectful, please report it.)

 
3/27/2002 9:04:54 AMadamu@nuc.edu.ng

I tried to compile the program. But I recieve series of errors(25 in no.).

1. 'Invalid or undefine directive #' This appears for the first line of #import ......
etc..
Please what could have been the problem?

Waiting to hearing from you.

Thanks a lot.
(If this comment was disrespectful, please report it.)

 
7/17/2002 4:08:27 PMsonitus artifex

#import can only be used by a c++ compiler, so you *can't* compile it as c only!!!
(If this comment was disrespectful, please report it.)

 
11/19/2002 11:56:58 PM

hallo mr rahman, i'm student of application.in level 1.i'm beginer... I wish to know from you how to make database(create) or get the database name from c: or A: i had been tried your code by it said cannot find database...i'm sorry if my english is bad
(If this comment was disrespectful, please report it.)

 
1/27/2003 7:15:04 PMHasan Rashid

People to compile this u need to have a C++ compiler preferebly MS Visual C++. Secondly, This code requires the presence of DLL in the mentioned folder. You have to make sure the DLL exists.
Regards,
Hasan Rashid.
(If this comment was disrespectful, please report it.)

 
1/29/2003 6:07:59 PM

I'm having similar trouble. In Dev-cpp, from bloodshed or mingw software - the compiler doesn't recognize the "#import" instruction. The dll does exist in the directory named in the code.
the line 'no_namespace rename("EOF", "EndOfFile")' also produces an error. many errors follow, but i suspect they are bogus due to these early problems. Would appreciate your response.


(If this comment was disrespectful, please report it.)

 
2/24/2003 4:22:01 AM

HI MR RAHMAN...i`m new in c++ language..i need to call a text files from database(access or sql).i cant call the table field in data base..can u help me with the coding..pls
(If this comment was disrespectful, please report it.)

 
3/5/2003 3:00:41 AM

Dear Mokarrabin A Rahman,
It is very usefull code. I could test it using VC++ compiler. How do you compile with DJGPP(gcc) compiler?

many thanks
and Best regads
Abdugani
(If this comment was disrespectful, please report it.)

 
3/10/2003 10:50:21 PM

hi sir mokkarrabin... i'm a bigginner... i've made database app using vb once... the coding and procedure here is just quite similar..... using connection and recordset... is the code original from and for c and c++ or ?.....
(If this comment was disrespectful, please report it.)

 
10/28/2003 4:23:49 AM

Can I run this program in turboc++ 3.0, if not in which version should i run?
I am getting an error when using the import directive. I want to know whether databse can be linked in turboc++ version 3.0, if possible how to correct the error.
(If this comment was disrespectful, please report it.)

 
12/22/2003 5:13:03 PM

good programs
(If this comment was disrespectful, please report it.)

 
11/19/2004 1:55:11 AM

i want to know how to display data from ms access that generate with C
(If this comment was disrespectful, please report it.)

 
5/29/2005 11:13:30 AM

i am running the program under turbo C

shows error:
1)" unknown preprocessor directive: 'import' "
2)" must use c++ for the type of iostream "
(If this comment was disrespectful, please report it.)

 
5/6/2006 5:24:01 PMJoel Douglas

Question: If I am dealing with tthe MFC classes, which file should this code be used in?
(If this comment was disrespectful, please report it.)

 
9/29/2007 11:57:01 AMKirankumar

Dear Sir,
I wish to know simple code for calling database file in c++. Database file eithe Access or Fox. I am working in the Engineering college-Mechanical dept. and wish to give idea to the student. If u know please convey me code. I feel obliged for it.
Thanking you,
Prof. k. m. bhuptani
(If this comment was disrespectful, please report it.)

 
11/29/2007 10:29:26 AMEtta

I'm facing a problem with #import and main... :( please help me, Sir...
(If this comment was disrespectful, please report it.)

 
1/10/2008 8:41:49 AMThamarai Selvan

i am running the program under turbo C++

shows error:(about 25)
1)" unknown preprocessor directive: 'import' "
2)" must use c++ for the type of iostream "
please let me know the method of executing the program........

(If this comment was disrespectful, please report it.)

 
12/28/2009 9:09:52 PMIman

Hello, I would like to ask about this piece of code :
//_bstr_t strCnn("DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;DBQ=GBOOK.mdb");//

DBQ=GBOOK.mdb -> where is the exact location of GBOOK access file? Is it must be in the same location with c:\Program Files\Common Files\System\ADO? No right

Because i get an error "Microsoft Access Driver] Could not find file.
(If this comment was disrespectful, please report it.)

 
12/28/2009 9:16:18 PMIman

Dear Mr. Mokarrabin,
Thanks for sharing with us this very useful code. But i think i got a problem with this

("DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;DBQ=GBOOK.mdb");

where is the exact location of GBOOK.mdb file? and how to find the UID= is it all default to admin?

Because i got Microsoft Access Driver] File not found

I used Visual C++ programming
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.