这是我的程序中的一段代码供参考:procedure TClientData.AlterUsersColumn; var i: Integer; s: String; begin if UseCDS.FieldCount > StartFieldNum then begin // 删除用户库的所有权限字段 s := ''; for i := StartFieldNum to UseCDS.FieldCount - 1 do s := s + UseCDS.Fields[i].FieldName + ','; Delete(s, Length(s), 1); UseCDS.Active := False; ExecuteSQL('ALTER TABLE JXC_USERS DROP COLUMN ' + s); end else UseCDS.Active := False; // 用主窗口菜单中设置了功能权限的项(Tag<>0)重新定义用户权限字段 s := ''; with MainForm.ActionManager1 do for i := 0 to ActionCount - 1 do if Actions[i].Tag <> 0 then s := s + Actions[i].Name + ' char(1),'; if s = '' then Exit; Delete(s, Length(s), 1); ExecuteSQL('ALTER TABLE JXC_USERS ADD ' + s); // 初始化用户权限 UsersCDS.Active := True; try while not UsersCDS.Eof do begin UsersCDS.Edit; for i := StartFieldNum to UsersCDS.FieldCount - 1 do UsersCDS.Fields[i].Value := UsersCDS.FieldByName('Admin').Value; UsersCDS.Next; end; UsersCDS.ApplyUpdates(-1); finally UsersCDS.Active := False; end; end;
补充:ExecuteSQL方法procedure TClientData.ExecuteSQL(SQL: String); begin ComCDS.CommandText := SQL; ComCDS.Execute; end;
var
i: Integer;
s: String;
begin
if UseCDS.FieldCount > StartFieldNum then
begin
// 删除用户库的所有权限字段
s := '';
for i := StartFieldNum to UseCDS.FieldCount - 1 do
s := s + UseCDS.Fields[i].FieldName + ',';
Delete(s, Length(s), 1);
UseCDS.Active := False;
ExecuteSQL('ALTER TABLE JXC_USERS DROP COLUMN ' + s);
end else UseCDS.Active := False;
// 用主窗口菜单中设置了功能权限的项(Tag<>0)重新定义用户权限字段
s := '';
with MainForm.ActionManager1 do
for i := 0 to ActionCount - 1 do
if Actions[i].Tag <> 0 then
s := s + Actions[i].Name + ' char(1),';
if s = '' then Exit;
Delete(s, Length(s), 1);
ExecuteSQL('ALTER TABLE JXC_USERS ADD ' + s);
// 初始化用户权限
UsersCDS.Active := True;
try
while not UsersCDS.Eof do
begin
UsersCDS.Edit;
for i := StartFieldNum to UsersCDS.FieldCount - 1 do
UsersCDS.Fields[i].Value := UsersCDS.FieldByName('Admin').Value;
UsersCDS.Next;
end;
UsersCDS.ApplyUpdates(-1);
finally
UsersCDS.Active := False;
end;
end;
begin
ComCDS.CommandText := SQL;
ComCDS.Execute;
end;