stringgrid1内数据修改到数据库方法求教!(200分)

V

vip1977

Unregistered / Unconfirmed
GUEST, unregistred user!
刚刚用STRINGGRID不是很熟悉,现在数据库为主从表格式!
STRINGGRID1内放的是从表内容,现在从表内容已经添入到的STRINGGRID中(多条记录),
要求修改STRINGGRID中的内容提交给数据库,然后重新刷新,主表记录也同时修改!
不知道是怎么做的,本人用的是ADO,C/S结构,要求效率高,而且最好具体一点。
 
function TSaddForm.AddCKSH :integer;
var
i:integer;
Res:integer;
begin
for i:=1 to 11 do
begin
if (stringgrid1.Cells[i,1]<>'') and (stringgrid1.Cells[i,2]<>'') and then
begin
Res:=UpdateDm.RefashMX(SWSJ.LiuSH,stringgrid1.Cells[i,0],SGHS.Cells[i,1],
'国内扣费'))
end;
if (SGHS.Cells[i,3]<>'') and (SGHS.Cells[i,4]<>'') then
begin
Res:=UpdateDm.RefashMX(SWSJ.LiuSH,stringgrid1.Cells[i,0],SGHS.Cells[i,3],
'国外扣费))
end;
if Res=-100 then
begin
Messagebox(handle,'数据更新出错','提示信息成功',MB_OK+MB_ICONInformation);
conn.AdoConn.RollbackTrans ;
abort;exit;
end;
end;
end;
这个是我现在在用的,不知道对你有没有帮助。
 
用数据库最好用DBGrid.
 
TO:CHRISTIN
你说的方法因该是可行的,不过你的RefashMX是怎么做的呢?
能不能详细点,而且如果在STRINGGRID中有100,或者1000行
那你怎么处理呢?
 
在网上有很多介绍数据库内容填入STRINGGRID的内容,可是修改到数据库却写的不
是很具体,而且效率问题也有待观察,希望大家献技,给予帮助?
 
哦,可能写的有点乱,不好意思。
首先是写入各个cell名目的代码
with stringgrid do
begin
//////////////////////竖列
Cells[0,0]:='项目';
Cells[0,1]:='field1';
Cells[0,2]:='field2';
Cells[0,3]:='field3';
Cells[0,4]:='field4';
/////////////////////////横列
Cells[1,0]:='field2-1';
Cells[2,0]:='field2-2';
Cells[3,0]:='field2-3';
Cells[4,0]:='field2-4';
Cells[5,0]:='field2-5';
Cells[6,0]:='field2-6';
Cells[7,0]:='field2-7';
Cells[8,0]:='field2-8';
end;
如果字段多可以做循环,代码跟下面的写入数据差不多
with adoquery do begin
while not Eof do
begin
for i:=1 to 10 do
begin
if stringgrid.Cells[i,0]= trim(FieldByName('fields').Value) then
begin
if FieldByName('fields').Value='CHN' then
begin
stringgrid.Cells[i,1]:=FieldByName('fields').Value;
stringgrid.Cells[i,2]:=floattostr(FieldByName('fields').Value);
end;
if FieldByName('fields').Value='CHN' then
begin
stringgrid.Cells[i,3]:=FieldByName('CHN').Value;
stringgrid.Cells[i,4]:=floattostr(FieldByName('CHN').Value);
end;
end;
end;
next;
end;
end;
是明确字段才这样写的。你自己可能要看情况。
接着是将修改后的数据返回数据库,就先前发的代码了。
RefashMX函数不过是一段insert代码而已。
 
TO:CHRISTIN;
其实你说的我已经知道了,大概意思你不是很了解。
我的从表已经填入STRINGGRID ,现在要修改STRINGGRID,再提交数据库,修改从表内容。
我现在是先删除从表内容,然后再插入数据库。
不过有一个问题,我的从表有一个累加字段,如果是删除的是最后几条内容好办,如果
是中间的几条记录,那么从新提交后就是最后几条记录了!
所以希望是用UPDATE来写这个过程。
而不是删除后在插入!
 
顶部