需要在一个通信服务器下面挂接一个access的数据库,用的这样的代码#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")int SEARCH_DATA(int a,int &b,int &c)
{ _ConnectionPtr m_pConnection;
CoInitialize(NULL); //---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D://ipserver.mdb","","",adModeRead);
}
catch(_com_error e) {
cout<<"数据库连接失败,确认数据库ipserver.mdb是否在当前路径下!"<<endl;
return FALSE;
} _RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM ipserver where cIp=0x107a002c",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText); }
catch(_com_error *e)
{
cout<<e->ErrorMessage()<<endl;
} _variant_t var;
char *ID,*name;
try {
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
cout<<"表内数据为空"<<endl;
return 1;
}
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("gate");
if(var.vt != VT_NULL)
b= _com_util::ConvertBSTRToString((_bstr_t)var);
var = m_pRecordset->GetCollect("cIp");
if(var.vt != VT_NULL)
c=_com_util::ConvertBSTRToString((_bstr_t)var);
cout<<ID<<" is "<<name<<endl;
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
cout<<e->ErrorMessage()<<endl;
}
m_pRecordset->Close();
m_pRecordset = NULL;
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
return b;
return c;
}
编译无错,但每次运行的时候就提示“数据库连接失败,确认数据库ipserver.mdb是否在当前路径下”
各位高手麻烦帮忙看看问题吧……感激不尽!对了,数据库就放在代码的文件夹里,没有用相对路径
{ _ConnectionPtr m_pConnection;
CoInitialize(NULL); //---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D://ipserver.mdb","","",adModeRead);
}
catch(_com_error e) {
cout<<"数据库连接失败,确认数据库ipserver.mdb是否在当前路径下!"<<endl;
return FALSE;
} _RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM ipserver where cIp=0x107a002c",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText); }
catch(_com_error *e)
{
cout<<e->ErrorMessage()<<endl;
} _variant_t var;
char *ID,*name;
try {
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
cout<<"表内数据为空"<<endl;
return 1;
}
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("gate");
if(var.vt != VT_NULL)
b= _com_util::ConvertBSTRToString((_bstr_t)var);
var = m_pRecordset->GetCollect("cIp");
if(var.vt != VT_NULL)
c=_com_util::ConvertBSTRToString((_bstr_t)var);
cout<<ID<<" is "<<name<<endl;
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
cout<<e->ErrorMessage()<<endl;
}
m_pRecordset->Close();
m_pRecordset = NULL;
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
return b;
return c;
}
编译无错,但每次运行的时候就提示“数据库连接失败,确认数据库ipserver.mdb是否在当前路径下”
各位高手麻烦帮忙看看问题吧……感激不尽!对了,数据库就放在代码的文件夹里,没有用相对路径
--》
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\ipserver.mdb","","",adModeRead);
这是个目录地址,不是url的
所以得用\\
void PrintComError(_com_error &e) {
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description()); // Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D://ipserver.mdb","","",adModeRead);
}
catch(_com_error e) {
cout<<"数据库连接失败,确认数据库ipserver.mdb是否在当前路径下!"<<endl;
PrintComError(e);
return FALSE;
}
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description()); // Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\ipserver.mdb","","",adModeRead);
}
catch(_com_error e) {
cout<<"数据库连接失败,确认数据库ipserver.mdb是否在当前路径下!"<<endl;
PrintComError(e);
return FALSE;
}