全部的分值都给你,只要你能帮我解决问题!! 关于delphi对access操作的

blueflag 2006-11-06 10:11:50
我这段代码的主要作用是: 把stringgrid的数据存入已知表! stringgrid中的某些字段,我需要从另外一个表里读出另外的一个字段替换掉! 这段代码在DLL中实现,是一个按钮的click事件中写的!

sql.Add('insert into stainfo(stano,sname,sex,depid,sorts)'+
'values(:InpStano,:InpSname,:InpSex,[SELECT dept.id FROM dept WHERE (((dept.dep)=:InpDep))],[SELECT dept.id FROM dept WHERE (((dept.sorts)=:InpSorts))]');

Parameters.ParamByName('InpStano').Value:=strgrinport.Cells[1,i];
Parameters.ParamByName('InpSname').Value:=StrGrInport.Cells[2,i];
Parameters.ParamByName('InpSex').Value:=StrGrInport.Cells[3,i];
InpDep:=StrGrInport.Cells[8,i]; //要读取其他表获得
InpSorts:=StrGrInport.Cells[9,i]; //要读取其他表获得

编译没有任何错误,但是运行的时候,提示 ''不是有效值,估计是参数传递的时候,值没有传递出来,为什么这样,该如何解决,谢谢大家,给个指引!!!
...全文
416 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
XXSingle 2006-11-09
  • 打赏
  • 举报
回复
如果是数值类型的字段,''肯定是无效值
blueflag 2006-11-09
  • 打赏
  • 举报
回复
神经啊!!什么垃圾程序啊,改完后,怎么会出现
Parameter 'InpStano' not found ! 错误啊!! 这东西明明写上了啊,!! 我改成
InportQuery.Parameters.ParamByName('InpStano').Value:=StrGrInport.Cells[1,i];
也不行~~(就是给他指定对象)
对了,是不是因为 stringgrid 的第一列没有数据?? 不对啊,第一列有数据啊!!! 这个问题太奇怪了~~
前面都对了~~
blueflag 2006-11-09
  • 打赏
  • 举报
回复
明天结帖~~~~~~~~~~
blueflag 2006-11-09
  • 打赏
  • 举报
回复
问题解决,开心啊~~ 庆祝庆祝庆祝啊~~~

//第三步
with InportQuery do
begin
ParamCheck := False; <<<-----这个在搞鬼,我都把它给忘了,呵呵, 注释掉,就OK!!!
Close;
SQL.Clear;
SQL.Add('insert into stainfo(stano,sname,sex,depid,sorts)' +
'values(:InpStano,:InpSname,:InpSex,:InpDep,:InpSorts)');
.......

还不知道稳定不稳定,我在测试中~~ 嘎嘎,太开心了~~ 2个星期的问题啊,终于解决了,爽死了~~
blueflag 2006-11-09
  • 打赏
  • 举报
回复
InpStano 对应的是文本~
而且,StrGrInport.Cells[1,i]的值也出来了啊,也正常啊~~ 不明白了~~~
blueflag 2006-11-08
  • 打赏
  • 举报
回复
我的dept表中,有信息中心这个记录,ID号是29 !!


我现在估计是因为在DLL中实现,或许还有其他的问题没有注意到啊!!
在DLL中实现,这些代码都是在click事件下,还需要怎么处理吗???
do2008 2006-11-08
  • 打赏
  • 举报
回复
>>my_deptid := fieldbyname('id').asinteger;

这一句是没有问题的,问题的是下一句:
my_InpSorts := fieldbyname('id').asinteger;
//showmessage(string(my_InpSorts));这一句不要,改为下面
showmessage(IntToStr(my_InpSorts));
blueflag 2006-11-08
  • 打赏
  • 举报
回复
>>>>my_deptid := fieldbyname('id').asinteger;

我已经调通其他语句,但是这句赋值出错!! :
ACCESS VIOLATION ....
错误!!! 好象是哪个地方的内存不能读!!!
程序确实已经走到了这步!!!
do2008 2006-11-07
  • 打赏
  • 举报
回复
把你的代码,能贴出来,都贴出来吧,
blueflag 2006-11-07
  • 打赏
  • 举报
