ADO를 공부하기 위해 샘플입니다. 아무런 기능도 없다. 찾고, 추가하는 기능 밖에 없습니다. 뭐 또 나중에는 기능을 추가할지도 모르지만…
1. import는 stdafx.h에서
#import “C:\\Program Files\\Common Files\\System\\ado\\msado15.dll” no_namespace rename(”EOF”, “adoEOF”)
해준다.
2. 초기화는
BOOL CDicharDlg::OnInitDialog()
{
…..
// TODO: Add extra initialization here
CoInitialize(NULL);
HRESULT hr = g_pConn.CreateInstance(__uuidof(Connection));
if(FAILED(hr))
{
AfxMessageBox(”연결객체를 생성하지 못하였습니다.”);
}
hr = g_pConn->Open(_bstr_t(”Provider = Microsoft.Jet.OLEDB.4.0;Data Source = Dic.MDB”), _bstr_t(”"), _bstr_t(”"), adModeUnknown);
if(FAILED(hr))
{
AfxMessageBox(”데이터 소스를 열지 못하였습니다.”);
}
return TRUE;
}
3. 컴포넌트 Release를 위한 작업
BOOL CDicharDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if((g_pConn->State & adStateOpen) == adStateOpen)
{
g_pConn->Close();
}
CoUninitialize();
return CDialog::DestroyWindow();
}
4. 찾기
void CDicharDlg::OnSearchButton()
{
// TODO: Add your control notification handler code here
try
{
_CommandPtr pCommand;
HRESULT hr = pCommand.CreateInstance(__uuidof(Command));
if(FAILED(hr))
{
AfxMessageBox(”명령객체 생성 실패”);
}
CString strCommand, temp;
strCommand = “Select * From Word WHERE word = ‘”;
m_ctrlSearchEdit.GetWindowText(temp);
temp += “‘”;
strCommand += temp;
pCommand->ActiveConnection = g_pConn;
pCommand->CommandText = (_bstr_t)strCommand;
_RecordsetPtr pRecordset;
hr = pRecordset.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
{
AfxMessageBox(”레코드셋객체 생성 실패”);
}
pRecordset->CursorLocation = adUseClient;
pRecordset->Open((IDispatch*)pCommand, vtMissing, adOpenForwardOnly, adLockReadOnly, adCmdUnknown);
CString strContents;
strContents = (char*)(_bstr_t)pRecordset->Fields->GetItem(”contents”)->Value;
pRecordset->Close();
m_ctrlContentsEdit.SetWindowText(strContents);
UpdateData(FALSE);
}
catch(_com_error &e)
{
TRACE(e.ErrorMessage());
AfxMessageBox(”해당하는 단어가 없습니다.”);
m_ctrlSearchEdit.SetWindowText(”");
}
}
5. 추가
void CAddDlg::OnOK()
{
// TODO: Add extra validation here
try
{
_CommandPtr pCommand;
HRESULT hr = pCommand.CreateInstance(__uuidof(Command));
if(FAILED(hr))
{
AfxMessageBox(”명령객체 생성에 실패하였습니다.”);
return;
}
CString strQuery, strWord, strContents;
m_ctrlWordEdit.GetWindowText(strWord);
m_ctrlContentsEdit.GetWindowText(strContents);
strQuery.Format(”INSERT INTO Word (word, contents) VALUES (’%s’, ‘%s’)”, strWord, strContents);
pCommand->ActiveConnection = g_pConn;
pCommand->CommandText = (_bstr_t)strQuery;
pCommand->Execute(NULL, NULL, adCmdText);
}
catch(_com_error &e)
{
TRACE(e.ErrorMessage());
AfxMessageBox(”추가에 실패 했습니다.”);
}
CDialog::OnOK();
}
1. import는 stdafx.h에서
#import “C:\\Program Files\\Common Files\\System\\ado\\msado15.dll” no_namespace rename(”EOF”, “adoEOF”)
해준다.
2. 초기화는
BOOL CDicharDlg::OnInitDialog()
{
…..
// TODO: Add extra initialization here
CoInitialize(NULL);
HRESULT hr = g_pConn.CreateInstance(__uuidof(Connection));
if(FAILED(hr))
{
AfxMessageBox(”연결객체를 생성하지 못하였습니다.”);
}
hr = g_pConn->Open(_bstr_t(”Provider = Microsoft.Jet.OLEDB.4.0;Data Source = Dic.MDB”), _bstr_t(”"), _bstr_t(”"), adModeUnknown);
if(FAILED(hr))
{
AfxMessageBox(”데이터 소스를 열지 못하였습니다.”);
}
return TRUE;
}
3. 컴포넌트 Release를 위한 작업
BOOL CDicharDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if((g_pConn->State & adStateOpen) == adStateOpen)
{
g_pConn->Close();
}
CoUninitialize();
return CDialog::DestroyWindow();
}
4. 찾기
void CDicharDlg::OnSearchButton()
{
// TODO: Add your control notification handler code here
try
{
_CommandPtr pCommand;
HRESULT hr = pCommand.CreateInstance(__uuidof(Command));
if(FAILED(hr))
{
AfxMessageBox(”명령객체 생성 실패”);
}
CString strCommand, temp;
strCommand = “Select * From Word WHERE word = ‘”;
m_ctrlSearchEdit.GetWindowText(temp);
temp += “‘”;
strCommand += temp;
pCommand->ActiveConnection = g_pConn;
pCommand->CommandText = (_bstr_t)strCommand;
_RecordsetPtr pRecordset;
hr = pRecordset.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
{
AfxMessageBox(”레코드셋객체 생성 실패”);
}
pRecordset->CursorLocation = adUseClient;
pRecordset->Open((IDispatch*)pCommand, vtMissing, adOpenForwardOnly, adLockReadOnly, adCmdUnknown);
CString strContents;
strContents = (char*)(_bstr_t)pRecordset->Fields->GetItem(”contents”)->Value;
pRecordset->Close();
m_ctrlContentsEdit.SetWindowText(strContents);
UpdateData(FALSE);
}
catch(_com_error &e)
{
TRACE(e.ErrorMessage());
AfxMessageBox(”해당하는 단어가 없습니다.”);
m_ctrlSearchEdit.SetWindowText(”");
}
}
5. 추가
void CAddDlg::OnOK()
{
// TODO: Add extra validation here
try
{
_CommandPtr pCommand;
HRESULT hr = pCommand.CreateInstance(__uuidof(Command));
if(FAILED(hr))
{
AfxMessageBox(”명령객체 생성에 실패하였습니다.”);
return;
}
CString strQuery, strWord, strContents;
m_ctrlWordEdit.GetWindowText(strWord);
m_ctrlContentsEdit.GetWindowText(strContents);
strQuery.Format(”INSERT INTO Word (word, contents) VALUES (’%s’, ‘%s’)”, strWord, strContents);
pCommand->ActiveConnection = g_pConn;
pCommand->CommandText = (_bstr_t)strQuery;
pCommand->Execute(NULL, NULL, adCmdText);
}
catch(_com_error &e)
{
TRACE(e.ErrorMessage());
AfxMessageBox(”추가에 실패 했습니다.”);
}
CDialog::OnOK();
}
댓글을 달아 주세요
안녕하세요 ...
2011/06/28 16:48 [ ADDR : EDIT/ DEL : REPLY ]나는 또한 당신의 블로그를 읽고 ... 어떤 꿈을 와우!
당신의 COM을 가져 주셔서 감사합니다
우리의 새로운 모험에 대해 곧 당신을보고!