用VC和ADO连接如何用sql语句的问题.

pingshan 2003-10-15 05:08:44
VC++ 6.0; SQL 2000; 程序用ADO方式连接;
连接代码如下:
if(FAILED(CoInitialize(NULL))) return FALSE;
_bstr_t strCnn("Provider=sqloledb;Data Source=;Integrated
Security='SSPI';Initial Catalog=psgs;User Id=;Password=;");

// Open recordset from Authors table.
g_pRS.CreateInstance(__uuidof(Recordset));
g_pRS->CursorType = adOpenStatic;

// Use client cursor to enable AbsolutePosition property.
g_pRS->CursorLocation = adUseClient;
g_pRS->Open("usertable", strCnn, adOpenKeyset, adLockBatchOptimistic, adCmdTable);
//usertable
return TRUE;

所有连接都是成功的.现在假设usertable有一条我要找的记录;其中有个字段(名字)的值是"pingshan"; 那么在SQL中的语句为:
"select * from usertable where by 名字='pingshan'" ;

可我弄不懂;"select * from usertable where by 名字='pingshan'" 该怎么发送到SQL数据库让它运行或者说该怎么调用?哎,这问题问得不清楚,请谅解..你们知道我想问什么了吗?就是如何在VC中实现查找数据库中的特定记录? 高手们多多支持解答啊.分数50分,不算高,如果不够请说明,解决问题后我另开帖给分!
...全文
23 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
arvid_gs 2003-10-16
  • 打赏
  • 举报
回复
_bstr_t strSQL = "select * from usertable where 名字='pingshan'";
直接带入就可以了
曾经的猎狐 2003-10-16
  • 打赏
  • 举报
回复
select * from usertable where 名字='pingshan'

SQL语句有错误
iamknight 2003-10-16
  • 打赏
  • 举报
回复
_bstr_t是C++对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。
char *转换到BSTR可以这样:
BSTR b=_com_util::ConvertStringToBSTR("数据");///使用前需要加上comutil.h和comsupp.lib
SysFreeString(bstrValue);
反之可以使用
char *p=_com_util::ConvertBSTRToString(b);
delete p;
pingshan 2003-10-16
  • 打赏
  • 举报
回复
CString strSQL = "Select count(*) as lcount from TT";
BSTR bstrSQL = strSQL.AllocSysString();

是MFC的,我用的是SDK,不知道该怎么办..怎么改成SDK可用?SDK有CString吗?
菜鸟来的啊,能说得详细一点不?
wuxfBrave 2003-10-16
  • 打赏
  • 举报
回复
select * from usertable where 名字='pingshan'
spwnihao 2003-10-15
  • 打赏
  • 举报
回复
给你个小例子,参考一下吧,不过你的SQL语句写错了
"select * from usertable where 名字='pingshan'"
才对呀!!!
_variant_t TheValue;
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\vctest\\AdoTest\\Debug\\Mydb.mdb;Persist Security Info=False","","",0);
CString strSQL = "Select count(*) as lcount from TT";
BSTR bstrSQL = strSQL.AllocSysString();
m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
TheValue = m_pRecordset->GetCollect("lcount");
if(TheValue.vt!=VT_NULL)
{
int AllCount = TheValue.lVal;

}

}
pingshan 2003-10-15
  • 打赏
  • 举报
回复
"select * from usertable where by 名字='pingshan'" 应为
"select * from usertable where 名字='pingshan'"

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