关于ADO,问了好多地方了,都没人能给我个正确的答复!!!

whq1982 2004-06-27 03:00:55
_variant_t vUsername,vUserpsw;
_RecordsetPtr m_pRecordset;
try
{ m_pRecordset.CreateInstance("ADODB.Recordset");
CString strSql;
strSql.Format("SELECT * FROM loginuser WHERE username='%s'",strName);
////////以下2句应该为错误的地方,因为注释掉以后就没有错了
m_pRecordset->Open(_variant_t(strSql),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
vUserpsw = m_pRecordset->GetCollect("userpsw");
//////////////////////////////////////////////////////
if (strcmp(strPsw,(LPCTSTR)(_bstr_t)vUserpsw)==0)
{CLogDlg::CreateFile();MessageBox("正在登陆");
AfxMessageBox("用户名或密码错误,请重新输入!");
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
请各位帮忙看看,编译没有错,运行时出错,应该怎样改呢?多谢


...全文
202 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyfdanny 2004-07-06
  • 打赏
  • 举报
回复
runtime error! 这种事情经常有的。是你字段或者你定义的记录集写错了。请仔细检查方面。
悲酥清风 2004-06-30
  • 打赏
  • 举报
回复
try{
m_pRecordset->Open(...)
}
//表打开之后先判断是否有符合条件的记录,再GetCollct()
if (m_pRecordset->adoEof && m_pRecordset->Bof)
{ m_pRecordset->Close();return;}
m_pRecordset->GetCollct(...)
chinadrgon 2004-06-29
  • 打赏
  • 举报
回复
粗粗的看了你的代码,我觉得是你动态构造出来的SQL语句有问题。例如你的 sql1.Format("select * from music where id >= %ld and id <= %ld order by id",m_id,m_id1);中m_id,m_id1可能包含特殊字符如双引号,单引号等,由于这些特殊字符导致你程序在运行时失败,因为m_id,m_id1是用户输入的数据,很有可能包含了这些特殊字符从而导致SQL查询错误!你可以试这把你动态构造的SQL查询字符串输出,然后拷贝下来到脱离程序,数据库上直接运行SQL语句,看看是不是这个原因。如果是的话,你就需要检测一下用户的输入数据,屏蔽掉一些特殊字符!
yang4410 2004-06-29
  • 打赏
  • 举报
回复
_variant_t vUsername,vUserpsw;
_RecordsetPtr m_pRecordset;
try
{ m_pRecordset.CreateInstance("ADODB.Recordset");
CString strSql;
strSql.Format("SELECT * FROM loginuser WHERE username='%s'",strName);
////////以下2句应该为错误的地方,因为注释掉以后就没有错了
m_pRecordset->Open(_variant_t(strSql),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
//修改20040629,判断记录集是否为空,EOF视自定义名称改变
if(!pRecordset->EOF)
{
vUserpsw = m_pRecordset->GetCollect("userpsw");
//////////////////////////////////////////////////////
if (strcmp(strPsw,(LPCTSTR)(_bstr_t)vUserpsw)==0)
{CLogDlg::CreateFile();MessageBox("正在登陆");
AfxMessageBox("用户名或密码错误,请重新输入!");
}
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
icetears 2004-06-29
  • 打赏
  • 举报
回复
楼主,我今天和你碰到一个同样的错误,原因就在
vUserpsw = m_pRecordset->GetCollect("userpsw");
引号里面的字段名我写错了,所以出错,你看看是不是这个愿意哪。

iuhxq 2004-06-28
  • 打赏
  • 举报
回复
给你个我写的:
关键代码如下:
void CMusicDlg::OnButton1()
{
// TOD Add your control notification handler code here
//GetDlgItem(IDC_BUTTON1)->Enable(true);
UpdateData(true);
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
CString conn;
if ( m_sql )
{
conn.Format("driver={SQL Server};Server=%s;UID=%s;PWD=%s;DATABASE=music",m_ip,m_username,m_password);
}
else
{
conn.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=asp2004.mdb");
}
CString sql1;
if ( m_id > 0 && m_id1 > m_id)
{
sql1.Format("select * from music where id >= %ld and id <= %ld order by id",m_id,m_id1);
}
else if ( m_id > 0 )
{
sql1.Format("select * from music where id >= %ld order by id",m_id);
}
else
{
sql1.Format("select * from music where url1 is null order by id");
}
m_pConnection->Open(_bstr_t(conn),"","",adModeUnknown);
m_pRecordset->Open(_variant_t(sql1),(IDispatch *)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
m_pRecordset->MoveFirst();
flag=false;
while(!m_pRecordset->adoEOF)
{
MSG msg;
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if( flag ) return;
}
_variant_t mp3name = m_pRecordset->GetCollect("mp3name");
_variant_t id = m_pRecordset->GetCollect("id");
m_id=long(id.iVal);
m_name.Format("%s",(char *)_bstr_t(mp3name));
m_cprogressctrl.SetPos( m_id % 100 );
UpdateData(false);

CInternetSession session;
CHttpFile *myHttpFile = NULL;
CString m_SiteInfo = "";
CString myData;
CString m_SiteName = "http://mp3.baidu.com/m?tn=baidump3&ct=134217728&rn=&word=";
m_SiteName += m_name + "&lm=0";

//session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000*30);
//session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
//session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
//session.EnableStatusCallback(false);

myHttpFile = (CHttpFile*)session.OpenURL(m_SiteName);
while( myHttpFile->ReadString( myData ) )
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if( flag ) return;
}
m_SiteInfo+=myData;
}

myHttpFile->Close();

session.Close();
CString str1;
int find = 1, find1;
CString url1 = "";
while( find > 0 )
{
find = m_SiteInfo.Find( ".mp3" );
str1 = m_SiteInfo.Left( find );
m_SiteInfo = m_SiteInfo.Right( m_SiteInfo.GetLength() - find - 1 );
str1.MakeReverse();
find1 = str1.Find( ":" );
str1 = str1.Left( find1 );
str1.MakeReverse();
str1 = "http:" + str1 + ".mp3";
if ( str1.GetLength() > 20 && netfile( str1 ) && str1.Find( "'" ) < 0 )
{
url1 = url1 + str1 + "|";
}
}
if (url1.GetLength()>0)
{
CString sql1;
sql1.Format("update music set url1='%s' where id=%ld",url1,m_id);
_variant_t RecordsAffected;
m_pConnection->Execute( _bstr_t(sql1), &RecordsAffected, adCmdText );
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pConnection->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
long errorCode=e.WCode();
if(3127==errorCode) AfxMessageBox("表不存在");
if(3092==errorCode) AfxMessageBox("表已经存在");
}
MessageBox( "成功完成" );
}

void CMusicDlg::OnButton2()
{
// TOD Add your control notification handler code here
flag=true;
MessageBox("停止");
}

bool CMusicDlg::netfile(CString str)
{
CInternetSession session;
long cb=0;
bool flag=false;
CInternetFile *file=NULL;
try
{
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000*10);
//session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
//session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
session.EnableStatusCallback(false);
file=(CInternetFile*)session.OpenURL(str,1,INTERNET_FLAG_TRANSFER_BINARY);
MSG msg;
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if ( flag ) return false;
}

if(file)
{
cb=file->SeekToEnd();//cb是文件的正确长度
if(cb>100)
{
return true;
}
else
{
return false;
}

}
else
{
return false;
}
}
catch(CInternetException* m_pException)
{
file=NULL;
if ( m_pException->m_dwError==ERROR_INTERNET_EXTENDED_ERROR )
{
return false;
}
//AfxMessageBox(m_pException->m_dwError);
//m_pException->ReportError();
m_pException->Delete();
return false;
}
file->Close();
session.Close();
return false;
}

void CMusicDlg::OnOK()
{
// TOD Add extra validation here

CDialog::OnOK();
}

void CMusicDlg::OnCheck1()
{
// TOD Add your control notification handler code here
m_sql=!m_sql;
if (m_sql)
{
GetDlgItem(IDC_EDIT4)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_EDIT5)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_EDIT6)->ShowWindow(SW_SHOW);
}
else
{
GetDlgItem(IDC_EDIT4)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_EDIT5)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_EDIT6)->ShowWindow(SW_HIDE);
}
}

