VC6.0利用ADO技术连接sql2000的几种方法说到用ADO来连接数据库的问题,那和想还是有必要向大家来说明一下什么是ADO技术.。 ADO(Active Data Object)是微软公司给予OLEDB的数据库模型。它实现了一系列COM接口,通过数据提供者(Data Provider)和数据使用者(Data Consumer)来实现了广义的数据存取.ADO模型一方面简化了数据存取,例如使用Jet OLE DB Provider 可以实现Access数据库的无DSN连接;另一方面在ASP等Internet应用中也得到了广泛的使用. 闲话少说来段代码来说明问题吧: 1> 利用VC++来连接本地的sql2000 _ConnectionPtr pConnection;// pConnection.CreateInstance("ADODB.Connection"); CString strsql="Provider=SQLOLEDB;data souce=338_BAOLIANG;initial catalog=MSDN;user id=sa;password=admin"; /*data souce=yoursqlserver(127.0.0.1 OR your IP);initial catalog=yourDatabase;user id=yourusername;passeord=yourPassWord*/ BSTR bstrsql; bstrsql=strsql.AllocSysString(); try { pConnection->Open(bstrsql,"","",adModeUnknown); } catch(_com_error e) { MessageBox("Connected is falied!"); return; } MessageBox("Connection is ok!"); 2〉利用VC远程连接sql2000 大体上还是没有区别的,只是在语句上有一些细微的差别而已: Code: CString strsql; strsql="driver={SQL Server};Server=192.168.1.105;DATABASE=MSDN;UID=sa;PWD=admin"; BSTR bstrsql; bstrsql=strsql.AllocSysString(); try { pConnection->Open(bstrsql,"","",adModeUnknown); } catch(_com_error e) { MessageBox("Connected is falied!"); return; } MessageBox("Connection is ok!"); strsql="select * from msdntable"; if(ReadData(strsql))//读取远程SQL数据库中的数据 { MessageBox("Data is Read!"); }
VC6.0利用ADO技术连接sql2000的几种方法
最新推荐文章于 2018-11-21 20:44:27 发布
CString strAdoConstr; CString strAdoCPU; strSecondUserName= "sa "; strAdoConstr.Format( "Provider=SQLOLEDB.1;Persist Security Info=True;Data Source=%s;Initial Catalog=%s;User ID=%s;Password=%s ",strSecondDatabaseIP,strSecondDatabaseName,strSecondUserName,DecodeDBPassword); m_pConnection.CreateInstance(_uuidof(Connection)); strAdoCPU.Format( "Driver={SQL Server};Server=%s;Uid=%s;Pwd=%s;Database=%s ",strFirstDatabaseIP, strFirstUserName, strFirstPassword,strFirstDatabaseName); m_pConnectionCenterCPU.CreateInstance(_uuidof(Connection)); try { m_pConnection-> ConnectionTimeout=8; m_pConnection-> Open( (_bstr_t)strAdoConstr, " ", " ",adModeUnknown ) ; m_pConnectionCenterCPU-> ConnectionTimeout=16; // m_pConnectionCenterCPU-> Open( (_bstr_t)strAdoCPU, " ", " ",adModeUnknown); Li // AfxMessageBox( "database is ok "); } catch(_com_error e)///捕捉异常 { // e.Description() AfxMessageBox(e.Description());// "数据库连接失败! "); return FALSE; } 这是链接部分,要想可用 ,你得先连上才可以知道,其实 用起来还是比较简单的 一、初始化 首先,在stdafx.h文件中加入: #import "C:/Program files/common files/System/ado/msado15.dll " no_namespace rename( "EOF ", "EndOfFile ") 定义变量 _ConnectionPtr ADOConn; _RecordsetPtr m_pADOSet; 二、连接 //SQL SERVER数据库连接 ::CoInitialize(NULL); CString strConnect; strConnect= "Provider=sqloledb.1;Data Source=192.168.0.131;Initial Catalog=mynet;UID=sa;Integrated Security=SSPI "; //mynet数据库名字,我使用的是非数据源方式 try { ADOConn.CreateInstance(__uuidof(Connection)); ADOConn-> Open((const char *)strConnect, " ", " ",adModeUnknown);//adConnectUnspecified } catch(...) { AfxMessageBox( "数据库初始化错误,程序将关闭! "); return FALSE; } 三、初始化ADO记录集 m_pADOSet.CreateInstance(__uuidof(Recordset)); _variant_t strSQL; strSQL= "select * from kk "; if ( m_pADOSet-> State == adStateOpen) m_pADOSet-> Close(); try { m_pADOSet-> CursorLocation=adUseClient; m_pADOSet-> Open( "select * from kk ", ADOConn.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdUnknown); } catch(_com_error &e) { CString err; err.Format( "ADO Error: %s ",(char*)e.Description()); } int iCount; iCount = m_pADOSet-> GetRecordCount(); 四、查询数据 _variant_t Holder; int T0; while(!m_pADOSet-> EndOfFile) { Holder = m_pADOSet-> GetCollect( "id "); //if(Holder.intVal!=VT_NULL) T0=Holder.intVal; m_pADOSet-> MoveNext(); } 五、添加数据 m_pADOSet-> AddNew(); m_pADOSet-> PutCollect( "id ",_variant_t( "23 ")); m_pADOSet-> PutCollect( "username ",_variant_t( "salkfj ")); m_pADOSet-> Update(); 六、修改数据 m_pADOSet-> EditMode; m_pADOSet-> PutCollect( "id ",_variant_t( "3 ")); m_pADOSet-> PutCollect( "username ",_variant_t( "saj123456789 ")); m_pADOSet-> Update(); 七、删除数据 m_pADOSet-> Delete(adAffectCurrent);//删除当前记录 八、断开数据库 //释放ADO连接对象 if(adStateOpen == ADOConn-> State) ADOConn-> Close(); ADOConn.Release(); // 释放ADO记录集 if(adStateOpen==m_pADOSet-> State) m_pADOSet-> Close(); m_pADOSet.Release();