Ik ben aan het werken aan een dynamische site die werkt door middel van CGI applicaties. In een van de applicaties word een database uitgelezen. Ik had naar een tijdje werken de volgende (werkende) code.
Maar over het volgende gedeelte was ik niet tevrede:
Zoals je ziet is daar geen enkele vorm van foutcontrole. Dit heb ik opgelost (gedeeltelijk, niet naar mijn voldoening) met andere code. Eerst werkte deze niet:
Dez gaf op de regels met GetFieldValue(1, cstrValue) aanroepen de volgende foutmeldingen: "error C2668: 'GetFieldValue' : ambiguous call to overloaded function"
Dit heb ik opgelost door de query aan te passen naar ""SELECT * FROM _HTMLVariables WHERE page = 'loginscreen' ORDER BY stringname"", en daarna de nullen te vervangen door eenen en de eenen doo tweeen.
Zoals je ziet lost deze werkende code niet alle foute op en is hij uiterst lang, iets dat naar mijn idee beter en korter kan (en overzichterlijker). Ook gaf deze nieeuwe code ineens nieuwe warnings op de catch regels: "warning C4101: 'e' : unreferenced local variable Linking...".
De code geeft nu wel correcte uitvoer en ik weet dat er nog foutcontroles ingebouwd moeten worden in het uitleesgedeelte, maar ik wou eerst de code inkrimpen. Alhoewell ik geen idee heb hoe.
Hier nog even de database:
Ik gebruik Microsoft Visual C++ 6.0
Voor de duidelijkheid:
Heeft iemand een idee of een steundje in de goede richting betreffende het kleiner (en veiliger) maken van de code waarin de Recordsetvelden worden uitgelezen?
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
| // Logon.cpp: Geeft het Logonscherm weer #include <string> #include <iostream> #include <afxdb.h> using namespace std; int main() { // txt/HTML-Header verzenden cout << "Content-type: text/html\n\n"; // CDatabaseobject aanmaken CDatabase dbForum; // Probeer met het databestand te verbinden try { // Verbind het object met een read-only databron // Zorg dat het ODBC-verbinding dialoogvenster NOOIT verschijnt dbForum.OpenEx( _T( "DSN=dbForum" ), CDatabase::openReadOnly | CDatabase::noOdbcDialog ); } catch(CException* e) { cout << "<html><head><title>Fout</title></head><body>" << "Fout tijdens het verkijgen tot toegang tot de database</body></html>"; return 0; } // CRecordsetobject aanmaken CRecordset rsForum(&dbForum); // Probeer gegevens te halen uit het databestand try { // Open rsForum en voer de SQL-opdracht uit, // Statishe verbinding, rsForum.Open( CRecordset::snapshot, _T("SELECT stringname, content FROM _HTMLVariables WHERE page = 'loginscreen' ORDER BY stringname"), CRecordset::readOnly | CRecordset::useBookmarks | CRecordset::noDirtyFieldCheck ); } catch(CException* e) { cout << "<html><head><title>Fout</title></head>" << "<body>Fout tijdens het verkrijgen van informatie uit de database</body></html>"; return 0; } // Declareren van de variabellen, die daarna door databasewaarden gevuld worden string strHTMLTitle, strHTMLcmdSubmit, strHTMLcmdRegister, strHTMLcmdCancel, strHTMLsrcJavaScript, strHTMLMainMessage, strHTMLUser, strHTMLPass, strHTMLsrcStyle, strHTMLcgiLogin, strHTMLcgiRegister ; // Voor de tijdelijke opslag voor veldwaarde CString cstrValue; string strValue; rsForum.GetFieldValue(1, cstrValue); strHTMLcgiLogin = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcgiRegister = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcmdCancel = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcmdRegister = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcmdSubmit = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLMainMessage = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLPass = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLsrcJavaScript = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLsrcStyle = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLTitle = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLUser = LPCTSTR(cstrValue); // Set van records en connectie naar Database sluiten rsForum.Close( ); dbForum.Close( ); // De pagina zelf cout << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"" << "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" << "\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n" << "<head>\n\t<meta content=\"text/html; charset=iso-8859-1\" http-equiv=\"Content-Type\" />\n\t" << "<title>" << strHTMLTitle << "</title>\n"; // Geen javascripttags als er geen javascript gebruikt word if (strHTMLsrcJavaScript != "none") { cout << "\n\t<script languague=\"javascript\" src=\"" << strHTMLsrcJavaScript << "\"></script>"; } // vervolg pagina cout << "\n\t<link rel=\"stylesheet\" href=\"" << strHTMLsrcStyle << "\"" << "type=\"text/css\" />\n</head>\n\n" << "<body>\n\t<div id=\"main\">\n\t\t<div class=\"title\">" << strHTMLMainMessage << "</div>\n" << "\t\t<form method=\"post\" action=\"" << strHTMLcgiLogin << "\">\n" << "\t\t\t<table border=\"0\" width=\"100%\">\n" << "\t\t\t\t<tr><td>" << strHTMLUser << "</td><td><input type=\"text\" name=\"cgiLoginName\" maxlength=\"50\" /></td></tr>\n" << "\t\t\t\t<tr><td>" << strHTMLPass << "</td><td><input type=\"password\" name=\"cgiLoginPassWord\" maxlength=\"50\" /></td></tr>\n" << "\t\t\t</table>\n" << "\t\t\t<div class=\"buttons\">\n" << "\t\t\t\t<input type=\"submit\" value=\"" << strHTMLcmdSubmit << "\" name=\"cmdSubmit\" />\n" << "\t\t\t\t<input type=\"reset\" value=\"" << strHTMLcmdCancel << "\" name=\"cmdCancel\" />\n" << "\t\t\t\t<input type=\"button\" value=\"" << strHTMLcmdRegister << "\" name=\"cmdRegister\" />\n" << "\t\t\t</div>\n" << "\t\t</form>\n" << "\t</div>\n</body>\n\n</html>"; return 0; } |
Maar over het volgende gedeelte was ik niet tevrede:
C++:
1
2
3
4
5
6
7
8
9
10
11
| rsForum.GetFieldValue(1, cstrValue); strHTMLcgiLogin = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcgiRegister = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcmdCancel = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcmdRegister = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLcmdSubmit = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLMainMessage = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLPass = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLsrcJavaScript = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLsrcStyle = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLTitle = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(1, cstrValue); strHTMLUser = LPCTSTR(cstrValue); |
Zoals je ziet is daar geen enkele vorm van foutcontrole. Dit heb ik opgelost (gedeeltelijk, niet naar mijn voldoening) met andere code. Eerst werkte deze niet:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
| rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); if (strValue == "strHTMLcgiLogin") { rsForum.GetFieldValue(1, cstrValue); strHTMLcgiLogin = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLcgiRegister") { rsForum.GetFieldValue(1, cstrValue); strHTMLcgiRegister = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLcmdCancel") { rsForum.GetFieldValue(1, cstrValue); strHTMLcmdCancel = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLcmdRegister") { rsForum.GetFieldValue(1, cstrValue); strHTMLcmdRegister = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLcmdSubmit") { rsForum.GetFieldValue(1, cstrValue); strHTMLcmdSubmit = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLMainMessage") { rsForum.GetFieldValue(1, cstrValue); strHTMLMainMessage = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLPass") { rsForum.GetFieldValue(1, cstrValue); strHTMLPass = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLsrcJavaScript") { rsForum.GetFieldValue(1, cstrValue); strHTMLsrcJavaScript = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLsrcStyle") { rsForum.GetFieldValue(1, cstrValue); strHTMLsrcStyle = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLTitle") { rsForum.GetFieldValue(1, cstrValue); strHTMLTitle = LPCTSTR(cstrValue); rsForum.MoveNext( ); rsForum.GetFieldValue(0, cstrValue); strValue = LPCTSTR(cstrValue); } if (strValue == "strHTMLUser") { rsForum.GetFieldValue(1, cstrValue); strHTMLUser = LPCTSTR(cstrValue); } |
Dez gaf op de regels met GetFieldValue(1, cstrValue) aanroepen de volgende foutmeldingen: "error C2668: 'GetFieldValue' : ambiguous call to overloaded function"
Dit heb ik opgelost door de query aan te passen naar ""SELECT * FROM _HTMLVariables WHERE page = 'loginscreen' ORDER BY stringname"", en daarna de nullen te vervangen door eenen en de eenen doo tweeen.
Zoals je ziet lost deze werkende code niet alle foute op en is hij uiterst lang, iets dat naar mijn idee beter en korter kan (en overzichterlijker). Ook gaf deze nieeuwe code ineens nieuwe warnings op de catch regels: "warning C4101: 'e' : unreferenced local variable Linking...".
De code geeft nu wel correcte uitvoer en ik weet dat er nog foutcontroles ingebouwd moeten worden in het uitleesgedeelte, maar ik wou eerst de code inkrimpen. Alhoewell ik geen idee heb hoe.
Hier nog even de database:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
| page |stringname |content ---------------------------------------------------------------------------- loginscreen | strHTMLTitle | :: C++ Forum - Login :: loginscreen | strHTMLcmdSubmit | Aanmelden loginscreen | strHTMLcmdRegister | Registreren loginscreen | strHTMLcmdCancel | Wissen loginscreen | strHTMLsrcJavaScript | none loginscreen | strHTMLMainMessage | Welkom op het C++ Forum loginscreen | strHTMLUser | Gebruikersnaam loginscreen | strHTMLPass | Wachtwoord loginscreen | strHTMLsrcStyle | style\loginscreen.css loginscreen | strHTMLcgiLogin | login.exe loginscreen | strHTMLcgiRegister | register.exe |
Ik gebruik Microsoft Visual C++ 6.0
Voor de duidelijkheid:
Heeft iemand een idee of een steundje in de goede richting betreffende het kleiner (en veiliger) maken van de code waarin de Recordsetvelden worden uitgelezen?
[ Voor 78% gewijzigd door Verwijderd op 23-02-2004 22:52 . Reden: Anti-opmaak vernageling ]