VC编程中利用ADO方式访问SQL Server 2012数据库(2)

来源:互联网 发布:网络用语bp是什么意思 编辑:程序博客网 时间:2024/06/12 17:58

数据库是存储数据的系统,对数据库的操作主要是对数据的操作,操作数据无非就是增、删、查、改,这里总结一下如何使用ADO对象对SQL Server数据库进行 增删查改 操作。在操作数据前必须要连接上数据库,关于SQL Server数据库的连接请参考前一篇文章VC编程中利用ADO方式访问SQL Server 2012数据库(1) - 连接数据库 。

其实,ADO操作数据库主要是通过 Connection 对象的 Excute() 方法调用 SQL 语句来完成对数据的操作。Excute() 方法的原型为:

Execute(_bstr_t CommandText, VARIANT * RecordsAffected, long Options)

返回值
返回 Recordset 对象引用。
参数
CommandText 字符串,包含要执行的 SQL 语句、表名、存储过程或特定提供者的文本。
RecordsAffected 可选,长整型变量,提供者向其返回操作所影响的记录数目。
Options 可选,长整型值,指示提供者应如何计算 CommandText 参数,可为下列值:
常量 说明
<1> adCmdText 指示提供者应按命令的文本定义计算 CommandText。
<2> adCmdTable  指示 ADO 应生成 SQL 查询以便从 CommandText 命名的表中返回所有行。
<3> adCmdTableDirect  指示提供者应从 CommandText 命名的表中返回所有行。
<4> adCmdTable  指示提供者应按表名计算 CommandText。
<5> adCmdStoredProc  指示提供者应按存储过程计算 CommandText。
<6> adCmdUnknown  指示 CommandText 参数中的命令类型未知。
<7> adAsyncExecute  指示命令应该异步执行。
<8> adAsyncFetch  指示对在 CacheSize 属性指定的初始数量之后的剩余行使用异步提取。

0、读写数据库 - 声明变量

_RecordsetPtr m_pRst(__uuidof(Recordset));_variant_t vAffected;_bstr_t strSqlCommand;

1、读写数据库 - 增加数据
插入数据的SQL语句格式: insert into <表名>(<列名1>, <列名2>, <列名3>, ...) value(<列值1>, <列值2>, <列值3>, ...)

strSqlCommand = "insert into TestTable(SerialNum, Weight, Height, Width) values('23', '23', '23', '23')";m_pConn->Execute(strSqlCommand, &vAffected, adCmdText);

2、读写数据库 - 删除数据
删除数据的SQL语句格式:delete from <表名> where <列名 >= <列值>

strSqlCommand = "delete from TestTable where SerialNum=23";m_pConn->Execute(strSqlCommand, &vAffected, adCmdText);

3、读写数据库 - 查询数据
查询数据的SQL语句格式:select top 10 * from <表名> where <列名 >= <列值>

strSqlCommand = "select top 10 * from TestTable where SerialNum=23";m_pRst = m_pConn->Execute(strSqlCommand, &vAffected, adCmdText);int nRowCount = 0;CString strSerialNum = _T("");CString strWeight = _T("");CString strHeight = _T("");CString strWidth = _T("");while (!m_pRst->rsEOF){nRowCount = m_MatchedDataList.GetItemCount();// 查询每一行各项数据的值strSerialNum = (CString)m_pRst->GetCollect("SerialNum");strWeight = (CString)m_pRst->GetCollect("Weight");strHeight = (CString)m_pRst->GetCollect("Height");strWidth = (CString)m_pRst->GetCollect("Width");// 查询结果显示在CListCtrl控件中m_MatchedDataList.InsertItem(nRowCount, strSerialNum);m_MatchedDataList.SetItemText(nRowCount, 1, Weight);m_MatchedDataList.SetItemText(nRowCount, 2, Height);m_MatchedDataList.SetItemText(nRowCount, 3, Width);// 移动到下一行m_pRst->MoveNext();}

4、读写数据库 - 修改数据
更新数据的SQL语句格式:update <表名> set <列名 >= <列值> where <列名 >= <列值>

strSqlCommand = "update TestTable set Weight='213' where SerialNum=23";m_pConn->Execute(strSqlCommand, &vAffected, adCmdText);
5、读写数据库 - 关闭ADO连接

(1)_ConnectionPtr

if (m_pConn->GetState() == adStateOpen){m_pConn->Close();}if (m_pConn)// 释放对象资源,一般在软件关闭时释放一次就可以{m_pConn.Release();m_pConn = NULL;}
(2)_RecordsetPtr
if (m_pRst->GetState() == adStateOpen){m_pRst->Close();}if (m_pRst)// 释放对象资源,一般在软件关闭时释放一次就可以{m_pRst.Release();m_pRst = NULL;}

参考资料:
[1] http://blog.csdn.net/johe2003/article/details/3903197
[2] http://blog.csdn.net/smstong/article/details/11829133

[3] http://blog.sina.com.cn/s/blog_8a7012cf0101484u.html

原创粉丝点击