Db数据库怎样用程序导入到Access数据库(100分)

  • 主题发起人 主题发起人 82625741
  • 开始时间 开始时间
8

82625741

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:我想把.db的数据库导入到Access数据库中,不是用导入导出命令,而是用程序
实现,不过Access中没有表,必须要建立同名的数据库表,请问用什么办法实现。我用过
insert into ... select ..from ...但是不行,不能用在不同类型的数据库中,能否给出
一点实例,谢谢!
 
procedure TFormMain.ChangeDataClick(Sender: TObject);
var
FileName: String;
ConStr: WideString;
SqlStr: String;
i: Integer;
BB: Boolean;
upDate: TDateTime;
//StringList: TStringList;
begin
Screen.Cursor := crHourGlass;
BB := False;
if OpenDialog1.Execute then
begin
FileName := OpenDialog1.FileName;
if length(ExtractFileName(FileName)) = 11 then
begin
upDate := FileDateToDateTime(FileAge(FileName));
ConStr := 'Provider=Microsoft.Jet.OLEDB.4.0;';
ConStr := ConStr + 'Data Source=' + FileName + ';';
ConStr := ConStr + 'Persist Security Info=False';
ADOConnection1.Close;
ADOConnection1.ConnectionString := ConStr;
ADOConnection1.Open;
SqlStr := 'SELECT sTime, sDate, sWho FROM ADB WHERE (sWhere = ''电子门2,门外'') ORDER BY sDate, sWho';
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(SqlStr);
ADOQuery1.Open;
//StringList := TStringList.Create;
//StringList.LoadFromFile(SetupFile);
SQLUser := 'cwa_login';
SQLPass := 'cwa';
SQLIP := '192.168.161.201';
//SQLIP := StringList[0];
//SQLUser := StringList[1];
//SQLPass := StringList[2];
//StringList.Free;
ConStr := 'Provider=SQLOLEDB.1;';
ConStr := ConStr + 'Password=' + SQLPass + ';';
ConStr := ConStr + 'Persist Security Info=True;';
ConStr := ConStr + 'User ID=' + SQLUser + ';';
ConStr := ConStr + 'Data Source=' + SQLIP;
ADOConnection2.Close;
ADOConnection2.ConnectionString := ConStr;
ADOConnection2.Open;
{for i := 0 to ADOTable1.RecordCount - 1 do
begin
if ADOTable1.FieldByName('sDate').AsString = ADOQuery1.FieldByName('sDate').AsString then
BB := True;
ADOTable1.Next;
end;}
{ADOTable2.Open;
if ADOTable2.IsEmpty = False then
begin
for i := 0 to ADOTable2.RecordCount - 1 do
begin
if ADOTable2.FieldByName('date').AsString = ADOQuery1.FieldByName('sDate').AsString then
BB := True;
ADOTable1.Next;
end;
end;}
ADOTable2.Open;
if ADOTable2.IsEmpty = False then
begin
for i := 0 to ADOTable2.RecordCount - 1 do
begin
if ADOTable2.FieldByName('file').AsString = ExtractFileName(FileName) then
BB := True;
ADOTable2.Next;
end;
end;

if BB = True then
ShowMessage('数据已加入')
else
begin
if ADOQuery1.RecordCount > 0 then
begin
ADOTable1.Open;
for i := 0 to ADOQuery1.RecordCount - 1 do
begin
ADOTable1.Append;
ADOTable1.FieldByName('sDate').AsString :=
ADOQuery1.FieldByName('sDate').AsString;
ADOTable1.FieldByName('sTime').AsString :=
ADOQuery1.FieldByName('sTime').AsString;
ADOTable1.FieldByName('sWho').AsString :=
ADOQuery1.FieldByName('sWho').AsString;
ADOQuery1.Next;
end;
ADOTable1.Post;
ADOTable2.Append;
ADOTable2.FieldByName('file').AsString := ExtractFileName(FileName);
ADOTable2.FieldByName('upDate').AsDateTime := upDate;
ADOTable2.Post;
end;
ShowMessage('数据加入成功');
end;
ADOQuery1.Close;
ADOTable2.Close;
end
else
ShowMessage('文件选择错误');
ADOTable1.Close;
ADOConnection1.Close;
end;
Screen.Cursor := crDefault;
end;
 
TO:老人家
上面一段代码并没有说到怎么建立表呀,并且所有的数据结构并不是已知的,你要动态去
取得,并且你取得DB文件名之后并没有打开过该文件,只是在
ADOTable2.FieldByName('file').AsString := ExtractFileName(FileName);用到而已,能
否更详细说明一下,谢谢了!如果问题解决,分一定给你!
 
