请教更新记录集的方法

nevdongdong 2010-05-07 01:09:56
我想更新用户表users中Uname为edit5中输入的记录的密码。
系统报错 error C2664: 'Open' : cannot convert parameter 1 from 'class CString' to 'const class _variant_t &'
Reason: cannot convert from 'class CString' to 'const class _variant_t'

需要把传递进去的这个ch3如何处理?
还有,假设我直接传递个数值进去,下面更新记录集的循环好像还是不对,程序直接就卡住,哪位大哥来帮忙小弟我一下
CString ch3;
ch3=GetDlgItemInt(IDC_edit_5);
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");

m_pRecordset->Open("SELECT * FROM Users where Uname='3'",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error &e)
{
AfxMessageBox(e.Description());
}

try
{
while(!m_pRecordset->adoEOF)
{
m_pRecordset->PutCollect("password",_variant_t("m_zhun"));
m_pRecordset->MoveNext();
}
m_pRecordset->Update();
AfxMessageBox("修改密码成功");
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
}

m_pRecordset->Close();
m_pRecordset=NULL;
m_pConnection->Close();
m_pConnection=NULL;
}
...全文
109 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
康斯坦汀 2010-05-08
  • 打赏
  • 举报
回复
你的代码根本看不懂,界面的变量赋给了ch3,但下面的循环又没用到ch3.

用2楼提供的ADO类吧,封装好的,直接调用它提供的函数就可以了,

更新数据库中的值,可以采用调用SQL语句的方式,比如update语句,

用你的ch3的值构建update语句就行,或者直接调用update函数也可以,

用那个类中的SetFieldValue设置,然后update();

nevdongdong 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 r3000 的回复:]
你的代码根本看不懂,界面的变量赋给了ch3,但下面的循环又没用到ch3.

用2楼提供的ADO类吧,封装好的,直接调用它提供的函数就可以了,

更新数据库中的值,可以采用调用SQL语句的方式,比如update语句,

用你的ch3的值构建update语句就行,或者直接调用update函数也可以,

用那个类中的SetFieldValue设置,然后update();
[/Quote]

m_pRecordset->Open("SELECT * FROM Users where Uname='"+ch3+"'",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
上面的这句是改了的
m_pRecordset->Open(_bstr_t("SELECT * FROM Users where Uname='"+ch3+"'"),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

nevdongdong 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 r3000 的回复:]
你的代码根本看不懂,界面的变量赋给了ch3,但下面的循环又没用到ch3.

用2楼提供的ADO类吧,封装好的,直接调用它提供的函数就可以了,

更新数据库中的值,可以采用调用SQL语句的方式,比如update语句,

用你的ch3的值构建update语句就行,或者直接调用update函数也可以,

用那个类中的SetFieldValue设置,然后update();
[/Quote]

CString ch3;
ch3=GetDlgItemInt(IDC_edit_5);
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");

m_pRecordset->Open("SELECT * FROM Users where Uname='"+ch3+"'",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
try
{
while(!m_pRecordset->adoEOF)
{
m_pRecordset->PutCollect("password",_variant_t("m_zhun"));
m_pRecordset->MoveNext();
}
m_pRecordset->Update();
AfxMessageBox("修改密码成功");
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
}

m_pRecordset->Close();
m_pRecordset=NULL;
m_pConnection->Close();
m_pConnection=NULL;
}

m_zhun是关联了一个EDIT的变量。
再看看哪里有问题,谢谢
nevdongdong 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 nevdongdong 的回复:]
我想更新用户表users中Uname为edit5中输入的记录的密码。
系统报错 error C2664: 'Open' : cannot convert parameter 1 from 'class CString' to 'const class _variant_t &'
Reason: cannot convert from 'class CString' to 'cons……
[/Quote]
谢谢你的帮助。
这个问题是解决了
但是进入程序之后运行到这段代码程序还是直接就卡住,是下面的代码哪里什么循环出错了么。
zyq5945 2010-05-07
  • 打赏
  • 举报
回复
CString strSQL;
strSQL = "SELECT * FROM Users where Uname='3'";
_variant_t var;
var = (LPCTSTR)strSQL;
康斯坦汀 2010-05-07
  • 打赏
  • 举报
回复
ADO 建议用封装好的ADO类,省去很多麻烦:

用这个
康斯坦汀 2010-05-07
  • 打赏
  • 举报
回复
m_pRecordset->Open("SELECT * FROM Users where Uname='3'",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);


这句改成:


m_pRecordset->Open(_bstr_t("SELECT * FROM Users where Uname='3'"),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

4,012

社区成员

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

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