list index out of bounds(0)

yuanterry 2011-02-28 09:48:05
DELPHI 调用存储过程报错:list index out of bounds(0)
With AdoDataset1 Do
Begin
Active:=False;
CommandType:=cmdStoredProc;
CommandText:='P_QUERY';
Parameters.Clear;
Parameters[0].Name:='@DATE1';
Parameters[0].Direction:=Pdinput;
Parameters[0].DataType:=ftstring;
Parameters.Refresh;
Parameters.ParamByName('@Date1').Value:=trim(Edit1.Text);
Parameters[1].Name:='@Date2';
Parameters[1].Direction:=Pdinput;
Parameters[1].DataType:=ftstring;
Parameters.Refresh;
Parameters.ParamByName('@DATE1').Value:=Trim(Edit2.Text);
Active:=True;
//Parameters.ParamByName('@Date1').Value:='2011-01-01';
//Parameters.ParamByName('@Date2').Value:='2011-02-27';
ExecuteOptions
...全文
304 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kye_jufei 2011-02-28
  • 打赏
  • 举报
回复
eg2:

unit Unit1;

Interface usesinterface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids;

type
  TTestProcedures = Class (class(TForm)
    ADOConnection1: TADOConnection;
    ADODataSet1: TADODataSet;
    DataSource1: TDataSource;
    Selete: TButton;
    Edit1: TEdit;
    DBGrid1: TDBGrid;
    GroupBox1: TGroupBox;
    Select: TGroupBox;
    Label1: TLabel;
    GroupBox2: TGroupBox;
    Edit2: TEdit;
    Edit3: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Insert: TButton;
    ADOStoredProc1: TADOStoredProc;
    GroupBox3: TGroupBox;
    Edit4: TEdit;
    Label4: TLabel;
    Delete: TButton;
    GroupBox4: TGroupBox;
    Label5: TLabel;
    Edit5: TEdit;
    Label6: TLabel;
    Edit6: TEdit;
    Label7: TLabel;
    Edit7: TEdit;
    Update: TButton;
    procedure GetAll;
    procedure SeleteClick(Sender: TObject);
    procedure InsertClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure DeleteClick(Sender: TObject);
    procedure UpdateClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  TestProcedures: TTestProcedures;

implementation

{$R *.dfm}

procedure TTestProcedures.SeleteClick(Sender: TObject);
begin
with ADODataSet1 do
 begin
   Active:=false;
   CommandType:=cmdStoredProc;
   CommandText:='usp_selectone';
   Parameters.Clear;
   Parameters.AddParameter;

   Parameters[0].Name:='@id';
   Parameters[0].Direction:= pdInput;
   Parameters[0].DataType:=ftString;

Parameters.Refresh;
   Parameters.ParamByName('@id').Value:=Edit1.Text;

   Active:=true;
   ExecuteOptions;

 end;
end;

procedure TTestProcedures.InsertClick(Sender: TObject);
begin
  try
    AdoConnection1.BeginTrans;
    with ADOStoredProc1 do
    begin
      Close;
      ProcedureName:='usp_insertone';
      Parameters.Refresh;
      Parameters.ParamByName('@aaaaa').Value:=Edit2.Text;
      parameters.ParamByName('@bbbbb').Value:=Edit3.text;
      ExecProc;
    end;
    AdoConnection1.CommitTrans;
    GetAll;
  except
    Adoconnection1.RollbackTrans;
  end;
end;
procedure TTestProcedures.DeleteClick(Sender: TObject);
begin
  try
    AdoConnection1.BeginTrans;
    with ADOStoredProc1 do
    begin
      close;
      ProcedureName:='usp_deleteone';
      Parameters.Refresh;
      Parameters.ParamByName('@id').Value:=Edit4.Text;
      ExecProc;
    end;
    Adoconnection1.CommitTrans;
    GetAll;
  except
    Adoconnection1.RollbackTrans;
  end;
end;

procedure TTestProcedures.UpdateClick(Sender: TObject);
begin
  try
    AdoConnection1.BeginTrans;
    with ADOStoredProc1 do
    begin
      close;
      Procedurename:='usp_updateone';
      Parameters.Refresh;
      Parameters.ParamByName('@id').Value:=Edit5.Text;
      Parameters.ParamByName('@aaaaa').Value:=Edit6.Text;
      Parameters.ParamByName('@bbbbb').Value:=Edit7.Text;
      ExecProc;
    end;
    AdoConnection1.CommitTrans;
    GetAll;


except
    adoconnection1.RollbackTrans;
  end;
end;

procedure TTestProcedures.FormShow(Sender: TObject);
begin
 GetAll;
end;

procedure TTestProcedures.GetAll;
begin
  with  ADODataSet1 do
 begin
   Active:=false;
   CommandType:=cmdStoredProc;
   CommandText:='usp_selectall';
   Active:=true;
   ExecuteOptions;
 end;
end;
end.



 SQL DBScript:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_deleteone]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[usp_deleteone]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_insertone]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[usp_insertone]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_selectall]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[usp_selectall]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_selectone]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[usp_selectone]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_updateone]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[usp_updateone]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[test]
GO

CREATE TABLE [dbo].[test] (
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [aaaaa] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
    [bbbbb] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE usp_deleteone 
@id int
AS
delete  from test where id = @id

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE usp_insertone 
@aaaaa varchar(50),
@bbbbb varchar(50)
AS
insert into test (aaaaa,bbbbb)values(@aaaaa,@bbbbb)

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE usp_selectall 
AS
select * from test

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE usp_selectone 
@id int
AS
SELECT aaaaa,bbbbb  FROM test where id=@id

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE usp_updateone
@id int,
@aaaaa varchar(50),
@bbbbb varchar(50)
AS
Update test set aaaaa=@aaaaa,bbbbb=@bbbbb where id=@id

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
kye_jufei 2011-02-28
  • 打赏
  • 举报
回复
eg1:
ADOStoredProc1.ProcedureName:= 'total '; 
ADOStoredProc1.Parameters.clear;
ADOStoredProc1.Parameters.createparam(,,,,);//创建参数
ADOStoredProc1.Parameters.ParamByName( '@begin_time ').value:=strtodatetime(datetostr(DateTimePicker1.Date)+ ' ' + timetostr(datetimepicker2.Time));
ADOStoredProc1.Parameters.createparam(,,,,);//创建参数,内容你自己填写吧。^_^
ADOStoredProc1.Parameters.ParamByName( '@end_time ').value:=strtodatetime(datetostr(datetimepicker3.Date)+ ' ' + timetostr(datetimepicker4.Time));
ADOStoredProc1.ExecProc ;



hongqi162 2011-02-28
  • 打赏
  • 举报
回复
Parameters.Clear;
Parameters[0].Name:='@DATE1';

//已经clear了,怎么对0进行赋值了,你需要add一个参数再赋值吧
kye_jufei 2011-02-28
  • 打赏
  • 举报
回复
溢出錯誤。。。

1.list index out of bounds" 错误可谓比较经典的delphi错误,原因是存取TList元素时超过TList边界(范围),这个错误非来自数据库。当然更具体点通过异常类型知。
2、你的代码中存在资源泄漏的可能:
...Create...
try
...
finally
...free...
end;

另外,你可能也很少使用调试方法或工具吧,你可以用这些知道你的错误具体位置,然后跟踪,然后修改,迭代下去

2,495

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