实现VS2010连接SQL Server 2008操作与编程,主要记录的是一个学习的过程。
实现VS2010连接SQL Server 2008:逗逗飞的专栏
实现VS2010连接SQL Server 2008代码:点击打开链接
ADO的三个指针:
_ConnectionPtr:用于建立数据库的连接。 _RecordsetPtr:它是专门为通过记录集操作数据库而设立的指针,通过该接口可以对数据库的表内的记录、字段等进行各种操作。 _CommandPtr:提交的sql查询字符串指针
目的:连接数据库,实现对数据库的连接,读取操作。结果如下所示:
图:sql数据库
图:VS2010运行结果
需要的头文件 #import "C://Program Files//Common Files//System//ado//msado15.dll" no_namespace <span style="font-family: Arial, Helvetica, sans-serif;">rename("EOF","adoEOF")rename("BOF","adoBOF")</span>
用到的三个变量 _ConnectionPtr m_pConnection; // 数据库 _RecordsetPtr m_pRecordset; // 命令 _CommandPtr m_pCommand; // 记录
连接数据库 void CtestDlg::OnBnClickedConnect() { // TODO: 在此添加控件通知处理程序代码 ::CoInitialize(NULL); //初始化OLE/COM库环境 HRESULT hr = NULL; try { hr = m_pConnection.CreateInstance(_uuidof(Connection));//创建连接对象实例 if (SUCCEEDED(hr)) { m_pConnection->ConnectionString = ("Provider = SQLOLEDB.1; Persist Security Info = False; User ID = ARP; Password=123456; Initial Catalog = school; Data Source = (local)"); hr = m_pConnection->Open("", "", "", adConnectUnspecified);//打开数据库 if (FAILED(hr)) { AfxMessageBox(_T("Open Failed!")); } } else { AfxMessageBox(_T("Create instance of connection failed!")); } } catch (_com_error e) { CString temp; temp.Format(_T("数据库连接错误\r\n错误信息为:%s"), e.ErrorMessage()); AfxMessageBox(temp); } }
查询数据库 void CtestDlg::OnBnClickedAdd() { // TODO: 在此添加控件通知处理程序代码 try { m_pRecordset.CreateInstance("ADODB.Recordset"); m_pRecordset->Open("select sno, cno, grade from sc", _variant_t((IDispatch*)m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); } catch (_com_error &e) { AfxMessageBox(e.Description()); } _variant_t sno, cno, grade; try { int num = m_list.GetItemCount(); while (!m_pRecordset->adoEOF) { CString sno= (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("sno"))->Value); CString cno = (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("cno"))->Value); CString grade = (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("grade"))->Value); m_list.InsertItem(num, sno); m_list.SetItemText(num, 1, cno); m_list.SetItemText(num , 2, grade); num++; m_pRecordset->MoveNext(); } } catch (_com_error &e) { AfxMessageBox(e.Description()); } }
插入数据(不带参数) try { if(!m_pRecordset->Supports(adAddNew)) return; m_pRecordset->AddNew(); m_pRecordset->Fields->GetItem (_variant_t("姓名"))->Value=_bstr_t("赵薇"); m_pRecordset->Fields->GetItem (_variant_t("性别"))->Value=_bstr_t("女"); m_pRecordset->Fields->GetItem (_variant_t("age"))->Value=_variant_t((short)20); m_pRecordset->Fields->GetItem (_variant_t("marry"))->Value=_bstr_t("未婚"); m_pRecordset->Update(); }//try catch (_com_error &e) { ::MessageBox(NULL, "又出毛病了。","提示",MB_OK │ MB_ICONWARNING); }
删除数据 try { m_pRecordset->MoveFirst(); while(m_pRecordset->adoEOF==VARIANT_FALSE) { CString sName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem (_variant_t("姓名"))->Value); if(::MessageBox(NULL,"姓名="+sName+"\n删除她吗?", "提示",MB_YESNO │ MB_ICONWARNING)==IDYES) { m_pRecordset->Delete(adAffectCurrent); m_pRecordset->Update(); } m_pRecordset->MoveNext(); } }//try catch (_com_error &e) { ::MessageBox(NULL,"又出毛病了。","提示",MB_OK │ MB_ICONWARNING); }
时间: 2024-10-20 10:34:38