![]() |
|
|
|||||||
| Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
![]() |
|
|
Опции темы | Поиск в этой теме | Опции просмотра |
|
|
|
#1
|
|||
|
|||
|
Как создать таблицу с такой же структурой как в ClientDataSet?
PS: Работал с данными, которые хранятся в файлах, теперь нужно перенести в MSSQL. |
|
#2
|
|||
|
|||
|
Самый простой и понятный вариант открываем Enterprise Menager (в комплекте с MSSQL идет) выбираем базу правую кнопку создать таблицу, и пишем структуру ручками.
|
|
#3
|
||||
|
||||
|
Ну это не наш метод.
Я вижу еще два варианта: 1. Если у вас данные на диске хранятся как XML-файл, то возможен импорт средствами самого MSSQL. 2. Если данные хранятся в ином виде, то можно написать конвертер. |
|
#4
|
||||
|
||||
|
Вот держите, наваял конвертер. Должно вроде работать:
Код:
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;Последний раз редактировалось Страдалецъ, 01.06.2010 в 16:18. |
|
#5
|
|||
|
|||
|
Спасибо, буду смотреть.
Ручками не получиться там много таблиц будет. |