Author: Mike Heydon
Functions to load a StringList with MS-SQL Server Error Logs via SQL-DMO. MS-SQL
DMO is a COM/OLE object that can do many things, in this article we just read the
error logs off the server.
There a two overloaded functions, one for Windows Authentication, and another for
SQL Authentication. The function returns true if successful. The default log number
is 0 (Current Log).
1 // Windows Authentication
2
3 function SqlErrorLog(AStrings: TStrings;
4 const ASqlServer: string;
5 ALogNumber: integer = 0): boolean; overload;
6
7 // SQL Authentication
8 function SqlErrorLog(AStrings: TStrings;
9 const ASqlServer, AUserName, APassword: string;
10 ALogNumber: integer = 0): boolean; overload;
11
12 //Example
13
14 // Load memo using Default Log 0 and Windows Authentication
15 if SqlErrorLog(Memo1.Lines, 'BusServer1') then
16 .....
17
18 // Load memo using Log 3 and SQL Authentication
19 if SqlErrorLog(Memo1.Lines, 'BusServer', 'harry', 'mypass', 3) then
20 ..
21
22 //Answer:
23
24 uses ComObj, Variants; {Variants is for Delphi 7}
25
26 // =====================================================
27 // PRIMITIVE Load MS SQL Server Error Log Function
28 // =====================================================
29
30 function _SqlErrorLog(AObject: OleVariant;
31 AStrings: TStrings;
32 const ASqlServer: string;
33 ALogNumber: integer): boolean;
34 var
35 oLog: OleVariant;
36 bResult: boolean;
37 i: integer;
38 begin
39 try
40 AObject.Connect(ASqlServer);
41
42 try
43 AStrings.BeginUpdate;
44 oLog := AObject.ReadErrorLog(ALogNumber);
45 for i := 1 to oLog.Rows do
46 AStrings.Add(oLog.GetColumnString(i, 1));
47 oLog := Unassigned;
48 finally
49 AStrings.EndUpdate;
50 end;
51
52 AObject.Disconnect;
53 bResult := true;
54 except
55 bResult := false;
56 end;
57
58 AObject := Unassigned;
59 Result := bResult;
60 end;
61
62 // =====================================================
63 // Get SQL Server Log using Windows Authentication
64 // =====================================================
65
66 function SqlErrorLog(AStrings: TStrings;
67 const ASqlServer: string;
68 ALogNumber: integer = 0): boolean; overload;
69 var
70 oDMO: OleVariant;
71 bResult: boolean;
72 begin
73 AStrings.Clear;
74
75 try
76 oDMO := CreateOleObject('SQLDMO.SQLServer');
77 oDMO.LoginSecure := true;
78 bResult := _SqlErrorLog(oDMO, AStrings, ASqlServer, ALogNumber);
79 except
80 bResult := false;
81 end;
82
83 Result := bResult;
84 end;
85
86 // =====================================================
87 // Get SQL Server Log using SQL Authentication
88 // =====================================================
89
90 function SqlErrorLog(AStrings: TStrings;
91 const ASqlServer, AUserName, APassword: string;
92 ALogNumber: integer = 0): boolean; overload;
93 var
94 oDMO: OleVariant;
95 bResult: boolean;
96 begin
97 AStrings.Clear;
98
99 try
100 oDMO := CreateOleObject('SQLDMO.SQLServer');
101 oDMO.LoginSecure := false;
102 oDMO.Login := AUserName;
103 oDMO.Password := APassword;
104 bResult := _SqlErrorLog(oDMO, AStrings, ASqlServer, ALogNumber);
105 except
106 bResult := false;
107 end;
108
109 Result := bResult;
110 end;
|