fingerplay 发表于 2008-7-12 13:17

【求助】请高手们都帮忙看一下啊

我做了一个学生选课管理系统,在登陆的时候总是弹出一个对话框,提示Runtime出错,我用的是sqlserver2000的数据库服务器,登陆要用的用户ID和密码都保存在名为SC的数据库下的USER表中,用户ID和密码的输入分别用m_sUserID和m_sPassWord来记录,连接方式是ADO,我是用一个CString字符串传送sql语句,在执行ecord->open语句的时候出错,字符串如下:
"SELECT * FROM USER WHERE UserID=%s",m_sUserID

完整程序如下,
void CTestDlg::OnLogin()
{
        try
        {
        _RecordsetPtr pRecord;
        _variant_t value;
        if(UpdateData()==false)
        return;
                //生成Recordset实例
                pRecord.CreateInstance("ADODB.Recordset");
                CString sql;               
            sql.Format("SELECT * FROM USER WHERE UserID=%s",m_sUserID);

                //打开记录集
                pRecord->Open(sql.GetBuffer(0),_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);                     //这里提示出错
                if(!pRecord->State)
                {
                        MessageBox("不存在该用户");
                }
                else
                {
                        sql+="AND PassWord=";
                        sql+=m_sPassWord;
                        pRecord->Open(sql.GetBuffer(0),_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
                        if(!pRecord->State)
                        {
                                MessageBox("密码不正确");
                        }
                        else
                        {

                                MessageBox("登陆成功");
                        }
                }
                pRecord->Close();
                pRecord=NULL;
       
        }
        catch(_com_error *e)
        {
                MessageBox(e->ErrorMessage());
        }       
       
}

请各位高手帮忙修改,感激不尽!

fingerplay 发表于 2008-7-12 13:21

再补充一段代码,连接数据库的
BOOL CTestApp::InitInstance()
{
        AfxEnableControlContainer();
        //初始化OLE支持,使本程序可以访问数据库
        if(AfxOleInit()==false)
        {
                MessageBox(NULL,"初始化OLE支持失败","失败",MB_OK);
                return false;
        }
        //连接数据库
        try
        {
                HRESULT hr;
                hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
                if(SUCCEEDED(hr))
                {
               
                hr = m_pConnection->Open("Data Source=sc;UID=sa;PWD=;","","",adModeUnknown);

                        if(SUCCEEDED(hr))
                        {

                        }
                }
        }
        catch(_com_error e)///捕捉异常
        {
                CString errormessage;
                errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
                AfxMessageBox(errormessage);///显示错误信息
                return FALSE;
        }
        try
        {
                if(m_pConnection->State)
                m_pConnection->Close(); ///如果已经打开了连接则关闭它
        }
        catch(...)
        {
        }

#ifdef _AFXDLL
        Enable3dControls();                        // Call this when using MFC in a shared DLL
#else
        Enable3dControlsStatic();        // Call this when linking to MFC statically
#endif

        CTestDlg 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;
}

[[it] 本帖最后由 fingerplay 于 2008-7-12 13:22 编辑 [/it]]

womarcol 发表于 2008-7-20 01:21

乱七八糟的  你把你的源程序打包上传 把你的数据库建表的也用txt文档上传  我给你看看

页: [1]

编程论坛