到底哪里错了?(0分)

  • 主题发起人 主题发起人 babyrun
  • 开始时间 开始时间
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不久,遇到问题颇多。请各位不吝赐教。
程序要实现的功能是,将读入的文件列表按字段名写入数据库。请各位帮忙看看错误处在什么地方?
 
是这个问题么,我现在要下了,明天来了一定仔细看看。。。。
 
Sql:= Format('insert into table(zone,brno,jydm,amount)'+
[red]~~~[/red]加空格
'values(''%s'',''%s'',''%s'',''%s'')',[addno,netno,code,money]);
[red]~~~~[/red]
if ExecSql(Sql)=false then
exit
else
begin
Sql:= 'insert into table(zone,brno,jydm,amount) values
[red]~~~~[/red] [red]~~~~[/red]
(''2101'',''2101'',''2001'',''1'')' ;
另外建议你用事件来处理错误,这样不会出现数据不同步的问题
 
从贴出的代码看不出什么问题,楼上说的加空格是对的,
Sql:= Format('insert into table(zone,brno,jydm,amount)'+
Sql:= Format('insert into table(zone,brno,jydm,amount) '+
下面的两行代码没有起作用:
AssignFile(f,sFileName);
Reset(F);
改为如下即可:
if not FileExists(sFileName) then
Exit;
 
接受答案了.
 

Similar threads

I
回复
0
查看
642
import
I
I
回复
0
查看
2K
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
2K
import
I
后退
顶部