| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 428 人关注过本帖
标题:【求助】请高手们都帮忙看一下啊
只看楼主 加入收藏
fingerplay
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-7-12
收藏
 问题点数:0 回复次数:2 
【求助】请高手们都帮忙看一下啊
我做了一个学生选课管理系统,在登陆的时候总是弹出一个对话框,提示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());
    }    
    
}

请各位高手帮忙修改,感激不尽!
2008-07-12 13:17
fingerplay
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-7-12
收藏
得分:0 
再补充一段代码,连接数据库的
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]]
2008-07-12 13:21
womarcol
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-7-19
收藏
得分:0 
乱七八糟的  你把你的源程序打包上传 把你的数据库建表的也用txt文档上传  我给你看看

走牛x的路,让傻x说去吧!
2008-07-20 01:21
快速回复:【求助】请高手们都帮忙看一下啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011636 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved