求教C++连接数据库的方法
似乎可以在代码中通过sql语句对数据库进行操作?求这样的方法教程!或者相关的书籍!
#include <stdio.h> #include <windows.h> #include <string.h> #import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") //引入ADO库 int main(void) { _ConnectionPtr m_pConnection=NULL; CoInitialize(NULL); //初始化com库 HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection"); //创建Connection对象 if(SUCCEEDED(hr)) //创建数据库connection对象成功 { hr = m_pConnection->Open("Driver={Microsoft dBase Driver (*.dbf)}","","",adModeUnknown); if(SUCCEEDED(hr)) //连接数据库字符串成功 { _RecordsetPtr MySet; MySet.CreateInstance(__uuidof(Recordset)); MySet->Open("SELECT * FROM 123",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); _variant_t Holder; while(!MySet->adoEOF) { Holder = MySet->GetCollect("XMMC"); if(Holder.vt!=VT_NULL) printf("%s\n",(char*)_bstr_t(Holder)); MySet->MoveNext(); } } } return 0; }
#include<iostream> #include<string> using namespace std; #import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF") class ADOConn { //定义变量 public: _ConnectionPtr m_pConnection;//添加一个纸箱Connection对象的指针 _RecordsetPtr m_pRecordset;//添加一个指向Recordset对象的指针 //定义方法 public: ADOConn(){}; virtual ~ADOConn(){}; void OnInitADOConn();//初始化——连接数据库 _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);//执行查询 BOOL ExecuteSQL(_bstr_t bstrSQL);//执行sql语句,Insert Update_variant_t void ExitConnect(); }; void ADOConn::OnInitADOConn() { //初始化OLE/COM库环境 ::CoInitialize(NULL); //创建Connection对象 m_pConnection.CreateInstance("ADODB.Recordset"); //设置连接字符串,必须是BSTR型或_bstr_t类型 _bstr_t strConnect = "Provider=SQLOLEDB;Server=我的计算机;Database=Student;uid=sa;pwd=123456"; /**********************************************************************************************/ // 调试时在此处出错 /**********************************************************************************************/ m_pConnection->Open(strConnect, "", "", adModeUnknown); /**********************************************************************************************/ } _RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL) { //连接数据库,如果Connection对象为空,则重新连接数据库 if (m_pConnection == NULL) OnInitADOConn(); //创建记录集对象 m_pRecordset.CreateInstance(__uuidof(Recordset)); //取得表中记录 m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); return m_pRecordset;//返回记录集 } //BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL) //{ // try // { // //是否已经连接数据库 // if (m_pConnection == NULL) // OnInitADOConn(); // m_pConnection->Execute(bstrSQL, NULL, adCmdText); // // return true; // } // catch (_com_error e) // { // return false; // } //} // //void ADOConn::ExitConnect() //{ //关闭记录集和连接 // if (m_pRecordset != NULL) // m_pRecordset->Close(); // m_pConnection->Close(); // // ::CoUninitialize();//释放环境 //} void main() { string Sname; ADOConn m_AdoConn; m_AdoConn.OnInitADOConn(); //设置select语句 _bstr_t vSQL; vSQL = "select sname from Student"; _RecordsetPtr m_pRecordset; m_pRecordset = m_AdoConn.GetRecordSet(vSQL); Sname = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("sname"); cout << Sname; }
[此贴子已经被作者于2015-12-21 09:51编辑过]