Показать сообщение отдельно
  #4  
Старый 01.06.2010, 11:49
Аватар для Страдалецъ
Страдалецъ Страдалецъ вне форума
Гуру
 
Регистрация: 09.03.2009
Адрес: На курорте, из окна вижу теплое Баренцево море. Бррр.
Сообщения: 4,723
Репутация: 52347
По умолчанию

Вот держите, наваял конвертер. Должно вроде работать:
Код:
function FormatSQL(S: String): String;
begin
 Result := StringReplace(S, #13#10,'',[rfReplaceAll]);
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
  i,j: Integer;
  Q: TAdoQuery;
  DS: TClientDataset;
begin
 Q := TAdoQuery.Create(nil);
 Q.Connection := AdoConnection1;
 //Создание таблиц
 for i := 0 to ComponentCount - 1
 do if Components[i].ClassType = TClientDataset
    then begin
         DS := TClientDataset(Components[i]);
         Q.SQL.Text := FormatSQL(Format('create table %s (',[Copy(DS.FileName,1,Length(DS.FileName)-4)]));
         for j := 0 to DS.FieldCount - 1
         do begin
            Q.SQL.Text := FormatSQL(Q.SQL.Text + DS.Fields[j].FieldName);
            case DS.Fields[j].DataType
            of ftString: Q.SQL.Text := Format('%s varchar(%d)',[Q.SQL.Text,DS.Fields[j].Size]);
               ftAutoInc: Q.SQL.Text := Format('%s int primary key',[Q.SQL.Text]);
               ftInteger: Q.SQL.Text := Format('%s int',[Q.SQL.Text]);
               ftBoolean: Q.SQL.Text := Format('%s bit',[Q.SQL.Text]);
               ftFloat, ftCurrency: Q.SQL.Text := Format('%s float',[Q.SQL.Text]);
               ftDate: Q.SQL.Text := Format('%s datetime',[Q.SQL.Text]);
               end;
            if j < DS.FieldCount - 1
            then Q.SQL.Text := FormatSQL(Q.SQL.Text + ',');
            end;
         Q.SQL.Text := FormatSQL(Q.SQL.Text + ');');
         Memo1.Lines.Add(Q.SQL.Text);
         Q.ExecSQL;
         end;
 // Заполнение данными
 for i := 0 to ComponentCount - 1
 do if Components[i].ClassType = TClientDataset
    then begin
         DS := TClientDataset(Components[i]);
         while not DS.Eof
         do begin
            Q.SQL.Text := FormatSQL(Format('insert into %s (',[Copy(DS.FileName,1,Length(DS.FileName)-4)]));
            for j := 0 to DS.FieldCount - 1
            do begin
               Q.SQL.Text := FormatSQL(Q.SQL.Text + DS.Fields[j].FieldName);
               if j < DS.FieldCount - 1
               then Q.SQL.Text := FormatSQL(Q.SQL.Text + ',');
               end;
            Q.SQL.Text := FormatSQL(Q.SQL.Text + ') values(');

            for j := 0 to DS.FieldCount - 1
            do begin
               if DS.Fields[j].IsNull
               then Q.SQL.Text := FormatSQL(Q.SQL.Text + 'null')
               else begin
                    if DS.Fields[j].DataType in [ftString,ftDate]
                    then Q.SQL.Text := FormatSQL(Q.SQL.Text + ''''+DS.Fields[j].AsString+'''')
                    else Q.SQL.Text := FormatSQL(Q.SQL.Text + DS.Fields[j].AsString);
                    end;
               if j < DS.FieldCount - 1
               then Q.SQL.Text := FormatSQL(Q.SQL.Text + ',');
               end;
            Q.SQL.Text := FormatSQL(Q.SQL.Text + ')');
            Q.ExecSQL;
            Memo1.Lines.Add(Q.SQL.Text);
            DS.Next;
            end;
         end;

 Q.Free;
end;
__________________
Жизнь такова какова она есть и больше никакова.
Помогаю за спасибо.
Ответить с цитированием