There is an MFC program specially designed for this database course in my space. You can refer to it
# include & ltole 2 . h & gt;
# include & ltiostream.h & gt
# import " c:\ Program Files \ Common Files \ System \ ADO \ msado 15 . dll "
no_namespace rename ("EOF "," EndOfFile ")
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
_ ConnectionPtr pConnection
HRESULT hr
_ RecordsetPtr pRecordset
_ bstr _ t sql//SQL statement
_ bstr _ t sqlCount//
_ variant _ t vCount// Count the number of records
_variant_t record is affected;
Invalid count ()
{
//Execute the SQL statistics command to obtain a record set containing the number of records.
pRecordset = p connection-& gt; Execute (sqlcount &; RecordsAffected,ADC mdtext);
_ variant _ t vin dex =(long)0;
//Get the value of the first field and put it in the vCount variable.
vCount = pRecordset-& gt; get collect(vin dex);
//pRecordset-& gt; close();
Cout & lt& lt“* * * * Yes "
}
Invalid account book ()
{
_variant_t vBookName,vAuthor,vWord,vPricing,vCategory,vISBN,vPrintTime
attempt
{
And (! Pre-recorded set->; EndOfFile)
{
//Get the value of each column, counting from 0.
vBookName = pRecordset-& gt; get collect(" BookName ");
vAuthor = pRecordset-& gt; get collect(" Author ");
vWord = pRecordset->。 get collect(" Word ");
vPricing = pRecordset-& gt; get collect(" Pricing ");
vCategory = pRecordset-& gt; GetCollect ("category");
vISBN = pRecordset->。 get collect(" ISBN ");
vPrintTime = pRecordset-& gt; get collect(" print time ");
///Output the records in the recordset
if( vBookName.vt! = VT_NULL)
{
Cout & lt& lt "Title:"
& lt& lt "Author:"
& lt& lt ":"< & ltvWord.lVal & lt& lt ""
& lt& lt "Price:"
< < "Category:"
& lt& lt" ISBN:" & lt; & lt(_ bstr _ t)vISBN & lt; & lt" "
& lt& lt "Publication date:"
}
Pre-recorded set->; MoveNext(); ///Move to the next record
}
}
catch (_com_error* e)
{
cout & lt& lte-& gt; error message()& lt; & ltendl
}
}
Invalid print ()
{
_variant_t vPrintNo,vBookName,vMaterialNo,vVolume,vPrintPrice
attempt
{
And (! Pre-recorded set->; EndOfFile)
{
//Get the value of each column, counting from 0.
vPrintNo = pRecordset-& gt; get collect(" print no ");
vBookName = pRecordset-& gt; get collect(" BookName ");
vMaterialNo = pRecordset-& gt; get collect(" material no ");
vVolume = pRecordset-& gt; get collect(" Volume ");
vprintprise = pRecordset-& gt; get collect(" print price ");
///Output the records in the recordset
if( vPrintNo.vt! = VT_NULL)
{
Cout & lt& lt "Print job number:"
& lt& lt "Title:"
& lt& lt "Material number:"
& lt& lt "Number of prints:"
& lt& lt "Printing cost:"
}
Pre-recorded set->; MoveNext(); ///Move to the next record
}
}
catch (_com_error* e)
{
cout & lt& lte-& gt; error message()& lt; & ltendl
}
}
Void material ()
{
_variant_t vMaterialNo,vMaterialName,vUnitPrice
attempt
{
And (! Pre-recorded set->; EndOfFile)
{
//Get the value of each column, counting from 0.
vMaterialNo = pRecordset-& gt; get collect(" material no ");
vMaterialName = pRecordset-& gt; get collect(" material name ");
vUnitPrice = pRecordset-& gt; GetCollect ("unit price");
///Output the records in the recordset
if( vMaterialNo.vt! = VT_NULL)
{
Cout & lt& lt "Material number:"
& lt& lt "Material name:"
& lt& lt "Material price:"
}
Pre-recorded set->; MoveNext(); ///Move to the next record
}
}
catch (_com_error* e)
{
cout & lt& lte-& gt; error message()& lt; & ltendl
}
}
void RunG();
void SelectTable()
{
int nSelect
While (true)
{
Cout & lt& lt "There are three tables in the database:"
& lt& lt" \ t 1->; Book "
& lt& lt" \ T2->; Print "
& lt& lt" \ T3->; Materials "
& lt& lt" \ t0-& gt; Go back to the previous menu "
& lt& lt "Please select:";
CIN & gt; & gtn selection;
if (nSelect == 0)
{
cout & lt& ltendl
RunG();
}
if((n select & gt; 3)| |(n Select & lt- 1))
{
Cout & lt& ltendl & lt& lt "Wrong choice! Please re-enter! " & lt& ltendl
Continue;
}
Switch (n selection)
{
Case 1:
sql = " SELECT * FROM Book
sqlCount = " SELECT COUNT(*)FROM Book ";
Cout & lt& lt "book";
Break;
Case 2:
sql = " SELECT * FROM Printing
sqlCount = " SELECT COUNT(*)FROM Printing "; //Print task
Cout & lt& lt "print";
Break;
Case 3:
sql = " SELECT * FROM Material
sqlCount = " SELECT COUNT(*)FROM Material ";
Cout < < "material";
Break;
}
count(); //////////
If (vCount.lVal == 0) // If the record is 0, it will not be traversed.
{
Return;
}
Precoding set. create instance(_ _ uuidof(Recordset));
Pre-recorded set->; Open(sql,_ variant _ t((IDispatch *)p connection,true),adOpenStatic,adLockOptimistic,ADC mdtext);
if (nSelect == 1)
{
book();
}
if (nSelect == 2)
{
Print (); //Print task
}
if (nSelect == 3)
{
Material ();
}
}
}
Empty step ()
{
int nSelect
bool inputFlag = false
Cout & lt& lt“* * * * * * * * Simple operation on database * * * * * "
& lt& lt" 1->; Query: ";
CIN & gt; & gtn selection;
And (! Input flag)
{
Switch (n selection)
{
Case 1:
select table();
inputFlag = true
Break;
Default value:
inputFlag = false
CIN . clear();
Cout & lt& lt "Wrong choice! Please re-enter: ";
CIN & gt; & gtn selection;
}
}
}
Invalid data ()
{
//* * * * Connect to the database * * * * * * *
attempt
{
_ bstr _ t sCon
sCon="Provider=Microsoft。 Jet. OLEDB.4.0 data source = mydb.mdb ";
Hr = connection. create instance(_ _ uuidof(Connection));
If (Success (hours))
{
Hr = connection-> Open(sCon,"","",admode unknown);
Cout & lt& lt "Connection to database successful!" & lt& ltendl
}
}
catch (_com_error* e)
{
cout & lt& lte-& gt; error message()& lt; & ltendl
}
/* * * * * * * Establish a data set * * * * * * *
/*
attempt
{
//Execute SQL command: CREATE TABLE creates a table book, which contains four fields:
//plastic book number, string author, plastic page, currency pricing, plastic category.
SQL = " CREATE TABLE Book(Book no INTEGER,Author TEXT,Page INTEGER,Pricing Currency,category no TEXT)";
P connection-> Execute (SQL&; RecordsAffected,ADC mdtext);
//Add a record to the table.
SQL = "insert into book (book number, author, page number, pricing, category number) value (1,' Washington', 250, 15.4,' novel') ";
P connection-> Execute (SQL&; RecordsAffected,ADC mdtext);
///All recorded pricing field values plus 1.
Sql = "Update SOB Pricing = Pricing+1";
P connection-> Execute (SQL&; RecordsAffected,ADC mdtext);
}
catch (_com_error* e)
{
cout & lt& lte-& gt; error message()& lt; & ltendl
} */
}
void CloseData()
{
//* * * * * Close the data set * * * * * * *
Pre-recorded set->; close();
pRecordset = NULL
//* * * * * Close the connection * * * * * * *
If(p connection->; Status)
{
P connection-> close();
}
pConnection = NULL
}
int main(int argc,char* argv[])
{
* common initialization (empty);
data();
RunG();
CloseData();
* coun initialize();
cout & lt& ltendl
System ("suspended");