连接字符串:try
{
//hr = pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
pConnection->CursorLocation=adUseClient;
strsql="Provider=SQLOLEDB;Server=pc-201009181941;Database=ms;Uid=sa;Pwd=sa;";//sql sever 2000连接字符串
pConnection->Open(_bstr_t(strsql),"","",adModeUnknown);
}
}
catch(_com_error e)
{
CString err;
err.Format("连接数据库失败,\n\r错误:%s",e.Description());
//MessageBox(err);
AfxMessageBox(err);
return false;
}
执行成功没有catch
接下来执行sql语句不行:
UpdateData(true); if(m_user!=""&&m_pass!="")
{
CString str;
str="select * from Mg where 用户名='"+m_user+"' and ";
str=str+"密码='"+m_pass+"'";
_RecordsetPtr m_Recordset;
m_Recordset.CreateInstance(__uuidof(Recordset)); CPJApp *theApp=(CPJApp *)AfxGetApp();
try
{
m_Recordset=theApp->pConnection->Execute((_bstr_t)str,NULL,adCmdText);
if(m_Recordset->adoEOF)
{
theApp->usr_name=m_user;
m_Recordset->Close();
m_Recordset.Release();
// CDialog::OnOK();
}
else
{
MessageBox("用户名或密码错误!","提示",MB_ICONWARNING);
}
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
}
else
{
MessageBox("用户名或密码不能为空!","提示",MB_ICONWARNING);
}
总是提示用户名或密码错误 很显然是执行sql语句错误 各位请告知 谢谢啦
{
//hr = pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
pConnection->CursorLocation=adUseClient;
strsql="Provider=SQLOLEDB;Server=pc-201009181941;Database=ms;Uid=sa;Pwd=sa;";//sql sever 2000连接字符串
pConnection->Open(_bstr_t(strsql),"","",adModeUnknown);
}
}
catch(_com_error e)
{
CString err;
err.Format("连接数据库失败,\n\r错误:%s",e.Description());
//MessageBox(err);
AfxMessageBox(err);
return false;
}
执行成功没有catch
接下来执行sql语句不行:
UpdateData(true); if(m_user!=""&&m_pass!="")
{
CString str;
str="select * from Mg where 用户名='"+m_user+"' and ";
str=str+"密码='"+m_pass+"'";
_RecordsetPtr m_Recordset;
m_Recordset.CreateInstance(__uuidof(Recordset)); CPJApp *theApp=(CPJApp *)AfxGetApp();
try
{
m_Recordset=theApp->pConnection->Execute((_bstr_t)str,NULL,adCmdText);
if(m_Recordset->adoEOF)
{
theApp->usr_name=m_user;
m_Recordset->Close();
m_Recordset.Release();
// CDialog::OnOK();
}
else
{
MessageBox("用户名或密码错误!","提示",MB_ICONWARNING);
}
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
}
else
{
MessageBox("用户名或密码不能为空!","提示",MB_ICONWARNING);
}
总是提示用户名或密码错误 很显然是执行sql语句错误 各位请告知 谢谢啦
或者用theApp->m_Recordset->Open((_bstr_t)str, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)试试。
str=str+"密码='"+m_pass+"'";
这个字符串好像写得不对吧?
在VC中的add watch 中当我写一个变量看值是总是报错:ERROR:symbol "str" not found 是怎么回事
str也确实定义了 用MessageBox(str)也可以看见值
未载入调试信息。rebuild all一下再调试看。
那为什么当我第二次调用m_Recordset=theApp->pConnection->Execute((_bstr_t)str,NULL,adCmdText);时执行完后 到了catch 了
能加你的QQ或MSN吗
貌似大家的技术问题都不私聊
你用theApp->m_Recordset->Open((_bstr_t)str, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)试试看先
改成
pConnection.CreateInstance(__uuidof(Connection));
pConnection->Open(_bstr_t(strsql),"","",NULL);
if(!m_Recordset->adoEOF)
pConnection->Open(_bstr_t(strsql),"","",NULL);
if(!m_Recordset->adoEOF)
pConnection是全局变量 我在APP中已经初始化了
就是把pConnection的初始化代码改一下啊
我在登陆框中用完m_Recordset后就用close 和release了
在图书添加对话框中重新初始化RecordsetPtr对象
m_Recordset->Open((_bstr_t)str, theApp->pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if(!m_Recordset->adoEOF)
//检查以下代码和你的有什么不同
#import "c:\program files\common files\system\ado\msado15.dll" named_guids rename_namespace("ADOCG") rename("EOF", "EndOfFile") rename("BOF","FirstOfFile")
using namespace ADOCG;
#include "icrsint.h"//全局
_ConnectionPtr m_pCon;
_RecordsetPtr m_pRs;try
{
m_pCon.CreateInstance(__uuidof(Connection));
m_pCon->ConnectionTimeout = 3;
if(SUCCEEDED(m_pCon->Open(strConnect/*换成你的连接字串*/, _T(""), _T(""), NULL)))
{
m_pRs.CreateInstance(__uuidof(Recordset));
if(SUCCEEDED(m_pRs->Open(strSQL/*换成你的SQL字串*/, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)))
{
//成功处理
}
}
}
catch(_com_error &e)
{
}
using namespace ADOCG;
m_pCon->ConnectionTimeout = 3;
if(SUCCEEDED(m_pRs->Open(strSQL/*换成你的SQL字串*/, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)))
{
//成功处理
}
hr=m_Recordset->Open((_bstr_t)str, theApp->pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(hr))
{
if(!m_Recordset->adoEOF)
还是执行到hr=m_Recordset->Open((_bstr_t)str, )就跳到catch了 和以往的不同 以前还要执行if(SUCCEEDED(hr))的 先在都不执行了
仔细检查一下你的SQL语句有没有拼对(最好在调试的时候拼完了检查),在数据库中的表名列名是否完全匹配。