首页 > 代码库 > ADO实例

ADO实例

  1 //rename作用:解决EOF重定义问题,dll中定义的EOF和程序定义的EOF冲突  2 /*no_namespace作用:  3 #import msado15.dll 生成文件 msado15.tlh,默认是有namespace ADODB {},  4 所有内容都包在这个名字空间中,访问时需要输入命名空间;  5 用了no_namespace 生成的头文件中没有namespace,所有内容是全局的,方便访问。   6 也可以使用 using namespace ADODB。  7 */  8 #import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" rename("EOF", "adoEOF") no_namespace  9 //using namespace ADODB; 10  11 /*CONNSTRING生成过程: 12 新建一个记事本文件,后缀改为udl,然后双击打开,配置好后用记事本打开,即可得到CONNSTRING 13 */ 14 #define CONNSTRING "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=." 15  16  17 void CMy2014_09_11Dlg::OnBtnSelect()  18 { 19     //清空listview 20     m_ctrlResult.DeleteAllItems(); 21  22     //COM初始化 23     CoInitialize(NULL); 24      25     //定义连接对象的智能指针 26     _ConnectionPtr conn; 27     //定义记录集对象的智能指针 28     _RecordsetPtr  rs; 29      30     try 31     { 32         HRESULT hr; 33          34         //创建连接对象(智能指针初始化,. 表示调用智能指针的成员函数) 35         //{00000514-0000-0010-8000-00AA006D2EA4} 36         hr = conn.CreateInstance("ADODB.Connection");  37          38         //连接数据库(-> 表示调用智能指针指向对象的成员函数,即调用连接对象的成员函数) 39         hr = conn->Open(CONNSTRING, "", "", adConnectUnspecified); 40          41         //构造查询语句 42         UpdateData(TRUE); 43         CString strSQL = "select * from t_student where 1 = 1"; 44         if (m_strSno.Compare("*") != 0) 45         { 46             CString strChild = " and sno = ‘"+ m_strSno +""; 47             strSQL += strChild; 48         } 49         if (m_strSname.Compare("*") != 0) 50         { 51             CString strChild = " and sname = ‘"+ m_strSname +""; 52             strSQL += strChild; 53         } 54         if (m_strSex.Compare("*") != 0) 55         { 56             CString strChild = " and sex = ‘"+ m_strSex +""; 57             strSQL += strChild; 58         } 59         if (m_strBirthday.Compare("*") != 0) 60         { 61             CString strChild = " and birthday = ‘"+ m_strBirthday +""; 62             strSQL += strChild; 63         } 64  65         //执行命令 66         rs = conn->Execute(strSQL.GetBuffer(0), NULL, adOptionUnspecified); 67          68         //获取字段数量 69         int nCount = rs->Fields->GetCount(); 70          71         //获取并输出数据 72         HWND hList = m_ctrlResult.GetSafeHwnd(); 73         LVITEM lvi = {0}; 74         lvi.mask = LVIF_TEXT; 75         while(!rs->adoEOF) 76         { 77             int nIndex = 0; 78             for (int i = 0; i < nCount; i++) 79             { 80                 if (i == 0) 81                 { 82                     lvi.iItem = nIndex; 83                     //发送LVM_INSERTITEM消息时,subitem必须为0 84                     lvi.iSubItem = i; 85                     CString strTmp = (char*)(_bstr_t)rs->Fields->Item[(long)i]->Value; 86                     lvi.pszText = strTmp.GetBuffer(0); 87                     //返回值为item的索引 88                     nIndex = ::SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&lvi); 89                 } 90                 else 91                 { 92                     lvi.iItem = nIndex; 93                     lvi.iSubItem = i; 94                     CString strTmp = (char*)(_bstr_t)rs->Fields->Item[(long)i]->Value; 95                     lvi.pszText = strTmp.GetBuffer(0); 96                     ::SendMessage(hList, LVM_SETITEM, 0, (LPARAM)&lvi); 97                 } 98             } 99 100             rs->MoveNext();101         }102         103     }104     catch (_com_error e)105     {106         MessageBox(e.Description());107     }108 109     //关闭连接110     conn->Close();111 112     //COM反初始化113     CoUninitialize();114 }

 

ADO实例