DVD出租系统【7】个人总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunjikui1255326447/article/details/89093525

1、连接数据库导入dll:

#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "adoEof")

放在stdafx.h倒数第二行位置,并给程序管理员权限

2、新界面需要来连接数据库,对连接数据库的指针从父窗口进行传递,传递完成后在构造函数中进行初始化:

3、打开数据库之后记得关闭程序时候

void CDVDRentDlg::CloseDB()
{
	if (m_pConnection && m_pConnection->State == adStateOpen) {
		m_pConnection->Close();
	}
}

void CDVDRentDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类
	CloseDB();
	CDialogEx::OnCancel();
}

4、连接数据库步骤:

BOOL CDVDRentDlg::connectDB()
{
	HRESULT hr;
	try
	{
		hr = m_pConnection.CreateInstance(__uuidof(Connection));
		if (SUCCEEDED(hr)) {
			//_bstr_t connectStr = _T("Provider=SQLNCLI;Server=192.168.0.100;Database=DVDRentDB;Uid=cctry;Pwd=www.cctry.com");
			_bstr_t connectStr = _T("Provider=sqloledb;Data Source=192.168.1.31;Initial Catalog=DVDRentDB;User Id=sa;Password=123456");
			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;
}

猜你喜欢

转载自blog.csdn.net/sunjikui1255326447/article/details/89093525