void CMyDlg::OnButtonSure() 
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
HRESULT hr;
_RecordsetPtr pMyRecordset;
hr=pMyRecordset.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
{
AfxMessageBox("Createinstance of Recordset fail!");
return;
}
CString strSQL;
CString strPWD;
_variant_t var;
CString strUserID;
CString strValue;
int Quan;
strSQL="SELECT * FROM User";
try
{
hr=pMyRecordset->Open(_variant_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
-> adCmdText);
if(SUCCEEDED(hr))
{
while(!pMyRecordset->adoEOF)
{
var=pMyRecordset->GetCollect((long)0);
if(var.vt!=VT_NULL)
strUserID=(LPCSTR)_bstr_t(var);
var=pMyRecordset->GetCollect("PWD");
if(var.vt!=VT_NULL)
strPWD=(LPCSTR)_bstr_t(var);
var=pMyRecordset->GetCollect("Mark");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
Quan=atoi(strValue);
if(strUserID==m_UserID&&strPWD==m_Pwd)
{
if(Quan==1)
StudentMes.DoModal();
if(Quan==2)
TeacherMes.DoModal();
if(Quan==3)
SchoolOffice.DoModal();
return;
}
pMyRecordset->MoveNext();
}
AfxMessageBox("用户不存在或密码错误!");
}
else
{
AfxMessageBox("Open recordset fail!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
pMyRecordset->Close();
pMyRecordset=NULL;
}如题,调试到箭头处的时候出现该错误,请高手帮忙啊,急...

解决方案 »

  1.   

    我是在文件夹下建了一个*.udl文件,测试连接成功的,表也存在
      

  2.   

    m_pConnection.CreateInstance(__uuidof(Connection));
    m_pConnection->ConnectionString="File Name=my_data.udl";
    m_pConnection->ConnectionTimeout=20;上面是连接语句,my_data.udl是那个连接数据库的文件
      

  3.   

    BOOL CMyDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here
    HRESULT hr;
    try
    {
    hr=m_pConnection.CreateInstance(__uuidof(Connection));
    if(SUCCEEDED(hr))
    {
    m_pConnection->ConnectionString="File Name=my_data.udl";
    m_pConnection->ConnectionTimeout=20;
    hr=m_pConnection->Open("","","",adConnectUnspecified);
    if(FAILED(hr))
    {
    AfxMessageBox("open fail!");
    return TRUE;
    }
    }
    else
    {
    AfxMessageBox("Createinstance of Connection fail!");
    return TRUE;
    }
    }
    catch(_com_error *e)
    {
    _bstr_t bstrSource(e->Source());
    _bstr_t bstrDescription(e->Description());
    AfxMessageBox(bstrSource+bstrDescription);
    return TRUE;
    }
    return TRUE;  // return TRUE  unless you set the focus to a control
    }这是那个连接函数
      

  4.   

    BOOL CMyApp::InitInstance()
    {
    AfxEnableControlContainer();
    ::CoInitialize(NULL);
    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif CMyDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
    } // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }这是初始化com库
      

  5.   

    int CMyApp::ExitInstance() 
    {
    // TODO: Add your specialized code here and/or call the base class
    ::CoUninitialize();
    return CWinApp::ExitInstance();
    }
    清除com库
      

  6.   

    m_pConnection-> ConnectionString="File   Name=my_data.udl"; 
    这种用法没见过...
    不能把他换成udl里面的连接字符串吗?
      

  7.   

    不好意思,刚上课去了
    m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Password=054100128;Persist Security Info=True;User ID=sa;Initial Catalog=教务管理;Data Source=YGXYANG";
    m_pConnection->ConnectionTimeout=20;这是换成连接字符串,还是相同的问题,那个数据库的名字是中文应该没关系吧?
      

  8.   

    我不知道俄为了测试,你不如直接用m_pConnection执行一下,比如select * into 新表...
    看看有没有新表生成,如果有
      

  9.   

    额...我刚试了一下,发现是表的名字有问题,把表的名字由User改成Users后就可以了,汗啊...不过这才刚开始,接下来几天应该会有不少问题需要你帮忙,先谢了,呵呵...