什么异常错误,前面有没加CoInitialize(NULL)初始化COM环境.
解决方案 »
- [DirectShow问题]Windows Mobile平台,CameraCapture的Preview的问题
- VC6.0中unsigned不起作用?
- CList::InsertItem调用的哪个dll哦,我想下钩子
- 菜鸟请教:VC/MFC中能不能调用C#写的DLL,请指点……
- 好心人,请教ADO问题
- dulon cpu 到底怎么样啊?
- CString 中英文字符串的操作问题~!
- 请问怎么编写一个关闭所有端口的程序(除指定的几个端口外)??
- 关于Gatagride的用法
- 急!!!如何查找CRecordSet中的记录???
- 使用资源脚本定义的资源和直接用函数建立的 有和区别?
- 现在的点歌系统用的什么格式文件啊 (影音文件)是mv吗
在stdafx.h中加入:
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")
在void CAdoDlg::OnBtnQuery()添加下面代码:(三种方式)
第一种:
CoInitialize(NULL);//初始化COM库
_ConnectionPtr pConn(__uuidof(Connection));//实例化一个connection对象pConn
_RecordsetPtr pRst(__uuidof(Recordset)); //实例化一个Recordset对象pRst
//连接字符串说明:Provider表示驱动引擎;User ID表示用户名;Password表示口令;Initial Catalog表示数据库名
pConn->ConnectionString=
"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=xx;Password=;Data Source=(local)";
try
{
pConn->Open("","","",adConnectUnspecified);
pRst=pConn->Execute("(_bstr_t)sql语句",NULL,adCmdText);
while(!pRst->rsEOF)
{
..(_bstr_t)pRst->GetCollect("列名");
pRst->MoveNext();
}
}
catch(_com_error e)
{
MessageBox(e.Description());//输出异常原因。
return;
}
pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();//释放COM库
第二种:
将上面红色语句删除,添加:
pRst->Open("(_variant_t)sql语句",_variant_t((IDispatch*)pConn),
adOpenDynamic,adLockOptimistic,adCmdText);
第三种:
在上面蓝色语句后添加:
_CommandPtr pCmd(__uuidof(Command));//实例化一个Command对象pCmd
在将第二种添加的语句删除,添加:
pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
pCmd->CommandText="sql语句";
pRst=pCmd->Execute(NULL,NULL,adCmdText);
在上面绿色语句后添加:
pCmd.Release();
数据库确定存在,而且没有拼错,,编译通过,运行出异常
参考这
http://www.connectionstrings.com/sql-server-2005