|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThat article present a way to build HTML pages in ISAPI using OLE DB database access. The sample database is an Access database.FunctionalityWhen you open the page in a client browser (WriteLayout.dll in the URL), the default page is opened: Here are 2 links, WritePageDB2Web and WritePageDemo. Both links load the same HTML page. The first link load the page using dynamic data from an access database and piece of data from HTML resource. The second link load the page using a HTML string resource. The advantages are clear: the entire HTML page will be fastest loaded because the only dynamic data will be coming from the Database using the CAdoDatabase and CAdoRecordset classes. Large HTML parts of the page are hard coded in resource strings and will be loaded only on the first call. Developing the pieces of HTML code inside of Visual Studio project are very easy. The HTML string resources from a C++ project will be linked exactly to the correspondent external files. If you will work with Macromedia or InterDev on that's HTML's files, just recompile the C++ project and the changes will be updated ! To link the external HTML files to HTML resources strings follow these steps:
You now have the HTML file in your C++ project! To see it just double click the imported resource Using the HTML resources in your project
BOOL CWriteLayoutExtension::LoadLongResource(CString& str, UINT nID)
{
HRSRC hRes;
HINSTANCE hInst = AfxGetResourceHandle();
BOOL bResult = FALSE;
//if you want standard html type
hRes = FindResource(hInst, MAKEINTRESOURCE(nID), RT_HTML);
if (hRes == NULL)
ISAPITRACE1("Error: Resource %d could not be found\r\n", nID);
else
{
DWORD dwSize = SizeofResource(hInst, hRes);
if (dwSize == 0)
{ str.Empty();
bResult = TRUE;
}
else
{
LPTSTR pszStorage = str.GetBufferSetLength(dwSize);
HGLOBAL hGlob = LoadResource(hInst, hRes);
if (hGlob != NULL)
{
LPVOID lpData = LockResource(hGlob);
if (lpData != NULL)
{
memcpy(pszStorage, lpData, dwSize);
bResult = TRUE;
}
FreeResource(hGlob);
}
}
}
return bResult;
}
CodingThe
The hard coded strings are loaded from resources using the // first piece of hardcoded page LoadLongResource(strPagePart1, IDR_HTML_PAGE_PART1);The dynamic HTML string parts are loaded from the database using customized functions. //The output page will be: *pCtxt << strPagePart1 + GetArticlesFeature + // some HTML built from DB strPagePart2 + GetArticlesTopRated + //some HTML built from DB strPagePart3 + GetArticlesLast10 + //some HTML built from DB strPagePart4. // (will have 3 fixed parts and 2 parts dynamically, from DB)Outputting the mix of hardcoded and dynamic HTML to the page is done as follows: //1, fixed part: *pCtxt << strPagePart1; // output hardcoded HTML part 1 //2, dynamic part: // GetArticlesFeature if ( ! GetArticlesFeature(AdoDB, &bstrOutput, &bstrError) ) { *pCtxt << bstrError; // if its an error display it return; } *pCtxt << bstrOutput; // write part 1 The dynamic data is built in private functions using the or a file connection string (double click the udl file to link it to access DB): The database is accessed very easily: //the sql stmt string _bstr_t bstrSTMT(L"SELECT * from tblArticles where type=1"); MACRO_BEGIN //here the begin code from macro _bstr_t bstrValue_link, bstrValue_title, bstrValue_author, bstrValue_headline; while ( !AdoRS->IsEof() ) //if we have records { AdoRS->GetFieldValue("link", &bstrValue_link ); AdoRS->GetFieldValue("title", &bstrValue_title ); AdoRS->GetFieldValue("author", &bstrValue_author ); AdoRS->GetFieldValue("headline",&bstrValue_headline ); wsprintf(wcOut, "TR"\ "TD width=100%% FONT class=links size=1 A href=%s %s/A BR "\ "by B%s/B BR"\ "FONT color=black%s/FONT"\ "/TD"\ "/TR", (LPCTSTR)bstrValue_link, (LPCTSTR)bstrValue_title, (LPCTSTR)bstrValue_author, (LPCTSTR)bstrValue_headline ); AdoRS->MoveNext(); *bstrOutput = (LPCTSTR)wcOut; //the output here } MACRO_END //here the end code from macro To Install
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||