赞
踩
首先需要新建一个access文件。
//主函数 #include<iostream> #include<map> #include <string> #include "inquire.h" using namespace std; void main() { CoInitialize(NULL); //初始化环境 inquire Inquire("xxx.mdb");//填自己存放的access文件地址,记得不能写错。 if ((Inquire.getmyerror().length() )!= 0) { cout << "数据库连接失败"; cout << Inquire.getmyerror(); getchar(); } //下面是三种操作对数据库进行添加删除和转化为数组形式查询 //① 添加操作 /* map<int, string> add; cin >> add[0]; cin >> add[1]; cin >> add[2]; Inquire.insertion("表1",add); if (Inquire.getmyerror().length() == 0) { cout << "插入成功\n"; getchar(); } else { cout <<Inquire.getmyerror(); getchar(); } */ //② 删除操作 /* map<int, string> delet; cin >> delet[0];//表名 cin >> delet[1];//删除条件 if(!Inquire.Delete(delet[0],delet[1])) { cout<<"删除失败"; cout<<Inquire.getmyerror(); getchar(); } */ //③查询表格 /* map<int ,map<int,string>>check; Inquire.check_table(check); if (Inquire.getmyerror().length() == 0) { for (int i = 0; i < check.size(); i++) { map<int, string> aaa = check[i]; for (size_t u = 0; u < aaa.size(); u++) { string str = aaa[u]; cout << str.c_str()<<"\t"; } cout << "\n"; } getchar(); } else { cout << Inquire.getmyerror(); getchar(); } */ CoUninitialize(); return ; } //类的头文件 #pragma once #include<map> #include<iostream> #include<string> #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF") using namespace std; class inquire { public: inquire(string phat); ~inquire(); string getmyerror(); void insertion(string, map<int, string>); bool Delete(string,string); void check_table(map<int, map<int, string>> &mp); private: _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; HRESULT hr; string m_error; }; //函数的类 #include "inquire.h" inquire::inquire(string phat) { m_error = ""; try { hr = m_pConnection.CreateInstance("ADODB.Connection");//ADO方式连接 if (SUCCEEDED(hr)) { m_pConnection->ConnectionTimeout = 5; string 路径; 路径 = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + phat; hr = m_pConnection->Open(路径.c_str(), "", "", adModeUnknown); } } catch (_com_error e)///捕捉异常 { string m_error= e.ErrorMessage(); } } inquire::~inquire() { if (m_pConnection->State) { m_pConnection->Close(); m_pConnection = NULL; } } string inquire::getmyerror() { return m_error; } void inquire::insertion(string table_name,map<int,string> train_value) { m_error = ""; try { _variant_t RecordsAffected; string sql; sql = "INSERT INTO " + table_name + "( name,old,height)VALUES("; for (size_t i = 0; i < train_value.size(); i++) { sql += train_value[i] + ","; } sql = sql.substr(0, sql.length() - 1) + ")"; m_pConnection->Execute(sql.c_str(), &RecordsAffected, adCmdText); } catch (_com_error e)///捕捉异常 { m_error = e.ErrorMessage(); } } bool inquire::Delete(string table_name, string condition)//删除 { m_error = ""; try { _variant_t RecordsAffected; string shanchu; shanchu = "DELETE FROM " + table_name + " WHERE " + condition; m_pConnection->Execute(shanchu.c_str(), &RecordsAffected, adCmdText); return true; } catch (_com_error e)///捕捉异常 { m_error = e.ErrorMessage(); return false; } } void inquire::check_table(map<int, map<int, string>> &mp) { try { m_error = ""; m_pRecordset.CreateInstance(_uuidof(Recordset)); char sql[300]; memset(sql, 0, 300); strcat(sql, "SELECT * FROM 表1"); m_pRecordset->Open(sql, (IDispatch*)m_pConnection, adOpenDynamic, adLockOptimistic, adCmdText); } catch (_com_error e)///捕捉异常 { m_error = e.ErrorMessage(); } try { if (m_pRecordset->BOF) { printf("表内数据为空"); if (m_pConnection->State) { m_pRecordset->Close(); m_pRecordset = NULL; m_pConnection->Close(); m_pConnection = NULL; } CoUninitialize(); return; } cout << "this is my database \n"; m_pRecordset->MoveFirst(); int i,u=0 ; while (!m_pRecordset->adoEOF) { int row = m_pRecordset->GetFields()->Count; _variant_t var[100]; for (i = 0; i < row; i++) { var[i] = m_pRecordset->GetCollect(_variant_t((long)i)); if (var[i].vt != VT_NULL) { mp[u][i] = _com_util::ConvertBSTRToString((_bstr_t)var[i]); } else { mp[u][i] = ""; } } m_pRecordset->MoveNext(); u++; } } catch (_com_error e)///捕捉异常 { m_error = e.ErrorMessage(); } }
这是简单数据库操作,望采纳!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。