|
各位大佬!VS2017 MFC连接SQL Server 2016出现“未发现数据源名称并且未指定默认驱动程序”怎么解决?
_ConnectionPtr m_pConnection;
BOOL CDBTest1Dlg::ConnectDB()
{
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance(__uuidof(Connection));
if (SUCCEEDED(hr)) {
_bstr_t connectStr = _T("Server=192.168.31.218;Database=PigSystim;User Id=sa;Password=sa123456;");
//_bstr_t connectStr = _T("Provider=sqloledb;Data Source=192.168.1.5;Initial Catalog=DVDRentDB;User Id=cctry;Password=www.cctry.com");
m_pConnection->ConnectionTimeout = 20;
hr = m_pConnection->Open(connectStr, _T(""), _T(""), adConnectUnspecified);
if (FAILED(hr)) {
MessageBox(_T("打开与数据库的连接失败."));
return FALSE;
}
}
else {
MessageBox(_T("实例化 Connection 对象失败."));
return FALSE;
}
}
catch (_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
MessageBox(bstrDescription, bstrSource);
return FALSE;
}
return TRUE;
}
void CDBTest1Dlg::CloseDB()
{
if (m_pConnection && m_pConnection->State == adStateOpen) {
m_pConnection->Close();
}
}
void CDBTest1Dlg::OnCancel()
{
// TODO: Add your specialized code here and/or call the base class
CloseDB();
CDialog::OnCancel();
}
void CDBTest1Dlg::OnBnClickedButton1()
{
ConnectDB();
// TODO: 在此添加控件通知处理程序代码
} |
上一篇: 求助 :三角函数下一篇: 求助:求AB红色曲线的长度。
|