WMSADO.h下
创建三个智能指针
public:
_ConnectionPtr m_pConnection; //数据库
_RecordsetPtr m_pRecordset; //命令
_CommandPtr m_pCommand; //记录
public:
void OnInitADOConn();
WMSADO.cpp下
void WMSADO::OnInitADOConn()
{
::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
_bstr_t strConnect = "Provider=SQLOLEDB;Server=127.0.0.1;Database=WMS;uid=sa;pwd=12345678";
m_pConnection->Open(strConnect,"","", adModeUnknown);
AfxMessageBox(_T("Database connection success!"));
}
catch (_com_error e)
{
AfxMessageBox(_T("connction Error!"));
}
}
其中Database是数据库名(不可以加扩展名.mdf)uid指登录名pwd:密码 adModeUnKnown:应用程序对数据库操作的权限,这里adModeUnKnown是指数据库默认的权限。
时间: 2024-10-25 00:23:23