使用VC6进行ADO编程,使用SQL SERVER 2000+WINDOWS SERVER 2003+VC6
目的是将SERVER产生的日志文件--文本格式,写入到SQL SERVER数据库,实现功能为:
1.修改源日志格式为以跳格键分割字段,字段用引号引起,写入成一个新的日志文件。
2.程序读出新日志文件,写入到数据库。
由于是初学VC,不当之处请高人指正,万分感谢!
第一部分
工程名称为vcado,如下是在vcado.cpp中的重要源代码:此代码目的为打开Connection对象!
BOOL CVcadoApp::InitInstance()
{
AfxEnableControlContainer();
AfxOleInit();///初始化COM库
////////////连接数据库//////////////
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("DSN=test;UID=;PWD=;","","",adModeUnknown);///连接数据库
}
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CVcadoDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
int CVcadoApp::ExitInstance()
{
if(m_pConnection->State)
m_pConnection->Close(); ///如果已经打开了连接则关闭它
return CWinApp::ExitInstance();
}
由于本人上网时间问题,下次进行发表“第二部分”。
本文地址:http://com.8s8s.com/it/it426.htm