设为首页 加入收藏

TOP

ado连接sqlserver
2015-11-21 01:57:30 来源: 作者: 【 】 浏览:0
Tags:ado 连接 sqlserver
//ado连接sql server


//头文件加上下面这句, 
#import "C:\Windows\system\msado15.dll" no_namespace rename("EOF","rsEOF")

//cpp里面
	CoInitialize(NULL);
	_ConnectionPtr pConn(__uuidof(Connection));
	_RecordsetPtr pRst(__uuidof(Recordset));
	_CommandPtr pCmd(__uuidof(Command));

	try{//以下是从windows身份验证进入的,sql server身份验证在我的机子上还为调试出来
	pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=WIN7-20120101NA\\SQLEXPRESS;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=WIN7-20120101NA;Use Encryption for Data=False;Tag with column collation when possible=False";
	//pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=test;Data Source=WIN7-20120101NA\\SQLEXPRESS;";
	//注释里的表示必须包含的字段

 pConn->Open("","","",adConnectUnspecified);           //打开连接

 pRst=pConn->Execute("select * from [dbo].[user]",NULL,adCmdText);    //读取user表
 _variant_t vUsername;
	while(!pRst->rsEOF)
	{
		vUsername=pRst->GetCollect("id");

		CString message;
		message.Format("%s",(LPCTSTR)(_bstr_t)vUsername);
		AfxMessageBox(message);///显示当前记录条数
		pRst->MoveNext();
	}
		}catch (_com_error e)///捕捉异常
		{
			CString errormessage;
			errormessage.Format("连接数据库失败!\r\n错误信息:%s", e.ErrorMessage());
			AfxMessageBox(errormessage);///显示错误信息
			return;
		}
	pRst->Close();
	pConn->Close();
	pCmd.Release();
	pRst.Release();
	pConn.Release();
	CoUninitialize();

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇sql存储过程 下一篇SQL查询初学者指南读书笔记(二)创..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: