您的位置:程序门 -> vc/mfc ->



【求助】vc++中数据库连接的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


【求助】vc++中数据库连接的问题
发表于:2007-10-23 10:07:25 楼主
用vc++与sql   server   2000进行数据库开发,初始化数据库连接这一步总是失败,请教高手

以下程序在运行时,并没有进入       if(succeeded(hr))
请问是什么原因?
在网上已经搜索过连接数据库失败可能的原因,试着查找自己程序的原因,都不得要领,还请高手指教啊,谢谢了。


::coinitialize(null);
_connectionptr   m_pconnection;
hresult   hr;
try
{
      //实例化连接对象
      hr=m_pconnection.createinstance(__uuidof(connection));
      if(succeeded(hr))
      {
          //设置连接串属性为udl文件
          m_pconnection-> connectionstring   =   "file   name=my_data1.udl";
          //设置等待连接打开的时间为20s
          m_pconnection-> connectiontimeout=20;
          hr=m_pconnection-> open("","","",adconnectunspecified);
          if(failed(hr))
          {
                afxmessagebox("open   fail!");
                return   true;
          }
        }
        else
        {
              afxmessagebox("数据库连接失败!");
              return   true;
        }
}

catch(_com_error   e)
{
    _bstr_t   bstrsource(e.source());
    _bstr_t   bstrdescription(e.description());
    afxmessagebox(bstrsource+bstrdescription);
    return   true;
}

已经在stdafx.h头文件中加入
#import   "c:\program   files\common   files\system\ado\msado15.dll"   no_namespace   re
name("eof","adoeof")


发表于:2007-10-23 10:13:101楼 得分:0
  hr=m_pconnection.createinstance(__uuidof(connection));   中的hr是不是空的啊?
if(!afxoleinit())      
{      
afxmessagebox("ole初始化出错!");      
return       false;      
}  
初始化了吗?
发表于:2007-10-23 10:29:542楼 得分:0
偶估计是你的succeeded()宏使用问题.
succeeded
provides   a   generic   test   for   success   on   any   status   value.   non-negative   numbers   indicate   success.
#define   succeeded(status)           ((hresult)(status)   > =   0)

你最好还是这样子写(用failed宏,我这里使用了自定义名字空间adong):
try{
//实例化数据库连接
if   (failed(m_padoconn.createinstance(__uuidof(adong::connection))))   //__uuidof(adong::connection)
{
//m_padoconn.release();//还没有实例化成功,不需要release
afxmessagebox("ado   connection:create   connection   instance   failed!");
//this-> sendmessage(wm_close);
return   false;
}
}
catch   (_com_error   e)  
{
cstring   strcomerror;
strcomerror.format("错误编号:   %08lx\n错误信息:   %s\n错误源:   %s\n错误描述:   %s",
e.error(),                                     //   错误编号
e.errormessage(),                     //   错误信息
(lpcstr)   e.source(),                 //   错误源
(lpcstr)   e.description());     //   错误描述
messagebox(strcomerror,"数据库错误",mb_iconexclamation);
//this-> sendmessage(wm_close);
return   false;
}
发表于:2007-10-23 10:34:073楼 得分:0
afxoleinit()
发表于:2007-11-25 10:30:304楼 得分:0
谢谢各位。。。已经解决了~~


快速检索

最新资讯
热门点击