ODBC:
Select:
UpdateData(TRUE);
if (m_strODBCName.IsEmpty())
{
return;
}
CFileDialog dlg(FALSE, _T("SQL"),
CTime::GetCurrentTime().Format(_T("BACKUP_%m_%d_%Y")),
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("SQL|*.SQL|ALL|*.*||"));
if (dlg.DoModal() != IDOK)
{
return;
}
DWORD dwBegin;
DWORD dwCount;
CString strMsg;
CString strConn;
CString strValue[8];// max field count is 7
CString strSQLDevice = _T("Select
DeviceID,DeviceName,X,Y,SymbolAngle,SymbolType From T_Device");
CString strSQLLock = _T("Select
DeviceID,LockNo,LockName,LockX,LockY,SymbolAngle,SymbolType From
T_Lock");
CString strSQLSymbolLabel = _T("Select
DeviceID,LabelX,LabelY,SymbolAngle,SymbolType From T_SymbolLabel");
strConn.Format(_T("DSN=%s;UID=%s;PWD=%s;"), m_strODBCName, m_strUser,
m_strPass);
try
{
CFile fSave(dlg.GetPathName(), CFile::modeCreate|CFile::modeWrite);
CDatabase db;
m_txtOutput.ReplaceSel(_T("Backup begin at ") +
CTime::GetCurrentTime().Format(_T("%H:%M:%S %b,%d,%Y")) + _T("\r\n"));
m_txtOutput.ReplaceSel(_T("File name: ") + dlg.GetPathName() + _T("\r\n"));
dwBegin = ::GetTickCount();
db.OpenEx(strConn, CDatabase::noOdbcDialog);
CRecordset rset(&db);
// T_Device table
m_txtOutput.ReplaceSel(_T("Select T_Device: ") + strSQLDevice + _T("\r\n"));
rset.Open(CRecordset::dynaset,strSQLDevice);
dwCount = 0;
while ( ! rset.IsEOF() )
{
for (short int nIndex = 0; nIndex < 6; ++nIndex)
{
rset.GetFieldValue(nIndex, strValue[nIndex]);
}
strMsg.Format(_T("UPDATE T_Device SET
DeviceName='%s',X='%s',Y='%s',SymbolAngle='%s',SymbolType='%s' WHERE
DeviceID='%s'\r\n"),
strValue[1],
strValue[2],
strValue[3],
strValue[4],
strValue[5],
strValue[0] );
fSave.Write(strMsg, strMsg.GetLength()*sizeof(TCHAR));
m_txtOutput.ReplaceSel(_T("^"));
rset.MoveNext();
if (++dwCount%100 == 0)
{
m_txtOutput.ReplaceSel(_T("\r\n"));
}
}
rset.Close();
// T_Lock table
m_txtOutput.ReplaceSel(_T("\r\nSelect T_Lock: ") + strSQLLock + _T("\r\n"));
rset.Open(CRecordset::dynaset, strSQLLock);
while ( ! rset.IsEOF() )
{
for (short int nIndex = 0; nIndex < 7; ++nIndex)
{
rset.GetFieldValue(nIndex, strValue[nIndex]);
}
strMsg.Format(_T("UPDATE T_Lock SET
LockName='%s',LockX='%s',LockY='%s',SymbolAngle='%s',SymbolType='%s'
WHERE DeviceID='%s' AND LockNo='%s'\r\n"),
strValue[2],
strValue[3],
strValue[4],
strValue[5],
strValue[6],
strValue[0],
strValue[1] );
fSave.Write(strMsg, strMsg.GetLength()*sizeof(TCHAR));
m_txtOutput.ReplaceSel(_T("^"));
rset.MoveNext();
if (++dwCount%100 == 0)
{
m_txtOutput.ReplaceSel(_T("\r\n"));
}
}
rset.Close();
// T_SymbolLabel table
m_txtOutput.ReplaceSel(_T("\r\nSelect T_SymbolLabel: ") +
strSQLSymbolLabel + _T("\r\n"));
rset.Open(CRecordset::dynaset, strSQLSymbolLabel);
while ( ! rset.IsEOF() )
{
for (short int nIndex = 0; nIndex < 5; ++nIndex)
{
rset.GetFieldValue(nIndex, strValue[nIndex]);
}
strMsg.Format(_T("UPDATE T_SymbolLabel SET
LabelX='%s',LabelY='%s',SymbolAngle='%s',SymbolType='%s' WHERE
DeviceID='%s'\r\n"),
strValue[1],
strValue[2],
strValue[3],
strValue[4],
strValue[0] );
fSave.Write(strMsg, strMsg.GetLength()*sizeof(TCHAR));
m_txtOutput.ReplaceSel(_T("^"));
rset.MoveNext();
if (++dwCount%100 == 0)
{
m_txtOutput.ReplaceSel(_T("\r\n"));
}
}
rset.Close();
db.Close();
fSave.Close();
m_txtOutput.ReplaceSel(_T("\r\nBackup end at ") +
CTime::GetCurrentTime().Format(_T("%H:%M:%S %b,%d,%Y")) + _T("\r\n"));
strMsg.Format(_T("%fs spent.\r\n%d records saved."),
(::GetTickCount()-dwBegin)/1000.0, dwCount);
m_txtOutput.ReplaceSel(strMsg);
}
catch (CDBException *pDE)
{
CString strErr;
pDE->GetErrorMessage(strErr.GetBufferSetLength(MAX_PATH), MAX_PATH);
strErr.ReleaseBuffer();
strErr += _T("\r\n");
m_txtOutput.ReplaceSel(_T("\r\n") + strErr);
pDE->Delete();
return;
}
catch (CFileException *pFE)
{
CString strErr;
pFE->GetErrorMessage(strErr.GetBufferSetLength(MAX_PATH), MAX_PATH);
strErr.ReleaseBuffer();
strErr += _T("\r\n");
m_txtOutput.ReplaceSel(_T("\r\n") + strErr);
pFE->Delete();
return;
}
catch (...)
{
m_txtOutput.ReplaceSel("\r\nUnknown error.\r\n");
return;
}
m_txtOutput.ReplaceSel("Success !\r\n");
Update:
UpdateData(TRUE);
if (m_strODBCName2.IsEmpty())
{
return;
}
DWORD dwBegin;
DWORD dwCount;
CString strMsg;
CString strConn;
CFileDialog dlg(TRUE);
if (dlg.DoModal() != IDOK)
{
return;
}
strConn.Format(_T("DSN=%s;UID=%s;PWD=%s;"), m_strODBCName2,
m_strUser2, m_strPass2);
try
{
CStdioFile fLoad(dlg.GetPathName(), CFile::modeRead);
CDatabase db;
m_txtOutput.ReplaceSel(_T("Restore begin at ") +
CTime::GetCurrentTime().Format(_T("%H:%M:%S %b,%d,%Y")) + _T("\r\n"));
m_txtOutput.ReplaceSel(_T("File name: ") + dlg.GetPathName() + _T("\r\n"));
dwBegin = ::GetTickCount();
db.OpenEx(strConn, CDatabase::noOdbcDialog);
dwCount = 0;
while ( fLoad.ReadString(strMsg) && ! strMsg.IsEmpty() )
{
try
{
db.ExecuteSQL(strMsg);
m_txtOutput.ReplaceSel(_T("*"));
if (++dwCount%100 == 0)
{
m_txtOutput.ReplaceSel(_T("\r\n"));
}
}
catch (...)
{
m_txtOutput.ReplaceSel(_T("\r\nError: ") + strMsg + _T("\r\n"));
}
}
db.Close();
fLoad.Close();
m_txtOutput.ReplaceSel(_T("\r\nRestore end at ") +
CTime::GetCurrentTime().Format(_T("%H:%M:%S %b,%d,%Y")) + _T("\r\n"));
strMsg.Format(_T("%fs spent.\r\n%d records loaded\r\n."),
(::GetTickCount()-dwBegin)/1000.0, dwCount);
m_txtOutput.ReplaceSel(strMsg);
}
catch (CDBException *pDE)
{
CString strErr;
pDE->GetErrorMessage(strErr.GetBufferSetLength(MAX_PATH), MAX_PATH);
strErr.ReleaseBuffer();
strErr += _T("\r\n");
m_txtOutput.ReplaceSel(_T("\r\n") + strErr);
pDE->Delete();
return;
}
catch (CFileException *pFE)
{
CString strErr;
pFE->GetErrorMessage(strErr.GetBufferSetLength(MAX_PATH), MAX_PATH);
strErr.ReleaseBuffer();
strErr += _T("\r\n");
m_txtOutput.ReplaceSel(_T("\r\n") + strErr);
pFE->Delete();
return;
}
catch (...)
{
m_txtOutput.ReplaceSel("\r\nUnknown error.\r\n");
return;
}
m_txtOutput.ReplaceSel("Success !\r\n");