nineclock 2004-06-27
  • 打赏
  • 举报
回复
CoInitialize(NULL)用了吗
Kudeet 2004-06-27
  • 打赏
  • 举报
回复
strSql.Format("SELECT * FROM [loginuser] WHERE [username]='%s'",strName);
needways 2004-06-27
  • 打赏
  • 举报
回复
BOOL OpenRecordset(LPCTSTR strSQL, LockTypeEnum LockType /* = adLockOptimistic */,
CursorTypeEnum CursorType /* = adOpenStatic */, long lOption /* = adCmdText */)
{
if (m_pConn == NULL || m_pRset == NULL) {
ASSERT(FALSE);
return FALSE;
}

if (strSQL == NULL || *strSQL == '\0') {
ASSERT(FALSE);
return FALSE;
}

try {
if (IsOpen()) { Close(); }
return SUCCEEDED(m_pRset->Open(_bstr_t(strSQL),
_variant_t((IDispatch*)m_pConn->GetConnection(), true),
CursorType, LockType, lOption));
} catch (_com_error e) {
TraceError("打开记录集发生异常");
return FALSE;
}
}
whq1982 2004-06-27
  • 打赏
  • 举报
回复
调用了您所说的函数,只要执行SQL语句的时候就会有错误。runtime error!
needways 2004-06-27
  • 打赏
  • 举报
回复
是什么错误你还没说呢。有没有调用 CoInitialize(NULL)?

4,012

社区成员

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

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