我的代码:
         GetDlgItem(IDC_EDIT_ID)->GetWindowTextW(ID);
GetDlgItem(IDC_EDIT_PSW)->GetWindowTextW(PSW);
sql.pCommand.CreateInstance(__uuidof(Command));
sql.pConnetion.CreateInstance(__uuidof(Connection));
sql.pRecordset.CreateInstance(__uuidof(Recordset));
if(ID == "")//ID是CString
{
AfxMessageBox(L"账号不能为空",MB_OK,0);
return;
} HRESULT hr;
CString open = L"driver={SQL Server};Server=.\\sqlexpress;DATABASE=企业员工管理系统;UID="+ID+L";PWD="+PSW;
//try
AfxMessageBox(open,MB_OK,0);

hr = sql.pConnetion->Open((_bstr_t)open,"","",adModeUnknown);
if(SUCCEEDED(hr))
AfxMessageBox(L"连接成功",MB_OK,0);
else
{
AfxMessageBox(L"连接失败",MB_OK,0);
//exit(0);
}两个问题,如果UID = sa PSW = 123,就会连接成功
如果IUD = ""也就是只要UID为空,连接成功
为什么?
如果UID、密码错误,我在debug编译运行的时候,会弹出
Unhandled exception at 0x7c812afb in EM-system.exe: Microsoft C++ exception: _com_error at memory location 0x0012f204..
的异常,我点continue还能继续显示 连接失败 ,但是在release下运行,就会
Unhandled exception at 0x7c812afb in EM-system.exe: Microsoft C++ exception: _com_error at memory location 0x0012f72c..
点continue
EM-system.exe has triggered a breakpoint
点continue
就不行了,会一直弹这个错误。我使用的方法是在stdafx加
#import "c:\program files\common files\system\ado\msado15.dll"no_namespace rename("EOF","adoEOF")
在winapp的initinstance里面加AfxOleInit();
然后声明变量
_ConnectionPtr pConnetion;
_RecordsetPtr pRecordset;
_CommandPtr pCommand;
,然后操作,这样对吗?需要改进吗?还有一个问题是:
我在stdafx.cpp里面
int i;
或者
CString i;
然后在stdafx.h里面
extern int i;
或者
extern CString i;这样那个i就可以当全局变量使用,可是如果_ConnectionPtr pConnetion;再
extern _ConnectionPtr pConnetion;
编译就会报错,这又是什么原因呢?希望高手指教。

解决方案 »

  1.   

    try...catch...显示出错误信息;
      

  2.   

    "还有一个问题是:
    我在stdafx.cpp里面
    int i;
    或者
    CString i;
    然后在stdafx.h里面
    extern int i;
    或者
    extern CString i;"
    一般全局变量放cpp文件里,其他文件调用就加extern
      

  3.   

    sql.pCommand.CreateInstance(__uuidof(Command));
    sql.pConnetion.CreateInstance(__uuidof(Connection));
    sql.pRecordset.CreateInstance(__uuidof(Recordset));
    HRESULT hr;
    hr = sql.pConnetion->Open("driver={SQL Server};Server=.\\sqlexpress;DATABASE=企业员工管理系统;","","",adModeUnknown);
    if(SUCCEEDED(hr))
    AfxMessageBox(L"和服务器连接成功",MB_OK,0);
    else
    {
    AfxMessageBox(L"和服务器连接失败",MB_OK,0);
    exit(0);
    } if(ID == "")
    {
    AfxMessageBox(L"账号不能为空",MB_OK,0);
    return;
    }
    sql.pCommand->put_ActiveConnection(_variant_t((IDispatch*)sql.pConnetion));//ActiveConnection = sql.pConnetion;
    CString command = L"select 密码 from 账号 where 员工编号='"+ID+L"' and 密码='"+PSW+L"'";
    sql.pCommand->CommandText = (_bstr_t)command;
    VARIANT line;
    try{
    sql.pRecordset = sql.pCommand->Execute(&line,NULL,adCmdText);
    }
    catch(_com_error e)///捕捉异常
    {
    CString errormessage;
    errormessage.Format(L"连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);///显示错误信息
    }
    int i=0;
    while(!sql.pRecordset->adoEOF)
    {
    i++;
    if(i>1)
    {
    AfxMessageBox(L"账号密码错误",MB_OK,0);
    return;
    }
    sql.pRecordset->MoveNext();
    }
    if(i<1)
    {
    AfxMessageBox(L"账号密码错误",MB_OK,0);
    return;
    } AfxMessageBox(L"登陆成功",MB_OK,0);
      

  4.   

    try{
    sql.pRecordset = sql.pCommand->Execute(&line,NULL,adCmdText);
    }
    catch(_com_error e)///捕捉异常
    {
    CString errormessage;
    errormessage.Format(L"连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
    AfxMessageBox(errormessage);///显示错误信息
    }
    就是这段
    会弹出:
    错误信息:不正确的变量类型