我有一个程序使用msado15.dll连接到数据库。该程序在我的Windows 7机器上运行得很好,但大多数(如果不是全部的话)最终用户正在运行Windows XP,但无法正常工作。我已经确定错误是打开数据库,但似乎没有错误信息,所以我不确定发生了什么问题。这里是写的代码我,以及调用打开数据库:在Windows 7中编译的ADO应用程序在Windows 7中运行不能在Windows XP中运行msado15.dll
Database.h:
#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
rename("EOF","adoEOF")
typedef ADODB::_RecordsetPtr RecPtr;
typedef ADODB::_ConnectionPtr CnnPtr;
struct Database {
CnnPtr m_Cnn;
Database();
~Database();
bool Open(const char* CnnStr, const char* UserName, const char* Pwd);
RecPtr Execute(const char* CmdStr);
bool Close();
};
Database.cpp:
#include "stdafx.h"
#include "Database.h"
#include "ErrorDlg.h"
using namespace ADODB;
Database::Database()
{
m_Cnn = NULL;
}
bool Database::Open(const char *CnnStr, const char *UserName, const char *Pwd)
{
::CoInitialize(NULL);
HRESULT hr;
try {
hr = m_Cnn.CreateInstance(__uuidof(Connection));
m_Cnn->Open(CnnStr, UserName, Pwd, adConnectUnspecified);
}
catch (_com_error &e) {
CErrorDlg dlg; // makes a window that shows info aobut an error
// the following pops up on XP when you start the .exe file and attempt to connect
dlg.DoError(_T("Error opening database.")); //dlg.DoError(e.ErrorMessage()) gives me a blank message
return 0;
}
return 1;
}
RecPtr Database::Execute(const char *CmdStr)
{
try {
if (m_Cnn == NULL)
return NULL;
return m_Cnn->Execute(CmdStr, NULL, 1);
}
catch (_com_error &e) {
CErrorDlg dlg;
dlg.DoError(_T("Error executing database command."));
return NULL;
}
}
bool Database::Close()
{
if (m_Cnn == NULL)
return 0;
try {
m_Cnn->Close();
m_Cnn = NULL;
}
catch (_com_error &e) {
CErrorDlg dlg;
dlg.DoError(_T("Error closing database"));
return 0;
}
return 1;
}
Database::~Database()
{
try {
if (m_Cnn) {
m_Cnn->Close();
m_Cnn = NULL;
}
}
catch (_com_error &e) {
CErrorDlg dlg;
dlg.DoError(_T("Error deconstructing database"));
}
}
打开数据库这样:
m_db.Open("driver={SQL Server};server=myServer;database=myDatabase","myUser","myPwd")
其中m_db
是Database
。我也尝试了""
用于上面的用户和密码,这给了我相同的结果:在7,但不是XP的作品。
为什么在Windows 7上可以正常工作,但不能在Windows XP上正常工作?我应该怎么做才能纠正这个问题?
我正在使用以下#import字符串'#import no_namespace rename(“EOF”,“adoEOF”)' –
此外,Microsoft KB 2640696对于我看到的问题更加明确,[在Windows 7 SP1或Windows Server 2008 R2 SP1中编译的基于ADO的应用程序不在早期版本的Windows中运行](http://support.microsoft.com/kb/2640696) –