ADOベースのMFCとSQL Serverの接続

4854 ワード

ステップ1:stdafx.hに追加:
コンパイルエラーが発生しないように、ここに最後のincludeの下に追加してください.
#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")

第二部:***Dlg.hに追加
_ConnectionPtr m_pConnection;

ステップ3:**Dlg.cppのOnInitDialog()に追加
注意:
Database:データベース名、uid:ログインアカウント、pwd:ログインパスワード
	::CoInitialize(NULL);		//   OLE/COM   
	HRESULT hr = NULL;
	try{
		hr = m_pConnection.CreateInstance(__uuidof(Connection));//        
		if (SUCCEEDED(hr))
		{
			_bstr_t strConnect = "Provider=SQLOLEDB;Server=(local);Database=***;uid=***;pwd=***";//    				// 
			hr = m_pConnection->Open(strConnect, "", "", adConnectUnspecified);	//     
			if (FAILED(hr)){
				AfxMessageBox(_T("Open Failed!"));
				return FALSE;
			}
		}
		else
		{
			AfxMessageBox(_T("Create instance of connection failed!"));
			return FALSE;
		}
	}
	catch (_com_error e){
		CString temp;
		temp.Format(L"       \r
:%s", e.ErrorMessage()); AfxMessageBox(temp); return FALSE; }

ステップ4:ボタンでコマンドとレコードセットを使用してデータベースを操作する
データテーブルの作成:
	/*     */
	try{
		CString CmdStr;
		_RecordsetPtr pRst(__uuidof(Recordset));	//     Recordset  pRst
		_CommandPtr pCmd(__uuidof(Command));	//     Command  pCmd
		pCmd->put_ActiveConnection(_variant_t((IDispatch*)m_pConnection));
		/*     */
		CmdStr = _T("CREATE TABLE userinfo( \
			id int PRIMARY KEY,\
			name varchar(20),\
			 password varchar(20), \
			 email varchar(20), \
			 QQ varchar(20),\
			 tel varchar(20))");
		pCmd->CommandText = (_bstr_t)CmdStr;
		pCmd->Execute(NULL, NULL, adCmdText);
		pRst.Release();	//     
		pCmd.Release();	//    
	}
	catch (_com_error e){
		CString errostr;
		errostr.Format(L"    。\r
:%s", e.ErrorMessage()); AfxMessageBox(errostr); }

データの追加:
	/*      */
	try{
		CString CmdStr;
		_RecordsetPtr pRst(__uuidof(Recordset));	//     Recordset  pRst
		_CommandPtr pCmd(__uuidof(Command));	//     Command  pCmd
		pCmd->put_ActiveConnection(_variant_t((IDispatch*)m_pConnection));
		CmdStr = _T("INSERT INTO userinfo(id,name,password,email,QQ,tel)VALUES(88888888,'   ',' ','88888888','88888888			','88888888')");
		pCmd->CommandText = (_bstr_t)CmdStr;
		pCmd->Execute(NULL, NULL, adCmdText);
		pRst.Release();	//     
		pCmd.Release();	//    
	}
	catch (_com_error e){
		CString errostr;
		errostr.Format(L"    。\r
:%s", e.ErrorMessage()); AfxMessageBox(errostr); }

データの変更:
	/*    */
	try{
		CString CmdStr;
		_RecordsetPtr pRst(__uuidof(Recordset));	//     Recordset  pRst
		_CommandPtr pCmd(__uuidof(Command));	//     Command  pCmd
		pCmd->put_ActiveConnection(_variant_t((IDispatch*)m_pConnection));
		CmdStr = _T("UPDATE userinfo SET name = '   ' WHERE id=88888888");
		pCmd->CommandText = (_bstr_t)CmdStr;
		pCmd->Execute(NULL, NULL, adCmdText);
		pRst.Release();	//     
		pCmd.Release();	//    
	}
	catch (_com_error e){
		CString errostr;
		errostr.Format(L"    。\r
:%s", e.ErrorMessage()); AfxMessageBox(errostr); }
データを削除:
	/*    */
	try{
		CString CmdStr;
		_RecordsetPtr pRst(__uuidof(Recordset));	//     Recordset  pRst
		_CommandPtr pCmd(__uuidof(Command));	//     Command  pCmd
		pCmd->put_ActiveConnection(_variant_t((IDispatch*)m_pConnection));
		CmdStr = _T("DELETE FROM userinfo WHERE id = 88888888");
		pCmd->CommandText = (_bstr_t)CmdStr;
		pCmd->Execute(NULL, NULL, adCmdText);
		pRst.Release();	//     
		pCmd.Release();	//    
	}
	catch (_com_error e){
		CString errostr;
		errostr.Format(L"    。\r
:%s", e.ErrorMessage()); AfxMessageBox(errostr); }
クエリーデータ:
	/*    */
	try{
		CString CmdStr;
		_RecordsetPtr pRst(__uuidof(Recordset));	//     Recordset  pRst
		_CommandPtr pCmd(__uuidof(Command));	//     Command  pCmd
		pCmd->put_ActiveConnection(_variant_t((IDispatch*)m_pConnection));
		CmdStr = _T("SELECT * FROM userinfo");
		pCmd->CommandText = (_bstr_t)CmdStr;
		pRst = pCmd->Execute(NULL, NULL, adCmdText);
		while(!pRst->adoEOF)
		{
			//    
			CString str = (_bstr_t)pRst->GetCollect("id") + " " + (_bstr_t)pRst->GetCollect("name") + " " + (_bstr_t)		pRst->GetCollect("email") + " " + (_bstr_t)pRst->GetCollect("QQ");
		pRst->MoveNext(); //      
		}

		pRst.Release();	//     
		pCmd.Release();	//    
	}
	catch (_com_error e){
		CString errostr;
		errostr.Format(L"    。\r
:%s", e.ErrorMessage()); AfxMessageBox(errostr); }