1.ACCESS 2000
_ConnectionPtr m_pConn;
CString m_sConn="Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=d:\\db1.mdb";
m_pConn.CreateInstance("ADODB.Connection");
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,"","",adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox("不能连接数据库 source!");
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox("不能连接数据库 error!");
return FALSE;
}
2.SQL Server 2000
_ConnectionPtr m_pConn;
CString m_sConn="Provider=SQLOLEDB.1;Data Source=192.168.3.9;Initial
Catalog=sode”; //sode是数据库服务器192.168.3.9上的一个数据库 m_pConn.CreateInstance(“ADODB.Connection”); try { HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,”sa”,”mapper”,adConnectUnspecified); if (FAILED(hr)) { AfxMessageBox(“不能连接数据库 source!”); return FALSE; } } catch(_com_error e) { AfxMessageBox(“不能连接数据库 error!”); return FALSE; }
3.Oracle 9i
_ConnectionPtr m_pConn;
CString m_sConn="Provider=MSDAORA.1;Data Source=sode_192.168.3.9"; //使用
ms连接库,sode为SID,192.168.3.9为机器ip m_pConn.CreateInstance(“ADODB.Connection”); try { HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,”sodeUser”,”sodePw”,adConnectUnspecified); if (FAILED(hr)) { AfxMessageBox(“不能连接数据库 source!”); return FALSE; } } catch(_com_error e) { AfxMessageBox(“不能打开数据库 error!”); return FALSE; }