代码如下:
m_pConnection.CreateInstance("ADODB.Connection");
CString strCn;
strCn.Empty();
strCn="provider=SQLOLEDB;data source="+this->DataSource
+";DATABASE="+this->Catalog
+";userID="+this->UserID
+";Password="+this->Password;
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(this->UserID);
_variant_t bpwdstr=_variant_t(this->Password);
//打开一个连接
try
{
m_pConnection->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);//adOpenUnspecified
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("Warning: 连接数据库发生异常. 错误信息: %s; 文件: %s; 行: %d ", e.ErrorMessage(), __FILE__, __LINE__);
AfxMessageBox(errormessage);
return FALSE;
}
catch(...)
{
AfxMessageBox("Warning :连接数据库时发生未知错误");
return FALSE;
}
return TRUE;
为什么运行是总是连接不上.啊..各位帮帮忙啊.
m_pConnection.CreateInstance("ADODB.Connection");
CString strCn;
strCn.Empty();
strCn="provider=SQLOLEDB;data source="+this->DataSource
+";DATABASE="+this->Catalog
+";userID="+this->UserID
+";Password="+this->Password;
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(this->UserID);
_variant_t bpwdstr=_variant_t(this->Password);
//打开一个连接
try
{
m_pConnection->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);//adOpenUnspecified
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("Warning: 连接数据库发生异常. 错误信息: %s; 文件: %s; 行: %d ", e.ErrorMessage(), __FILE__, __LINE__);
AfxMessageBox(errormessage);
return FALSE;
}
catch(...)
{
AfxMessageBox("Warning :连接数据库时发生未知错误");
return FALSE;
}
return TRUE;
为什么运行是总是连接不上.啊..各位帮帮忙啊.
试一下
{
AfxMessageBox("Component Initialize error!");
return FALSE;
}
HRESULT _hr = raw_Open(ConnectionString, UserID, Password, Options);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _hr;
}
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
你用下面的方法:HRESULT hr;
hr = m_pConnection.CreateInstance(__uuidof(Connection));
hr = m_pConnection->Open((LPCTSTR)m_strCn, L"", L"", adCmdUnspecified);你直接单步看hr的值,看是不是第一步就出错了???
一般来说,我用ado的做法是:创建工程,不选任何数据库支持。
然后在stdafx.h文件的最后加上下面这两句
#include <comdef.h>
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")然后在***app.cpp的InitInstance()函数中加上
if(!AfxOleInit())//这就是初始化COM库
{
AfxMessageBox("OLE初始化出错!");
return FALSE;
}
就可以正常使用的了。还有就是你先看一下MSADO15.DLL在你c盘的那个目录下面吧,如果你系统不是装在c盘,那就要改路径了
把你的连接字符串写成常量值,再创建连接对象,再连接数据库。
#include <comdef.h>
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")在InitInstance()函数中加上
if(!AfxOleInit())//这就是初始化COM库
{
AfxMessageBox("OLE初始化出错!");
return FALSE;
}
之后加了::CoInitialize(NULL);
运行后hr = m_pConnection.CreateInstance(__uuidof(Connection));没错
hr = m_pConnection->Open(LPCTSTR(strCn), L"", L"",adCmdUnspecified);出问题#3149
我本机上的sql用户sa没设定密码
//AfxOleInit() 和 CoInitialize()用一个就可以了
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(NULL,strComError,"错误",MB_ICONEXCLAMATION);
}你用上面的语句捕抓一下异常就知道了。建议你不要用sa这个超级管理员登陆,另外起一个账号和密码试一下吧
strCn="provider={SQLOLEDB};server=127.0.0.1;DATABASE=pms_db;userID=xie;Password=123";
后出现错误提示:
错误源:ADODB.CONNECTION
错误描述:未找到提供程序,该程序可能未正确安装.
strSRC="Driver=SQL Server;Server=127.0.0.1"; ////////////sql服务器
strSRC+=";Database=";
strSRC+="myDB"; ///////////////数据库
strSRC+=";UID=usename;PWD=123456"; /////////////账号和密码
_bstr_t bstrSRC(strSRC);
m_pConnection ->ConnectionTimeout =8;
hr=m_pConnection ->Open (bstrSRC,"","",-1); 我一般写连接字符是这样写的,你看行不行吧,如果不行的话,我觉得可能是sql 2000的问题。。
provider=SQLOLEDB.1;