如何将记事本中数据导入到数据库中,100分!!!!!!!!!!(50分)

  • 主题发起人 主题发起人 cz_liusen
  • 开始时间 开始时间
C

cz_liusen

Unregistered / Unconfirmed
GUEST, unregistred user!
哪位高手能将记事本中的数据导入到数据库中,最好能给点代码,分数是没有问题的,答对了继续加分!!!!!!!!
 
下面是我以前帮人家写的一段代码,希望对你有用。
procedure TForm1.Button2Click(Sender: TObject);
var
list: TstringList;
i: integer;
ClientID, OrdNO, OrdDate, sMemo, PrdtID, orderQty, Remark, temp, a, b: string;
begin
List := TstringList.Create;
try
a := ' ';
List.LoadFromFile('1.txt');
for i := 1 to list.Count -1 do //0行为表头,2行为从表的表头
begin
if i = 1 then
begin
ClientID := Copy(List, 0, pos(a, List) -1); //将客户单号截取出来
temp := StringReplace(List, ClientID + a ,'', [rfReplaceALL]); //删除客户单号和分割符 a
OrdNO := Copy(temp, 0, pos(a, temp) -1) ; //将订单号截取出来
temp := StringReplace(List, ClientID + a + OrdNO + a ,'', [rfReplaceALL]); //删除客户单号和分割符 a ,订单号,分割符a,下同
OrdDate := Copy(temp, 0, pos(a, temp) -1) ;
temp := StringReplace(List, ClientID + a + OrdNO + a + OrdDate + a ,'', [rfReplaceALL]);
sMemo := temp;
end;

if i > 2 then
begin
temp := StringReplace(List, ClientID + a ,'', [rfReplaceALL]);
PrdtID := Copy(temp, 0, pos(a, temp) -1) ;
temp := StringReplace(temp, ClientID + a + PrdtID + a ,'', [rfReplaceALL]);
orderQty := Copy(temp, 0, pos(a, temp) -1) ;
Remark := StringReplace(temp, (ClientID + a + PrdtID + a + orderQty + a) ,'', [rfReplaceALL]);
end;
//更新数据表的代码写在这里啦。
b := b + ClientID + OrdNO + OrdDate + sMemo + PrdtID + orderQty + Remark + chr(13)
end;
ShowMessage(b);
finally
FreeAndNil(List);
end;
end;
 
SQL里面有BCP工具,并且可以定义格式文件导入。
bcp pubs..authors2 in c:/new_auth.dat -fc:/authors.fmt -Sservername -Usa -Ppassword

The BULK INSERT statement can use format files saved by the bcp utility. For example:

BULK INSERT pubs..authors2 FROM 'c:/new_auth.dat'
WITH (FORMATFILE = 'c:/authors.fmt')
 
bulk insert 具体看帮助.
 
具体看什么数据库,access有一些工具的,sybase、SQL SERVER 都有BCP工具的。
 
procedure TForm1.XButton1Click(Sender: TObject);
var
list:Tstringlist;
str1,str2,str3,lsstr:string;
i,k,j:integer;
begin
list:=Tstringlist.Create;
list.LoadFromFile('C:/Exam/cad.txt');
for i:=0 to list.Count-1 do
begin
if (pos('//',list.Strings)=0) and (pos('IndexID',list.Strings)=0) and (length(list.Strings)>2) then
begin
j:=pos(#9,list.Strings);
str1:=copy(list.Strings,1,j-1);
// showmessage(str1);
lsstr:=trim(copy(list.Strings,j,length(list.Strings)-j+1));
j:=pos(#9,lsstr);
str2:=copy(lsstr,1,j);
lsstr:=trim(copy(lsstr,j,length(lsstr)-j+1));
str3:=lsstr;
self.ADOQuery1.Close;
self.ADOQuery1.SQL.Clear;
self.ADOQuery1.SQL.Text:='insert into CAD_Data (IndexID,Height,Width )values('
+str1
+','
+str2
+','
+str3
+')';
self.ADOQuery1.SQL.SaveToFile('c:/sql.txt');
self.ADOQuery1.ExecSQL;
end;
end;

例子 把文本文件存到sql的表里。文本文件的格式是这样的。。希望对LZ有帮助,
IndexID Height Width
1 124.714 346.456
2 124.714 468.376
3 298.958 423.164
4 299.847 440.0296
5 300.355 447.167
6 302.387 444.881
7 302.641 447.929
8 299.974 468.376
9 305.9938 422.9608
10 307.213 422.91
11 310.896 432.943
12 306.705 440.055
13 309.499 444.627
14 313.817 438.404
15 315.595 438.404
16 318.008 454.914
17 315.722 467.614
18 316.23 468.376
19 325.628 413.258
20 325.628 417.068
 
后退
顶部