if(!AfxOleInit())
    {
       AfxMessageBox("OLE初始化出错!");
    };//初始com库
//连接数据库
_ConnectionPtr m_pConnect;
        _ConnectionPtr pConn(__uuidof(Connection));//智能指针对象
_RecordsetPtr pRst(__uuidof(Recordset));
    try {       m_pConnect->Open("Provider=SQLOLEDB;Data Source=QSDOQZZH75KXTY8;Initial Catalog=DIALOG;","sa","19891114",-1);       MessageBox("连接数据库成功!"); }
    catch(_com_error e)
{       AfxMessageBox(e.ErrorMessage());       return; } while(!pRst->rsEOF)
{
UpdateData(TRUE);
                CString str;
CString name;
CString pwd;
str.Format("select * from 用户信息表 where 用户名-'%s' and 密码'%s'",name,pwd);
if((m_username=="")||(m_userpassword==""))
{
MessageBox("用户名或密码不能为空!","错误",MB_OK);
}
else if(name==m_username && pwd==m_userpassword)
{
            MessageBox("用户名或密码不能为空!","错误",MB_OK);
}
        else
{
MessageBox("用户名或密码不正确!","错误",MB_OK);
}
        UpdateData(FALSE);我对于连接数据库不懂,照着网上连接的方式,改了改,可是运行结果是提示无效指针,我做的是一个聊天软件的登录,是在登录button的函数下进行的,请牛人知道

解决方案 »

  1.   

    使用CoCreateInstance或者 CoInitializeEx在你的代码前面执行一次。
      

  2.   


    //stdafx.h
    #import "C:\Program Files\Common Files\System\ADO\msado15.dll"\
    no_namespace rename("EOF","sqlEOF")
    #include <icrsint.h>
    //应用程序InitInstance方法中加入
    ::CoInitialize(NULL);
    //声明
    public:
    _ConnectionPtr m_pConnection;
    _RecordsetPtr m_pRecordset;
    _CommandPtr m_pCommand;
    //调用
    m_pConnection.CreateInstance(__uuidof(Connection));
    m_pRecordset.CreateInstance(__uuidof(Recordset));
    m_pCommand.CreateInstance(__uuidof(Command));
    m_pConnection->ConnectionString="Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=test;User ID=sa;Password=123456";
    m_pConnection->ConnectionTimeout=60;
    HRESULT hr=m_pConnection->Open("","","",0);
    if(hr!=S_OK)
    MessageBox(_T("Can not connection to database"));
    MessageBox(_T("success!"));
    m_pConnection->Close();
      

  3.   

    while(!pRst->rsEOF)
    {
    UpdateData(TRUE);
      CString str;
    CString name;
    CString pwd;
    str.Format("select * from 用户信息表 where 用户名-'%s' and 密码'%s'",name,pwd);
    if((m_username=="")||(m_userpassword==""))
    {
    MessageBox("用户名或密码不能为空!","错误",MB_OK);
    }
    else if(name==m_username && pwd==m_userpassword)
    {
      MessageBox("用户名或密码不能为空!","错误",MB_OK);
    }
      else
    {
    MessageBox("用户名或密码不正确!","错误",MB_OK);
    }
      UpdateData(FALSE);你记录集操作这部份有错误..
    你没打开记录集..Open操作
      

  4.   

    m_pConnection->ConnectionString="Provider=SQLOLEDB;Data Source=.;Initial Catalog=test;User ID=sa;Password=123456";