怎么没人回答吗?
 
如果用bde的batchmove组件就可以
procedure TTableTranW.Button1Click(Sender: TObject);
Var
i:Integer;
s:String;
Name:String;
begin
Table1.Close;
Table1.TableName:=SrcFile.Text+'.DBF';
Table1.Open;
Name:=DstFile.Text;
List.Lines.Clear;
List.Lines.Add('Create Table '+Name);
List.Lines.Add('(');
With Table1 Do
Begin
For i:=0 to FieldCount-1 Do
Begin
S:='';
s:=S+Fields.Fieldname;
If length(S)<16 Then
S:=S+Space(16-length(S));
case (Fields.dataType) of
ftString: S:=S+' CHAR('+IntToStr(Fields.dataSize-1)+')';
ftInteger: s:=S+' Integer';
ftSmallInt: s:=S+' Integer';
ftFloat: s:=S+' Float';
ftdate: s:=S+' DateTime';
Else
s:=s+' ';
End;
S:=S+' NULL';
If i<FieldCount-1 Then
S:=S+',';
List.Lines.Add(s);
End;
List.Lines.Add(')');
List.Visible:=False;
List.Visible:=True;
End;
Button2.Enabled:=True;
Table1.Close;
end;

Function Space(N:Integer):String;
Var
S:String;
j:Integer;
Begin
For j:=1 to N Do
S:=S+'';
Space:=S;
End;
procedure TTableTranW.Button2Click(Sender: TObject);
Var
replace:Boolean;
begin
rePlace:=True;

If (DstList.Items.IndexOf('dbo.'+DstFile.Text)<>-1)
Or (DstList.Items.IndexOf(DstFile.Text)<>-1)then
If MessageDlg('¸ÃÊý¾Ý¿â±íÒѾ­´æÔÚ,Ìæ»»Âð',
mtInformation, [mbYes, mbNo], 0)= mrYes then
If MessageDlg('´íÎóÌæ»»½«Ôì³ÉÎÞ·¨»Ö¸´µÄÐÅÏ¢¶ªÊ§,ÇëÔÙ´ÎÈ·ÈÏ',
mtInformation, [mbYes, mbNo], 0) = mrYes
Then
Begin
DeleteTable.Sql.Clear;
deleteTable.Sql.Add('Drop Table '+DstFile.text);
DeleteTable.ExecSQl;
End
Else
Replace:=False
Else
Replace:=False;
if Replace Then
Begin
CreateTable.SQL:=List.Lines;
CreateTable.EXECSQL;
DstList.Items.Add(DstFile.text);
DstList.Visible:=False;
DstList.Visible:=True;
If Uppercase(DstFile.Text)='PASS' Then
InSYS.EXECSQL;
End;
Button2.Enabled:=False;
end;

procedure TTableTranW.SrcAliasChange(Sender: TObject);
begin
Session.GetTableNames(SrcAlias.Text,'*.DBF',False, False,SrcList.Items);
SrcBase.Connected:=False;
SrcBase.AliasName:=SrcAlias.Text;
SrcBase.Connected:=True;
end;

procedure TTableTranW.DstAliasChange(Sender: TObject);
begin
Session.GetTableNames(DstAlias.Text,'*.*',False, False,DstList.Items);
DstBase.Connected:=False;
DstBase.AliasName:=DstAlias.Text;
DstBase.Connected:=True;
end;

procedure TTableTranW.FormCreate(Sender: TObject);
begin
Session.getAliasNames(SrcAlias.Items);
Session.getAliasNames(DstAlias.Items);
end;

procedure TTableTranW.SrcListDblClick(Sender: TObject);
begin
SrcFile.text:=SrcList.items[SrcList.ItemIndex];
DstFile.text:=SrcList.items[SrcList.ItemIndex];
end;

procedure TTableTranW.DstListClick(Sender: TObject);
begin
DstFile.text:=DstList.items[DstList.ItemIndex];
end;

procedure TTableTranW.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=cafree;
end;

procedure TTableTranW.BitBtn1Click(Sender: TObject);
Var
i,CountSS:integer;
begin
Countss:=SrcList.items.Count;
for i:=0 to Countss-1 do
begin
SrcFile.text:=SrcList.items;
DstFile.text:=SrcList.items;
Button1Click(Sender);
Button2Click(Sender);
end;
end;
 
多人接受答案了。
 
后退
顶部