admin 管理员组

文章数量: 1087820

VC++控制台程序实现数据库操作

最近写了点C++课的大作业,用了这个,主要参考了这两个资源


.html

真得感谢这两个人,废话不说了,上代码

// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;void ViewAll()
{_ConnectionPtr m_pConnection;HRESULT hr;hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象if(SUCCEEDED(hr)){hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;\Data Source=student.mdb","","",adModeUnknown);///连接数据库}_RecordsetPtr m_pRecordset;m_pRecordset.CreateInstance("ADODB.Recordset");_CommandPtr m_pCommand;m_pCommand.CreateInstance("ADODB.Command");_variant_t vNULL;vNULL.vt = VT_ERROR;vNULL.scode = DISP_E_PARAMNOTFOUND;m_pCommand->ActiveConnection = m_pConnection;m_pCommand->CommandText = "SELECT * FROM stu";m_pRecordset = m_pCommand->Execute(&vNULL,&vNULL,adCmdText);_variant_t vid,vstu_name,vstu_id,vstu_class,vstu_sex,score;m_pRecordset->MoveFirst();int i=0;printf("%d\t%s\t%s\t%s\t%s\t%s\n",i,"stu_name","stu_id","stu_class","stu_sex","score");while(!m_pRecordset->adoEOF){vid = m_pRecordset->GetCollect("ID");vstu_name = m_pRecordset->GetCollect("stu_name");vstu_id = m_pRecordset->GetCollect("stu_id");vstu_class = m_pRecordset->GetCollect("stu_class");vstu_sex = m_pRecordset->GetCollect("stu_sex");score = m_pRecordset->GetCollect("score");printf("%d\t%s\t%s\t%s\t%s\t%s\n",i++,(char*)(_bstr_t)vstu_name,(char*)(_bstr_t)vstu_id,(char*)(_bstr_t)vstu_class,(char*)(_bstr_t)vstu_sex,(char*)(_bstr_t)score);m_pRecordset->MoveNext();}m_pRecordset->Close();m_pConnection->Close();return;
}
void DeleteOne()
{printf("the fast way to delete one is input the ID number in the all information");printf("For example: 20\n");_ConnectionPtr m_pConnection;HRESULT hr;hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象if(SUCCEEDED(hr)){hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;\Data Source=student.mdb","","",adModeUnknown);///连接数据库}_RecordsetPtr m_pRecordset;m_pRecordset.CreateInstance("ADODB.Recordset");m_pRecordset->Open("SELECT * FROM stu",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);int IdToDelete;scanf_s("%d",&IdToDelete);m_pRecordset->Move(IdToDelete);printf("delete this guy: %s?\n",(char*)(_bstr_t)m_pRecordset->GetCollect("stu_name"));printf("for sure, input 1, else input 0\n");int forsure = 0;scanf_s("%d",&forsure);if(forsure==1){try{m_pRecordset->Delete(adAffectCurrent);m_pRecordset->MoveLast();}catch(_com_error& e){printf("aaa,%s",(char*)e.Description());}m_pRecordset->Update();m_pRecordset->Close();m_pConnection->Close();printf("delete it!\n");return;}return;
}
void AddOne()
{printf("If you don't want to make it crash, please input the information in this order with blank\n");printf("%s\t%s\t%s\t%s\t%s\n","stu_name","stu_id","stu_class","stu_sex","score");_ConnectionPtr m_pConnection;HRESULT hr;hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象if(SUCCEEDED(hr)){hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;\Data Source=student.mdb","","",adModeUnknown);///连接数据库}_RecordsetPtr m_pRecordset;m_pRecordset.CreateInstance("ADODB.Recordset");m_pRecordset->Open("SELECT * FROM stu",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);char infor[5][19];for(int i=0;i<5;i++){scanf_s("%s",infor[i],18);}for(int i =0;i<5;i++)printf("%s ",infor[i]);m_pRecordset->MoveLast();m_pRecordset->AddNew();m_pRecordset->PutCollect("stu_name",_variant_t(infor[0]));m_pRecordset->PutCollect("stu_id",_variant_t(infor[1]));m_pRecordset->PutCollect("stu_class",_variant_t(infor[2]));m_pRecordset->PutCollect("stu_sex",_variant_t(infor[3]));m_pRecordset->PutCollect("score",_variant_t(infor[4]));m_pRecordset->Update();m_pRecordset->Close();m_pConnection->Close();return;
}
void EditOne()
{printf("the fast way to edit one is input the ID number in the all information");printf("For example: 20\n");_ConnectionPtr m_pConnection;HRESULT hr;hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象if(SUCCEEDED(hr)){hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;\Data Source=student.mdb","","",adModeUnknown);///连接数据库}_RecordsetPtr m_pRecordset;m_pRecordset.CreateInstance("ADODB.Recordset");m_pRecordset->Open("SELECT * FROM stu",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);int IdToDelete;scanf_s("%d",&IdToDelete);m_pRecordset->Move(IdToDelete);printf("Input which column you want to change, and the new information of that\n");printf("for example:2 11065555\n");int Column;char change[19];scanf_s("%d",&Column);scanf_s("%s",change,18);switch (Column){case 1:{m_pRecordset->PutCollect("stu_name",_variant_t(change));break;}case 2:{m_pRecordset->PutCollect("stu_id",_variant_t(change));break;}case 3:{m_pRecordset->PutCollect("stu_class",_variant_t(change));break;}case 4:{m_pRecordset->PutCollect("stu_sex",_variant_t(change));break;}case 5:{m_pRecordset->PutCollect("score",_variant_t(change));break;}default:break;}m_pRecordset->Update();m_pRecordset->Close();m_pConnection->Close();
}
int _tmain(int argc, _TCHAR* argv[])
{::CoInitialize(NULL);int mission;while(true){printf("This is my system of your student's information! Please input the right number\n");printf("View all student's information, please input 1\n");printf("Delete a student's information, please input 2\n");printf("Add a student's information, please input 3\n");printf("Change a student's information. please input 4\n");printf("Exit, please input 5\n");scanf_s("%d",&mission);switch (mission){case 1:{ViewAll();	  break;}case 2:{DeleteOne();break;}case 3:{AddOne();break;}case 4:{EditOne();break;}case 5:{return 0;}default:break;}}return 0;
}


本文标签: VC控制台程序实现数据库操作