MFC ado远程连接MSSQL
2013-10-14 13:18
459 查看
在stdafx.h中引入:
#import"C:\ProgramFiles\CommonFiles\System\ado\msado15.dll"no_namespacerename("EOF","adoEOF")添加变量:
_ConnectionPtrm_pConnection; _RecordsetPtrm_pRecordset;
连接数据库:
CoInitialize(NULL);m_pConnection.CreateInstance(_T("ADODB.Connection"));///创建Connection对象m_pConnection->ConnectionTimeout=8;try{HRESULThr;hr=m_pConnection->Open(_T("Provider=SQLOLEDB.1;DataSource=127.0.0.1,1433;InitialCatalog=databasename;"),_T("sa"),_T("sapwd"),adModeUnknown);if(FAILED(hr)){AfxMessageBox(_T("不能连接数据库!"));returnFALSE;}else{AfxMessageBox(_T("连接数据库成功!"));}}catch(_com_errore){AfxMessageBox(_T("不能打开数据库!"));returnfalse;}读取数据表:
CStringstrTableName=_T("");m_pRecordset=m_pConnection->OpenSchema(adSchemaTables);if(!m_pRecordset->BOF){m_pRecordset->MoveFirst();}while(!m_pRecordset->adoEOF){strTableName=(LPCSTR)_bstr_t(m_pRecordset->GetFields()->GetItem(_T("TABLE_NAME"))->Value);//MessageBox(strTableName);m_pRecordset->MoveNext();m_ctrlListTables.InsertString(m_ctrlListTables.GetCount(),strTableName);}读取数据:
CStringSQLStr;SQLStr.Format(_T("selecttop1*fromtest"));m_pRecordset.CreateInstance(__uuidof(Recordset));m_pRecordset->Open(_bstr_t(SQLStr),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);while(!m_pRecordset->adoEOF){CStringstr;str=(LPCSTR)_bstr_t(m_pRecordset->GetFields()->GetItem(_T("id"))->Value);//MessageBox(strTableName);m_pRecordset->MoveNext();m_ctrlListTables.InsertString(m_ctrlListTables.GetCount(),str);}插入数据:
CStringstrCommand=_T("");strCommand.Format(_T("INSERTINTOA_cpavalues('%s','%d','%s','%s','%s','%d','%s','%d','%s','%s','%s','%s','%s')"),LMuname,LMID,lailuURL,addtime,cpaUser,ok,regip,sex8,browser,mima,os,fbl,wwwurl);try{m_pConnection->Execute((_bstr_t)strCommand,NULL,adCmdText);}catch(_com_error&e){//MessageBox(e.ErrorMessage());returnfalse;}
相关文章推荐
- oracle 标识符无效 字段名和表中一致
- backup oracle
- ADO连接数据库的那些常用数据类型
- plsql developer (在8.04版本中试过可用)
- java.lang.ClassCastException: oracle.sql.BLOB cannot be cast to oracle.sql.BLOB
- 数据访问层DAL(数据库访问抽象类DataProvider)
- 【SQL Server学习笔记】Delete 语句、Output 子句、Merge语句
- 【SQL Server学习笔记】Delete 语句、Output 子句、Merge语句
- MongoDB经典入门(2)--shell命令
- 【SQL Server学习笔记】数据库审核
- 【SQL Server学习笔记】数据库审核
- 解决window8 下连接PLSQL 报ora-12154错误
- sql一般教程2
- SQLite 3 一些基本的使用
- Linux下安装MongoDB
- 使用sqlserver日期函数获取当前日期
- mysql常用命令
- 101 个 MySQL 的调节和优化的提示
- oracle 树状查讯问题
- mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1'