回复
这个问题很奇怪,表名 字段 都对!! 要插入的几个字段都是可以为空的!!
我快被折磨死了!!
我现在改了,改完后showmessage(string(my_deptid))出现空值,然后就出现一个ACCESS什么错误,看不懂,好象是读取哪个地方的内存地址禁止读取~~我倒,越来越复杂了~~ 可是我的数据库是可以为空值的啊~~ 郁闷死了

do2008 2006-11-07
  • 打赏
  • 举报
回复
>>fields dept.id not found!

是指sql语句中的字段名,不在表中~~~

我在上面说了,我看不明白你的数据表结构,
上面的代码,只是给你一个大致的思路,具体的字段,和表名,你要根据你的表名,字段名来写代码.

最好用单步调试跟踪一下,看看到底是哪个SQL的字段名不对,查出来,再改程序.

liliang800207 2006-11-07
  • 打赏
  • 举报
回复
再你执行sql之前用showmessage把你插入的数据显示出来
看有没有为空值的地方

如果没有看你的数据库设计又没有不让为空的字段

最后 一定要确定你插入的表 及其 表的字段是否一致、正确

3点应该可以解决你的问题
blueflag 2006-11-07
  • 打赏
  • 举报
回复
TO : do2008

按照你的方法还是有错:
files dept.id not found!
blueflag 2006-11-07
  • 打赏
  • 举报
回复
TO : liongo_007()
触发器怎么做?? 我新手,没弄通啊!! 给个指引啊,谢谢!!!
1024office 2006-11-07
  • 打赏
  • 举报
回复
估计是类型转换时发生的问题,你贴点有价值的出来看看呢?
do2008 2006-11-07
  • 打赏
  • 举报
回复
>>?查询不到ID'+信息中心

这是因为你的dept表中,没有"信息中心"的记录吧;
blueflag 2006-11-07
  • 打赏
  • 举报
回复
不行,弹出错误: 第二步查询不到ID'+信息中心
就是说,showmessage 显示了~~
也就是说, sql.add('SELECT [dept].[id] FROM [dept] WHERE [dept].[dep]=' + QuotedStr(InpDep1)); 没有执行或没有检索出来数据!!!!

你给我讲一下 InportQuery, adoQuery, adoQuery1: TADOQuery; 这些动态创建的ADOQuery,他是怎么连接的吧?? 我现在越弄月糊涂了,都弄不清楚了~~

谢谢~~
do2008 2006-11-07
  • 打赏
  • 举报
回复
你的代码,可以再精简一些,优化一些~~~

试试这样行不行;

procedure TForm1.BitBtn1Click(Sender: TObject);
//存盘
var

i: integer;
my_deptid: integer;
my_InpSorts: integer;
InpDep1, InpSorts1: string;

InportQuery, adoQuery, adoQuery1: TADOQuery;
begin
InportQuery := TADOQuery.Create(self);
InportQuery.Connection := ADOConnection1;
adoQuery := TADOQuery.Create(self);
adoQuery.Connection := ADOConnection1;
adoQuery1 := TADOQuery.Create(self);
adoQuery1.Connection := ADOConnection1;

my_deptid := 0;
my_InpSorts := 0;

//for i := 1 to StrGrInport.RowCount do 这一句是你原来的
for i := 1 to StrGrInport.RowCount-1 do
begin
//if StrGrInport.Cells[i,1]<>'' then 这一句是你原来的
if StrGrInport.Cells[1,i] <> '' then
begin
try

//第一步
with adoQuery do
begin
close();
sql.clear();
ParamCheck := False;
InpDep1 := StrGrInport.Cells[8, i]; //要读取其他表获得

sql.add('SELECT [dept].[id] FROM [dept] WHERE [dept].[dep]=' + QuotedStr(InpDep1));
open();
if (not isempty()) then
begin
my_deptid := fieldbyname('id').asinteger;
showmessage(string(my_deptid));
end
else
begin
showmessage('第二步查询不到ID'+InpSorts1);
end;
end;

//第二步
with adoQuery1 do
begin
close();
sql.clear();
ParamCheck := False;
InpSorts1 := StrGrInport.Cells[9, i]; //要读取其他表获得
sql.add('SELECT [oinfo].[id] FROM [oinfo] WHERE [oinfo].[sname]=' + QuotedStr(InpSorts1));
open();
if (not isempty()) then
begin
my_InpSorts := fieldbyname('id').asinteger;
showmessage(string(my_InpSorts));
end
else
showmessage('第二步查询不到ID'+InpSorts1);
end;

