CoInitialize(NULL);
HRESULT hr;
_ConnectionPtr m_pConnection;
try {
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\HY.mdb;uid=admin;pwd=111","","",adModeUnknown);
}
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库HY.mdb是否在当前路径下!");
exit(0);}
_variant_t RecordsAffected;
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL; _RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset"); try
{
m_pRecordset->Open("SELECT * FROM HY",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
CString str;
try
{
str.Format("insert into HY(x,y,mac,adcval) values (%d,%d,%d,%d)",x,y,mac,adcval);
m_pRecordset=m_pConnection->Execute((_bstr_t)str,&RecordsAffected,adCmdText);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
CoUninitialize();编译后总是出现“数据库连接失败,确认数据库HY.mdb是否在当前路径下!”的对话框,是什么问题呢??请指点
HRESULT hr;
_ConnectionPtr m_pConnection;
try {
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\HY.mdb;uid=admin;pwd=111","","",adModeUnknown);
}
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库HY.mdb是否在当前路径下!");
exit(0);}
_variant_t RecordsAffected;
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL; _RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset"); try
{
m_pRecordset->Open("SELECT * FROM HY",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
CString str;
try
{
str.Format("insert into HY(x,y,mac,adcval) values (%d,%d,%d,%d)",x,y,mac,adcval);
m_pRecordset=m_pConnection->Execute((_bstr_t)str,&RecordsAffected,adCmdText);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
CoUninitialize();编译后总是出现“数据库连接失败,确认数据库HY.mdb是否在当前路径下!”的对话框,是什么问题呢??请指点
改为
AfxMessageBox( e.Description() );
connection.Open connectionstring,userID,password,optionsuserID=admin
password=111
后面还有四个双引号?
那样传参就不对了吧
不对别嘲笑我啊
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\HY.mdb;","admin","111",adModeUnknown);
改为
hr = m_pConnection.CreateInstance(_uuidof(Connection));
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\HY.mdb","","",adModeUnknown); 如果在当前目录,则写:
hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=HY.mdb","","",adModeUnknown);