B
babyrun
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB, DB, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOCommand1: TADOCommand;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
function ExecSql(SqlCmd: string): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//函数====================================================================
function TForm1.ExecSql(SqlCmd: string): Boolean;
begin
result := true ;
try
AdoCommand1.CommandText := SqlCmd ;
AdoCommand1.Execute ;
except
result := false ;
end;
end;
//主程序==================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
MyAdoDataSet : TAdoDataSet ;
sFileName,sTmp,Sql : string;
F : TextFile;
sFileList : TStringList;
i : integer;
addno,netno,code,money : string;
begin
MyAdoDataSet := TAdoDataSet.Create(nil);
MyAdoDataSet.Connection := ADOConnection1;
//----------------------------------------------
if OpenDialog1.Execute then
sFileName := OpenDialog1.FileName;
if FileExists(sFileName) then
begin
AssignFile(f,sFileName);
Reset(F);
end;
Sql := 'delete from table';
if ExecSql(sql)= false then
exit;
//----------------------------------------------
sFileList := TStringList.Create();
sFileList.LoadFromFile(sFileName);
for i := 0 to sFileList.Count-1do
begin
sTmp := sFileList.Strings;
code := trim(copy(sTmp,16,4));
if code = '2420' then
begin
//
addno := trim(copy(sTmp,6,4));
netno := trim(copy(sTmp,11,4));
money := trim(copy(sTmp,41,25));
Sql:= Format('insert into table(zone,brno,jydm,amount)'+
'values(''%s'',''%s'',''%s'',''%s'')',[addno,netno,code,money]);
if ExecSql(Sql)=false then
exit
else
begin
Sql:= 'insert into table(zone,brno,jydm,amount) values(''2101'',''2101'',''2001'',''1'')' ;
if ExecSql(sql) = false then
exit ;
end;
end;
end;
end;
end.
接触delphi不久,遇到问题颇多。请各位不吝赐教。
程序要实现的功能是,将读入的文件列表按字段名写入数据库。请各位帮忙看看错误处在什么地方?
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB, DB, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOCommand1: TADOCommand;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
function ExecSql(SqlCmd: string): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//函数====================================================================
function TForm1.ExecSql(SqlCmd: string): Boolean;
begin
result := true ;
try
AdoCommand1.CommandText := SqlCmd ;
AdoCommand1.Execute ;
except
result := false ;
end;
end;
//主程序==================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
MyAdoDataSet : TAdoDataSet ;
sFileName,sTmp,Sql : string;
F : TextFile;
sFileList : TStringList;
i : integer;
addno,netno,code,money : string;
begin
MyAdoDataSet := TAdoDataSet.Create(nil);
MyAdoDataSet.Connection := ADOConnection1;
//----------------------------------------------
if OpenDialog1.Execute then
sFileName := OpenDialog1.FileName;
if FileExists(sFileName) then
begin
AssignFile(f,sFileName);
Reset(F);
end;
Sql := 'delete from table';
if ExecSql(sql)= false then
exit;
//----------------------------------------------
sFileList := TStringList.Create();
sFileList.LoadFromFile(sFileName);
for i := 0 to sFileList.Count-1do
begin
sTmp := sFileList.Strings;
code := trim(copy(sTmp,16,4));
if code = '2420' then
begin
//
addno := trim(copy(sTmp,6,4));
netno := trim(copy(sTmp,11,4));
money := trim(copy(sTmp,41,25));
Sql:= Format('insert into table(zone,brno,jydm,amount)'+
'values(''%s'',''%s'',''%s'',''%s'')',[addno,netno,code,money]);
if ExecSql(Sql)=false then
exit
else
begin
Sql:= 'insert into table(zone,brno,jydm,amount) values(''2101'',''2101'',''2001'',''1'')' ;
if ExecSql(sql) = false then
exit ;
end;
end;
end;
end;
end.
接触delphi不久,遇到问题颇多。请各位不吝赐教。
程序要实现的功能是,将读入的文件列表按字段名写入数据库。请各位帮忙看看错误处在什么地方?