//第三步
with InportQuery do
begin
ParamCheck := False;
Close;
SQL.Clear;
SQL.Add('insert into stainfo(stano,sname,sex,depid,sorts)' +
'values(:InpStano,:InpSname,:InpSex,:InpDep,:InpSorts)');

Parameters.ParamByName('InpStano').Value := strgrinport.Cells[1, i];
Parameters.ParamByName('InpSname').Value := StrGrInport.Cells[2, i];
Parameters.ParamByName('InpSex').Value := StrGrInport.Cells[3, i];
Parameters.ParamByName('InpDep').Value := my_deptid;
Parameters.ParamByName('InpSorts').Value := my_InpSorts;

ExecSQL;
Close;
end;

except
on E: Exception do
MessageBox(Handle, PAnsiChar('数据存盘失败!信息为:' + E.Message), '提示', MB_OK + MB_ICONINFORMATION);
end;
end;
end;

end;
blueflag 2006-11-07
  • 打赏
  • 举报
回复
这个是在DLL中实现的! 用来导入导出~!完整代码如下: 别嫌多哈~ 嘿嘿~~

procedure TfrmInportData.Button2Click(Sender: TObject);
//存盘
var

i:integer;
my_deptid:integer;
my_InpSorts:integer;
InpDep1,InpSorts1 :string;
begin
InportQuery:=TADOQuery.Create(self);
InportQuery.Connection:=Fconn;
adoQuery:=TADOQuery.Create(self);
adoQuery.Connection:=Fconn;
adoQuery1:=TADOQuery.Create(self);
adoQuery1.Connection:=Fconn;
with InportQuery do

begin
my_deptid:=0;
my_InpSorts:=0;

with StrGrInport do

for i:=1 to StrGrInport.RowCount do
begin

begin
if StrGrInport.Cells[i,1]<>'' then
begin
try

adoQuery.close();
adoQuery.sql.clear();
adoQuery.ParamCheck:=False;
InpDep1:=StrGrInport.Cells[8,i]; //要读取其他表获得

adoQuery.sql.add('SELECT [dept].[id] FROM [dept] WHERE [dept].[dep]='+QuotedStr(InpDep1));
adoQuery.open();
if (Not adoQuery.isempty()) then
my_deptid:= adoQuery.fieldbyname('id').asinteger;
showmessage(string(my_deptid)) ;

adoQuery1.close();
adoQuery1.sql.clear();
adoQuery1.ParamCheck:=False;
InpSorts1:=StrGrInport.Cells[9,i]; //要读取其他表获得
adoQuery1.sql.add('SELECT [oinfo].[id] FROM [oinfo] WHERE [oinfo].[sname]='+QuotedStr(InpSorts1));
adoQuery1.open();
if (Not adoQuery1.isempty()) then
my_InpSorts:= adoQuery1.fieldbyname('id').asinteger;
showmessage(string(my_InpSorts));

InportQuery.ParamCheck:=False;
InportQuery.Close;
InportQuery.SQL.Clear;
InportQuery.SQL.Add('insert into stainfo(stano,sname,sex,depid,sorts)'+
'values(:InpStano,:InpSname,:InpSex,:InpDep,:InpSorts)');


Parameters.ParamByName('InpStano').Value:=strgrinport.Cells[1,i];
Parameters.ParamByName('InpSname').Value:=StrGrInport.Cells[2,i];
Parameters.ParamByName('InpSex').Value:=StrGrInport.Cells[3,i];
// Parameters.ParamByName('InpBirth').Value:=strtodate(StrGrInport.Cells[4,i]);
// Parameters.ParamByName('InpTel').Value:=StrGrInport.Cells[5,i];
// Parameters.ParamByName('InpMobile').Value:=StrGrInport.Cells[6,i];
// Parameters.ParamByName('InpEmail').Value:=StrGrInport.Cells[7,i];

// Parameters.ParamByName('InpDep').Value:=1; //数字,要读取其他表获得
// Parameters.ParamByName('InpSorts').Value:=2; //数字,要读取其他表获得
// Parameters.ParamByName('InpDuty').Value:=3; //数字,要读取其他表获得

