procedure CreateNewDatabase(DatabaseFileName: string); var cat: OleVariant; begin if FileExists(DatabaseFileName) then begin if MessageBox(Application.Handle, PChar('Database ' + DatabaseFileName + ' has existed!' + #13#10 + 'Delete this database and create a new database ?'), 'Database Exists', MB_YESNO + MB_ICONWARNING + MB_DEFBUTTON2) = mrNo then exit; if not DeleteFile(DatabaseFileName) then begin MessageBox(Application.Handle, PChar('Cannot delete database ' + DatabaseFileName), 'Delete Database Error!', MB_OK + MB_ICONERROR); exit; end; end; cat := CreateOleObject('ADOX.Catalog'); cat.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DatabaseFileName); if ConnectDatabase(DatabaseFileName) then begin adoquryClass.Close; adoquryClass.SQL.Text := 'Create Table FaqClass ' + '(' + 'ClassID INT not null,' + 'ClassTitle char(100) not null,' + 'ParentClassID INT not null,' + 'ClassType TINYINT DEFAULT 0' + ')'; adoquryClass.ExecSQL; adoquryClass.Close; adoquryClass.SQL.Text := 'Create UNIQUE Index ClassIDIndex ON FaqClass (ClassID)'; adoquryClass.ExecSQL; adoquryClass.Close; adoquryClass.SQL.Text := 'Create Index ClassTitleIndex ON FaqClass (ClassTitle)'; adoquryClass.ExecSQL; adoquryClass.Close; adoquryClass.SQL.Text := 'Create Index ParentClassIDIndex ON FaqClass (ParentClassID)'; adoquryClass.ExecSQL; adoquryClass.Close; adoquryClass.SQL.Text := 'Create Index ClassTypeIndex ON FaqClass (ClassType)'; adoquryClass.ExecSQL; adoquryMemo.Close; adoquryMemo.SQL.Text := 'Create Table FaqMemo' + '(' + 'MemoID INT not null,' + 'ParentClassID INT not null,' + 'MemoTitle char(100) not null,' + 'MemoText TEXT,' + 'MemoPublic BIT DEFAULT 0,' + //缺省是不公开 'MemoLastModifyTime DATETIME' + ')'; adoquryMemo.ExecSQL; adoquryMemo.Close; adoquryMemo.SQL.Text := 'Create UNIQUE Index MemoIDIndex ON FaqMemo (MemoID)'; adoquryMemo.ExecSQL; adoquryMemo.Close; adoquryMemo.SQL.Text := 'Create Index ParentClassIDIndex ON FaqMemo (ParentClassID)'; adoquryMemo.ExecSQL; adoquryMemo.Close; adoquryMemo.SQL.Text := 'Create Index MemoTitleIndex ON FaqMemo (MemoTitle)'; adoquryMemo.ExecSQL; adoquryMemo.Close; adoquryMemo.SQL.Text := 'Create Index MemoPublicIndex ON FaqMemo (MemoPublic)'; adoquryMemo.ExecSQL; adoquryMemo.Close; adoquryMemo.SQL.Text := 'Create Index MemoLastModifyTimeIndex ON FaqMemo (MemoLastModifyTime)'; adoquryMemo.ExecSQL; end; end;
Delphi Tips
How to control Windows services unit cwinsrvc;interface function ServiceGetStrCode(nID : integer) : string;function ServiceGetStatus(sMachine, sService : string) : DWord;function ServiceRunning(sMachine, sService : string) : boolean;function ServiceStopped(sMachine, sService : string) : boolean;function ServiceStart(sMachine, sService : string) : boolean;function ServiceStop(sMachine, sService : string) : boolean; implementation uses Windows, SysUtils, WinSvc;// convert status codes returned by // ServiceGetStatus() to string values
function ServiceGetStrCode(nID : integer) : string; var s : string; begin case nID of SERVICE_STOPPED : s := 'STOPPED'; SERVICE_RUNNING : s := 'RUNNING'; SERVICE_PAUSED : s := 'PAUSED'; SERVICE_START_PENDING : s := 'START/PENDING'; SERVICE_STOP_PENDING : s := 'STOP/PENDING'; SERVICE_CONTINUE_PENDING : s := 'CONTINUE/PENDING'; SERVICE_PAUSE_PENDING : s := 'PAUSE/PENDING'; else s := 'UNKNOWN'; end; Result := s; end; // return status code if successful, -1 if not // return codes: // SERVICE_STOPPED // SERVICE_RUNNING // SERVICE_PAUSED
// following return codes are used to indicate that the // service is in the middle of getting to one // of the above states: // SERVICE_START_PENDING // SERVICE_STOP_PENDING // SERVICE_CONTINUE_PENDING // SERVICE_PAUSE_PENDING
function ServiceGetStatus(sMachine, sService : string) : DWord; var schm : SC_Handle; //service control manager handle schs : SC_Handle; // service handle ss : TServiceStatus; // service status dwStat : DWord; // current service status begin dwStat := -1; // connect to the service control manager schm := OpenSCManager( PChar(sMachine), Nil, SC_MANAGER_CONNECT); // if successful... if(schm > 0)then begin // open a handle to the specified service // we want to query service status schs := OpenService( schm, PChar(sService), SERVICE_QUERY_STATUS); // if successful... if(schs > 0)then begin // retrieve the current status //of the specified service if (QueryServiceStatus( schs, ss)) then begin dwStat := ss.dwCurrentState; end; // close service handle CloseServiceHandle(schs); end; // close service control manager handle CloseServiceHandle(schm); end; Result := dwStat; end; // Return TRUE if the specified service is running, // defined by the status code SERVICE_RUNNING. Return // FALSE if the service is in any other state, // including any pending states
function ServiceRunning(sMachine, sService : string) : boolean; begin Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService); end; // Return TRUE if the specified service was stopped, // defined by the status code SERVICE_STOPPED.
1.Project->Import Type Library...->在列表找"Microsoft ADO Ext.2.7 for DDL and Security(Version 2.7)"(注意:不要找到ADO去了,另外版本不一定是2.7,你用最新的就行)-> 别忙[install],先ClassNames:里改TTable->TADOXTTable/TColumn->TADOXColumn/TIndex->TADOXIndex,不然会冲突,然后就可以Install了 2.把下面作为一个Unit加入工程,就可以使用其他的三个常用函数喽! unit ADOXUnit;interfaceuses ADOX_TLB, comctrls, classes, ADOdb, Sysutils;type TTableR = array of array of string;const _Size = 5; _FieldName = 1; _FieldType = 2; _FieldLen = 3; _FieldNull = 4;var adoxCatalog: _Catalog; adoxTables: Tables; adoxTable: _Table; adoxColumns: Columns; adoxColumn: _Column;procedure GetDbInfo(Aconn: TAdoConnection; ATree: TTreeView); function GetADOXFieldType(aType: DataTypeEnum): string; procedure GetTableInfo(AConn: TAdoConnection; TableName: string; var TableInfo: TTableR);implementationprocedure GetTableInfo(AConn: TAdoConnection; TableName: string; var TableInfo: TTableR); var k, j: integer; temp: string; begin ADOXCatalog := coCatalog.Create; try ADOXCatalog.Set_ActiveConnection(AConn.connectionObject); ADOXTables := ADOXCatalog.Tables; for k := 0 to ADOXTables.Count - 1 do begin ADOXTable := ADOXTables.Item[k]; temp := ADOXTable.Name; if uppercase(temp) = Uppercase(TableName) then begin ADOXColumns := ADOXTable.Columns; SetLength(TableInfo, ADOXColumns.Count); for j := 0 to ADOXColumns.Count - 1 do begin SetLength(TableInfo[j], _Size); AdoXColumn := ADOXColumns[j]; TableInfo[j][_FieldName] := AdoXColumn.Name; //1 temp := GetADOXFieldType(AdoXColumn.Type_); Tableinfo[j][_FieldType] := copy(temp, 3, length(temp) - 2); //2 temp := inttostr(AdoXColumn.DefinedSize); Tableinfo[j][_FieldLen] := temp; //3 case AdoXColumn.Attributes of 0: temp := '不许为空'; 1: temp := '固定长度&不许为空'; 2: temp := ''; 3: temp := '固定长度'; end; Tableinfo[j][_FieldNull] := temp; end; end; end; finally ADOXCatalog := nil; end; end;procedure GetDbInfo(Aconn: TAdoConnection; ATree: TTreeView); var k, j: integer; ANode: TTreeNode; temp: string; TabList: TStringList; begin ADOXCatalog := coCatalog.Create; try adoxCatalog.set_ActiveConnection(AConn.ConnectionObject); with ATree.Items do begin Clear; TabList := TStringList.Create; try AConn.GetTableNames(TabList); ADOXTables := ADOXCatalog.Tables; for k := 0 to ADOXTables.Count - 1 do begin ADOXTable := ADOXTables.Item[k]; temp := ADOXTable.Name; if (TabList.IndexOf(temp) <> -1) then begin //if (temp = 'GTISFLOW') or (temp = 'GTISFLOWLINK') or (temp = 'GTISSHEET') or (temp = 'GTISDICT') or (temp = 'GTISLINK') then Continue; ANode := Add(nil, temp); ADOXColumns := ADOXTable.Columns; for j := 0 to ADOXColumns.Count - 1 do begin AdoXColumn := ADOXColumns[j]; temp := GetADOXFieldType(AdoXColumn.Type_); temp := copy(temp, 3, length(temp) - 2); if AdoXColumn.DefinedSize = 0 then temp := AdoXColumn.Name + '[' + temp + ']' else temp := AdoXColumn.Name + '[' + temp + '(' + inttostr(AdoXColumn.DefinedSize) + ')]'; case AdoXColumn.Attributes of 0: temp := temp + '不许为空'; 1: temp := temp + '固定长度&不许为空'; 2: ; 3: temp := temp + '固定长度'; end; AddChild(ANode, temp); end; end; end; finally TabList.Free; end; end; finally ADOXCatalog := nil; end; end;function GetADOXFieldType(aType: DataTypeEnum): string; begin case aType of // adEmpty: Result := 'adEmpty'; adTinyInt: Result := 'adTinyInt'; adSmallInt: Result := 'adSmallInt'; adInteger: Result := 'adInteger'; adBigInt: Result := 'adBigInt'; adUnsignedTinyInt: Result := 'adUnsignedTinyInt'; adUnsignedSmallInt: Result := 'adUnsignedSmallInt'; adUnsignedInt: Result := 'adUnsignedInt'; adUnsignedBigInt: Result := 'adUnsignedBigInt'; adSingle: Result := 'adSingle'; adDouble: Result := 'adDouble'; adCurrency: Result := 'adCurrency'; adDecimal: Result := 'adDecimal'; adNumeric: Result := 'adNumeric'; adBoolean: Result := 'adBoolean'; adError: Result := 'adError'; adUserDefined: Result := 'adUserDefined'; adVariant: Result := 'adVariant'; adIDispatch: Result := 'adIDispatch'; adIUnknown: Result := 'adIUnknown'; adGUID: Result := 'adGUID'; adDate: Result := 'adDate'; adDBDate: Result := 'adDBDate'; adDBTime: Result := 'adDBTime'; adDBTimeStamp: Result := 'adDBTimeStamp'; adBSTR: Result := 'adBSTR'; adChar: Result := 'adChar'; adVarChar: Result := 'adVarChar'; adLongVarChar: Result := 'adLongVarChar'; adWChar: Result := 'adWChar'; adVarWChar: Result := 'adVarWChar'; adLongVarWChar: Result := 'adLongVarWChar'; adBinary: Result := 'adBinary'; adVarBinary: Result := 'adVarBinary'; adLongVarBinary: Result := 'adLongVarBinary'; adChapter: Result := 'adChapter'; adFileTime: Result := 'adFileTime'; adPropVariant: Result := 'adPropVariant'; adVarNumeric: Result := 'adVarNumeric'; end; // case end;end.
var
cat: OleVariant;
begin
if FileExists(DatabaseFileName) then
begin
if MessageBox(Application.Handle, PChar('Database ' + DatabaseFileName + ' has existed!' + #13#10
+ 'Delete this database and create a new database ?'), 'Database Exists',
MB_YESNO + MB_ICONWARNING + MB_DEFBUTTON2) = mrNo then
exit;
if not DeleteFile(DatabaseFileName) then
begin
MessageBox(Application.Handle,
PChar('Cannot delete database ' + DatabaseFileName),
'Delete Database Error!', MB_OK + MB_ICONERROR);
exit;
end;
end;
cat := CreateOleObject('ADOX.Catalog');
cat.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DatabaseFileName);
if ConnectDatabase(DatabaseFileName) then
begin
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Table FaqClass ' +
'(' +
'ClassID INT not null,' +
'ClassTitle char(100) not null,' +
'ParentClassID INT not null,' +
'ClassType TINYINT DEFAULT 0' +
')';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create UNIQUE Index ClassIDIndex ON FaqClass (ClassID)';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Index ClassTitleIndex ON FaqClass (ClassTitle)';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Index ParentClassIDIndex ON FaqClass (ParentClassID)';
adoquryClass.ExecSQL;
adoquryClass.Close;
adoquryClass.SQL.Text := 'Create Index ClassTypeIndex ON FaqClass (ClassType)';
adoquryClass.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Table FaqMemo' +
'(' +
'MemoID INT not null,' +
'ParentClassID INT not null,' +
'MemoTitle char(100) not null,' +
'MemoText TEXT,' +
'MemoPublic BIT DEFAULT 0,' + //缺省是不公开
'MemoLastModifyTime DATETIME' +
')';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create UNIQUE Index MemoIDIndex ON FaqMemo (MemoID)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index ParentClassIDIndex ON FaqMemo (ParentClassID)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index MemoTitleIndex ON FaqMemo (MemoTitle)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index MemoPublicIndex ON FaqMemo (MemoPublic)';
adoquryMemo.ExecSQL;
adoquryMemo.Close;
adoquryMemo.SQL.Text := 'Create Index MemoLastModifyTimeIndex ON FaqMemo (MemoLastModifyTime)';
adoquryMemo.ExecSQL;
end;
end;
How to control Windows services
unit cwinsrvc;interface function ServiceGetStrCode(nID : integer) : string;function ServiceGetStatus(sMachine,
sService : string) : DWord;function ServiceRunning(sMachine,
sService : string) : boolean;function ServiceStopped(sMachine,
sService : string) : boolean;function ServiceStart(sMachine,
sService : string) : boolean;function ServiceStop(sMachine,
sService : string) : boolean;
implementation uses Windows, SysUtils, WinSvc;// convert status codes returned by
// ServiceGetStatus() to string values
function ServiceGetStrCode(nID : integer) : string;
var
s : string;
begin
case nID of
SERVICE_STOPPED : s := 'STOPPED';
SERVICE_RUNNING : s := 'RUNNING';
SERVICE_PAUSED : s := 'PAUSED';
SERVICE_START_PENDING : s := 'START/PENDING';
SERVICE_STOP_PENDING : s := 'STOP/PENDING';
SERVICE_CONTINUE_PENDING : s := 'CONTINUE/PENDING';
SERVICE_PAUSE_PENDING : s := 'PAUSE/PENDING';
else
s := 'UNKNOWN';
end;
Result := s;
end;
// return status code if successful, -1 if not
// return codes:
// SERVICE_STOPPED
// SERVICE_RUNNING
// SERVICE_PAUSED
// following return codes are used to indicate that the // service is in the middle of getting to one
// of the above states:
// SERVICE_START_PENDING
// SERVICE_STOP_PENDING
// SERVICE_CONTINUE_PENDING
// SERVICE_PAUSE_PENDING
// sMachine:
// machine name, ie: \\SERVER
// empty = local machine
// sService
// service name, ie: Alerter
function ServiceGetStatus(sMachine,
sService : string) : DWord;
var
schm : SC_Handle; //service control manager handle
schs : SC_Handle; // service handle
ss : TServiceStatus; // service status
dwStat : DWord; // current service status
begin
dwStat := -1;
// connect to the service control manager
schm := OpenSCManager( PChar(sMachine), Nil,
SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
// open a handle to the specified service
// we want to query service status
schs := OpenService( schm, PChar(sService),
SERVICE_QUERY_STATUS);
// if successful...
if(schs > 0)then
begin
// retrieve the current status
//of the specified service
if (QueryServiceStatus( schs, ss)) then
begin
dwStat := ss.dwCurrentState;
end;
// close service handle
CloseServiceHandle(schs);
end;
// close service control manager handle
CloseServiceHandle(schm);
end;
Result := dwStat;
end;
// Return TRUE if the specified service is running,
// defined by the status code SERVICE_RUNNING. Return
// FALSE if the service is in any other state,
// including any pending states
function ServiceRunning(sMachine,
sService : string) : boolean;
begin
Result := SERVICE_RUNNING =
ServiceGetStatus(sMachine, sService);
end;
// Return TRUE if the specified service was stopped,
// defined by the status code SERVICE_STOPPED.
别忙[install],先ClassNames:里改TTable->TADOXTTable/TColumn->TADOXColumn/TIndex->TADOXIndex,不然会冲突,然后就可以Install了
2.把下面作为一个Unit加入工程,就可以使用其他的三个常用函数喽!
unit ADOXUnit;interfaceuses ADOX_TLB, comctrls, classes, ADOdb, Sysutils;type
TTableR = array of array of string;const
_Size = 5;
_FieldName = 1;
_FieldType = 2;
_FieldLen = 3;
_FieldNull = 4;var
adoxCatalog: _Catalog;
adoxTables: Tables;
adoxTable: _Table;
adoxColumns: Columns;
adoxColumn: _Column;procedure GetDbInfo(Aconn: TAdoConnection; ATree: TTreeView);
function GetADOXFieldType(aType: DataTypeEnum): string;
procedure GetTableInfo(AConn: TAdoConnection; TableName: string; var TableInfo: TTableR);implementationprocedure GetTableInfo(AConn: TAdoConnection; TableName: string; var TableInfo: TTableR);
var
k, j: integer;
temp: string;
begin
ADOXCatalog := coCatalog.Create;
try
ADOXCatalog.Set_ActiveConnection(AConn.connectionObject);
ADOXTables := ADOXCatalog.Tables;
for k := 0 to ADOXTables.Count - 1 do
begin
ADOXTable := ADOXTables.Item[k];
temp := ADOXTable.Name;
if uppercase(temp) = Uppercase(TableName) then
begin
ADOXColumns := ADOXTable.Columns;
SetLength(TableInfo, ADOXColumns.Count);
for j := 0 to ADOXColumns.Count - 1 do
begin
SetLength(TableInfo[j], _Size);
AdoXColumn := ADOXColumns[j];
TableInfo[j][_FieldName] := AdoXColumn.Name; //1
temp := GetADOXFieldType(AdoXColumn.Type_);
Tableinfo[j][_FieldType] := copy(temp, 3, length(temp) - 2); //2
temp := inttostr(AdoXColumn.DefinedSize);
Tableinfo[j][_FieldLen] := temp; //3
case AdoXColumn.Attributes of
0: temp := '不许为空';
1: temp := '固定长度&不许为空';
2: temp := '';
3: temp := '固定长度';
end;
Tableinfo[j][_FieldNull] := temp;
end;
end;
end;
finally
ADOXCatalog := nil;
end;
end;procedure GetDbInfo(Aconn: TAdoConnection; ATree: TTreeView);
var
k, j: integer;
ANode: TTreeNode;
temp: string;
TabList: TStringList;
begin
ADOXCatalog := coCatalog.Create;
try
adoxCatalog.set_ActiveConnection(AConn.ConnectionObject);
with ATree.Items do
begin
Clear;
TabList := TStringList.Create;
try
AConn.GetTableNames(TabList);
ADOXTables := ADOXCatalog.Tables;
for k := 0 to ADOXTables.Count - 1 do
begin
ADOXTable := ADOXTables.Item[k];
temp := ADOXTable.Name;
if (TabList.IndexOf(temp) <> -1) then
begin
//if (temp = 'GTISFLOW') or (temp = 'GTISFLOWLINK') or (temp = 'GTISSHEET') or (temp = 'GTISDICT') or (temp = 'GTISLINK') then Continue;
ANode := Add(nil, temp);
ADOXColumns := ADOXTable.Columns;
for j := 0 to ADOXColumns.Count - 1 do
begin
AdoXColumn := ADOXColumns[j];
temp := GetADOXFieldType(AdoXColumn.Type_);
temp := copy(temp, 3, length(temp) - 2);
if AdoXColumn.DefinedSize = 0 then
temp := AdoXColumn.Name + '[' + temp + ']'
else
temp := AdoXColumn.Name + '[' + temp + '(' + inttostr(AdoXColumn.DefinedSize) + ')]';
case AdoXColumn.Attributes of
0: temp := temp + '不许为空';
1: temp := temp + '固定长度&不许为空';
2: ;
3: temp := temp + '固定长度';
end;
AddChild(ANode, temp);
end;
end;
end;
finally
TabList.Free;
end;
end;
finally
ADOXCatalog := nil;
end;
end;function GetADOXFieldType(aType: DataTypeEnum): string;
begin
case aType of //
adEmpty: Result := 'adEmpty';
adTinyInt: Result := 'adTinyInt';
adSmallInt: Result := 'adSmallInt';
adInteger: Result := 'adInteger';
adBigInt: Result := 'adBigInt';
adUnsignedTinyInt: Result := 'adUnsignedTinyInt';
adUnsignedSmallInt: Result := 'adUnsignedSmallInt';
adUnsignedInt: Result := 'adUnsignedInt';
adUnsignedBigInt: Result := 'adUnsignedBigInt';
adSingle: Result := 'adSingle';
adDouble: Result := 'adDouble';
adCurrency: Result := 'adCurrency';
adDecimal: Result := 'adDecimal';
adNumeric: Result := 'adNumeric';
adBoolean: Result := 'adBoolean';
adError: Result := 'adError';
adUserDefined: Result := 'adUserDefined';
adVariant: Result := 'adVariant';
adIDispatch: Result := 'adIDispatch';
adIUnknown: Result := 'adIUnknown';
adGUID: Result := 'adGUID';
adDate: Result := 'adDate';
adDBDate: Result := 'adDBDate';
adDBTime: Result := 'adDBTime';
adDBTimeStamp: Result := 'adDBTimeStamp';
adBSTR: Result := 'adBSTR';
adChar: Result := 'adChar';
adVarChar: Result := 'adVarChar';
adLongVarChar: Result := 'adLongVarChar';
adWChar: Result := 'adWChar';
adVarWChar: Result := 'adVarWChar';
adLongVarWChar: Result := 'adLongVarWChar';
adBinary: Result := 'adBinary';
adVarBinary: Result := 'adVarBinary';
adLongVarBinary: Result := 'adLongVarBinary';
adChapter: Result := 'adChapter';
adFileTime: Result := 'adFileTime';
adPropVariant: Result := 'adPropVariant';
adVarNumeric: Result := 'adVarNumeric';
end; // case
end;end.