C++对MS SQL Server的操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++对MS SQL Server的操作相关的知识,希望对你有一定的参考价值。
今天因为在做一份C++的期末作业,突然想用C++来链接数据库,实现数据的重复利用,所以就作死去百度搜了一下。
更巧的事情是,一搜居然还有很多搜索结果,然后就照着做了。
做的过程很艰辛,就不一一诉说了,就简单说两句,C++的类名很奇怪,然后就是参数名啥的非常奇怪,也没有相应的参数说明,什么参数啥的完全不懂。
然后就是慢慢的做,在类型转换和数据操作上废了点时间,其他的还好,就不多说了,直接上代码,因为注释很详细清楚。
不过,在看我的乱的要死的代码之前,建议先看看大神的代码。
第一份是遍历一个表的代码:
http://www.cnblogs.com/ha666/archive/2013/06/12/3133348.html
第二份是介绍详细的链接和代码编写:
http://blog.csdn.net/ljh56789/article/details/8061323
第三份的亮点在于介绍参数,我的代码就是基本仿照第三份改的
http://blog.csdn.net/zhangyulin54321/article/details/8056580
下面就是我的代码了,很丑别嫌弃
#pragma once #include <icrsint.h> #include<iostream> #include<iomanip> #include <string> using namespace std; #import "c:\\program files\\common files\\system\\ado\\msado15.dll" no_namespace rename("EOF", "adoEOF") int funeach() { CoInitialize(NULL); //这一步十分重要,没有这一步,将连不上服务器 _ConnectionPtr sqlSp; if (FAILED(sqlSp.CreateInstance(_uuidof(Connection)))) { cout << "_ConnectionPtr对象指针实例化失败!!!" << endl; return 0; } else { try { //设置连接字符串 _bstr_t strConnect = "Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=**********;database=homework;"; sqlSp->Open(strConnect, "", "", adModeUnknown); } catch (_com_error &e) { cout << e.Description() << endl; } _RecordsetPtr m_pRecordset; if (FAILED(m_pRecordset.CreateInstance(_uuidof(Recordset)))) { cout << "记录集对象指针实例化失败!" << endl; return 0; } try { //打开T_Member表 m_pRecordset->Open("select * from T_Member", (IDispatch*)sqlSp, adOpenDynamic, adLockOptimistic, adCmdText); } catch (_com_error &e) { cout << e.Description() << endl; } //查询数据 try { m_pRecordset->MoveFirst(); while (!m_pRecordset->adoEOF) { //遍历输出name列的值 string name = (char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("name"))->Value); cout << name << endl; m_pRecordset->MoveNext(); } } catch (_com_error &e) { cout << e.Description() << endl; } m_pRecordset->Close(); m_pRecordset = NULL; sqlSp->Close(); sqlSp = NULL; } } int funselect() { CoInitialize(NULL); _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try { if (FAILED(m_pConnection.CreateInstance(__uuidof(Connection)))) { cout << "_ConnectionPtr对象指针实例化失败!!!" << endl; return 0; } m_pConnection->Open("Provider=SQLOLEDB;Server=127.0.0.1;Database=homework;uid=sa; pwd=**********;", "", "", adModeUnknown); } catch (_com_error e) { cout << "连接数据库失败!错误信息:" << e.ErrorMessage() << endl; system("pause"); return 0; } try { if (FAILED(m_pRecordset.CreateInstance(_uuidof(Recordset)))) { cout << "记录集对象指针实例化失败!" << endl; return 0; } m_pRecordset->Open(_variant_t("select Id, name from T_Member where name=‘123‘"), _variant_t((IDispatch*)m_pConnection), adOpenKeyset, adLockOptimistic, adCmdText); } catch (_com_error &e) { cout << "处理失败1!错误信息:" << e.ErrorMessage() << endl; system("pause"); return 0; } _variant_t vEmployeeID, vFirstName, vLastName, vHireDate, vCity; try { while (!m_pRecordset->adoEOF) { vEmployeeID = m_pRecordset->GetCollect(_variant_t((long)0));//取得第1列的值,从0开始计数,你也可以直接列出列的名称,如下一行 vFirstName = m_pRecordset->GetCollect("Id"); vLastName = m_pRecordset->GetCollect("name"); //取值 string name = (char*)(_bstr_t)m_pRecordset->GetFields()->GetItem("name")->Value; cout << name; m_pRecordset->MoveNext(); } } catch (_com_error &e) { cout << "处理失败2!错误信息:" << e.ErrorMessage() << endl; system("pause"); return 0; } m_pRecordset->Close(); m_pRecordset = NULL; m_pConnection->Close(); m_pConnection = NULL; } int funupdate() { CoInitialize(NULL); _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try { m_pConnection.CreateInstance(__uuidof(Connection)); if (FAILED(m_pConnection.CreateInstance(__uuidof(Connection)))) { cout << "_ConnectionPtr对象指针实例化失败!!!" << endl; return 0; } m_pConnection->Open("Provider=SQLOLEDB;Server=127.0.0.1;Database=homework;uid=sa; pwd=**********;", "", "", adModeUnknown); } catch (_com_error e) { cout << "连接数据库失败!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } try { m_pRecordset.CreateInstance(__uuidof(Recordset)); if (FAILED(m_pRecordset.CreateInstance(_uuidof(Recordset)))) { cout << "记录集对象指针实例化失败!" << endl; return 0; } m_pRecordset->Open("select Id,name from T_Member where name=‘123‘", _variant_t((IDispatch*)m_pConnection), adOpenDynamic, adLockOptimistic, adCmdText); } catch (_com_error &e) { cout << "处理失败1!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } try { while ((!m_pRecordset->adoEOF) && m_pRecordset != NULL) { m_pRecordset->PutCollect("name", _variant_t("789")); m_pRecordset->MoveNext(); HRESULT m = m_pRecordset->Update(); } } catch (_com_error &e) { //此处屏蔽了一个错误,但是也阻止了其他错误的处理 /*cout << "处理失败2!错误信息:" << e.ErrorMessage(); system("pause"); return 0;*/ } m_pRecordset->Close(); m_pRecordset = NULL; m_pConnection->Close(); m_pConnection = NULL; } int funadd() { CoInitialize(NULL); _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try { m_pConnection.CreateInstance(__uuidof(Connection)); if (FAILED(m_pConnection.CreateInstance(__uuidof(Connection)))) { cout << "_ConnectionPtr对象指针实例化失败!!!" << endl; return 0; } m_pConnection->Open("Provider=SQLOLEDB;Server=127.0.0.1;Database=homework;uid=sa; pwd=**********;", "", "", adModeUnknown); } catch (_com_error e) { cout << "连接数据库失败!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } try { m_pRecordset.CreateInstance(__uuidof(Recordset)); if (FAILED(m_pRecordset.CreateInstance(_uuidof(Recordset)))) { cout << "记录集对象指针实例化失败!" << endl; return 0; } m_pRecordset->Open("select Id,name from T_Member", _variant_t((IDispatch*)m_pConnection), adOpenDynamic, adLockOptimistic, adCmdText); } catch (_com_error &e) { cout << "处理失败1!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } try { m_pRecordset->MoveLast(); m_pRecordset->AddNew(); m_pRecordset->PutCollect("name", _variant_t("dudeping")); m_pRecordset->Update(); } catch (_com_error &e) { cout << "处理失败2!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } m_pRecordset->Close(); m_pRecordset = NULL; m_pConnection->Close(); m_pConnection = NULL; } int fundelet() { CoInitialize(NULL); _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try { if (FAILED(m_pConnection.CreateInstance(__uuidof(Connection)))) { cout << "_ConnectionPtr对象指针实例化失败!!!" << endl; return 0; } m_pConnection->Open("Provider=SQLOLEDB;Server=127.0.0.1;Database=homework;uid=sa; pwd=**********;", "", "", adModeUnknown); } catch (_com_error e) { cout << "连接数据库失败!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } try { m_pRecordset.CreateInstance(__uuidof(Recordset)); if (FAILED(m_pRecordset.CreateInstance(_uuidof(Recordset)))) { cout << "记录集对象指针实例化失败!" << endl; return 0; } m_pRecordset->Open("select Id,name from T_Member where name=‘456‘", _variant_t((IDispatch*)m_pConnection), adOpenDynamic, adLockOptimistic, adCmdText); } catch (_com_error &e) { cout << "处理失败1!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } try { if (m_pRecordset != NULL && (!m_pRecordset->adoEOF)) { m_pRecordset->MoveFirst(); m_pRecordset->Delete(adAffectCurrent); //参数adAffectCurrent为删除当前记录 m_pRecordset->Update(); } } catch (_com_error &e) { cout << "处理失败2!错误信息:" << e.ErrorMessage(); system("pause"); return 0; } m_pRecordset->Close(); m_pRecordset = NULL; m_pConnection->Close(); m_pConnection = NULL; }
当然,这是开始的函数雏形,但是可以实现访问数据库并进行相应的操作,可以进一步封装成类,再对外开放接口,将数据库链接字符串和sql语句等传进去。
以上是关于C++对MS SQL Server的操作的主要内容,如果未能解决你的问题,请参考以下文章