// Parameters.ParamByName('InpFolk').Value:=4; //数字,要读取其他表获得
// Parameters.ParamByName('InpLev').Value:=5; //数字,要读取其他表获得
// Parameters.ParamByName('InpSpec').Value:=6; //数字,要读取其他表获得

Parameters.ParamByName('InpDep').Value:=my_deptid;
Parameters.ParamByName('InpSorts').Value:=my_InpSorts;

//Parameters.ParamByName('InpDep').Value:=StrGrInport.Cells[8,i]; //数字,要读取其他表获得
// Parameters.ParamByName('InpSorts').Value:=StrGrInport.Cells[9,i]; //数字,要读取其他表获得
// Parameters.ParamByName('InpDuty').Value:=StrGrInport.Cells[10,i]; //数字,要读取其他表获得
// Parameters.ParamByName('InpProvi').Value:=StrGrInport.Cells[11,i];
// Parameters.ParamByName('InpFolk').Value:=StrGrInport.Cells[12,i]; //数字,要读取其他表获得
// Parameters.ParamByName('InpLev').Value:=StrGrInport.Cells[13,i]; //数字,要读取其他表获得
// Parameters.ParamByName('InpSpec').Value:=StrGrInport.Cells[14,i]; //数字,要读取其他表获得
// Parameters.ParamByName('InpCardno').Value:=StrGrInport.Cells[15,i];
// Parameters.ParamByName('InpRecno').Value:=StrGrInport.Cells[16,i];
// Parameters.ParamByName('InpPolitics').Value:=StrGrInport.Cells[17,i];
// Parameters.ParamByName('InpSlevel').Value:=StrGrInport.Cells[18,i];
// Parameters.ParamByName('InpMarried').Value :=StrGrInport.Cells[19,i];
// Parameters.ParamByName('InpIndate').Value:=strtodate(StrGrInport.Cells[20,i]);
// Parameters.ParamByName('InpDutyState').Value:=StrGrInport.Cells[21,i];
// Parameters.ParamByName('InpDutyDate').Value:=strtodate(StrGrInport.Cells[22,i]);
// Parameters.ParamByName('InpStaDate').Value:=strtodate(StrGrInport.Cells[23,i]);
// Parameters.ParamByName('InpLeftDate').Value:=strtodate(StrGrInport.Cells[24,i]);
// Parameters.ParamByName('InpBaryYear').Value:=StrGrInport.Cells[25,i];
// Parameters.ParamByName('InpBarStart').Value:=strtodate(StrGrInport.Cells[26,i]);
// Parameters.ParamByName('InpBarEnd').Value:=strtodate(StrGrInport.Cells[27,i]);
// Parameters.ParamByName('InpSchool').Value:=StrGrInport.Cells[28,i];
// Parameters.ParamByName('InpGraDate').Value:=StrGrInport.Cells[29,i];
// Parameters.ParamByName('InpBody').Value:=StrGrInport.Cells[30,i];
// Parameters.ParamByName('InpAddress').Value:=StrGrInport.Cells[31,i];
// Parameters.ParamByName('InpPcode').Value :=StrGrInport.Cells[32,i];
// Parameters.ParamByName('InpDes').Value:=StrGrInport.Cells[33,i];

InportQuery.ExecSQL;
InportQuery.Close;

except
on E: Exception do
MessageBox(Handle, PAnsiChar('数据存盘失败!信息为:' + E.Message), '提示', MB_OK + MB_ICONINFORMATION);
end;
end;
end;
end;
end;
end;

这就是实现存盘的全部功能代码~~
do2008 2006-11-06
  • 打赏
  • 举报
回复
原因应该是,你的SQL语句,中某个列的值,不能写入数据表中,

>>>[SELECT dept.id FROM dept WHERE (((dept.dep)=:InpDep))],[SELECT dept.id FROM dept WHERE (((dept.sorts)=:InpSorts))]');

你这里两个都是[SELECT dept.id FROM ,[SELECT dept.id FROM...
而你的insert语句是:stainfo(stano,sname,sex,depid,sorts)

第二个是否要改为SELECT dept.sorts,呵呵,不知道你的表结构的字段意思,


另外如果不行,建议将上边的操作,分成三个sql语句来做,
先执行两个select语句,获取到那两个字段的值,再作为insert语句的参数吧
加载更多回复(10)

2,497

社区成员

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

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