各位朋友,小弟在多个窗体里面都有类似下面的代码,请问有没有朋友能帮忙将以下的代码做成一个过程,然后用过程的方式来调用,可以不用每一个窗体都写上一个类似下面的过程来UPDATE表,诚心请各位大虾赐教,谢谢!!!
with Ado_Master_Frp do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
begin
close;
sql.Clear;
sql.Text := 'update SO_1 set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(DBEditEh3.Text);
parameters.ParamByName('Type_of_sample').Value := trim(ComboBox1.Text);
parameters.ParamByName('topeople').Value := trim(ComboBox2.Text);
parameters.ParamByName('Brand_name').Value := trim(ComboBox3.Text);
parameters.ParamByName('Yarn_quality').Value := trim(ComboBox4.Text);
parameters.ParamByName('Yarn_quality2').Value := trim(ComboBox5.Text);
ExecSQL;
end;
with Ado_Query do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
begin
close;
sql.Clear;
sql.Text := 'update 表2 set aa=:aa, helo=:helo' + QuotedStr(edit1.Text);
parameters.ParamByName(aa').Value := trim(ComboBox1.Text);
parameters.ParamByName('helo').Value := trim(ComboBox2.Text);
ExecSQL;
end;
with Ado_Master_Frp do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
begin
close;
sql.Clear;
sql.Text := 'update SO_1 set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(DBEditEh3.Text);
parameters.ParamByName('Type_of_sample').Value := trim(ComboBox1.Text);
parameters.ParamByName('topeople').Value := trim(ComboBox2.Text);
parameters.ParamByName('Brand_name').Value := trim(ComboBox3.Text);
parameters.ParamByName('Yarn_quality').Value := trim(ComboBox4.Text);
parameters.ParamByName('Yarn_quality2').Value := trim(ComboBox5.Text);
ExecSQL;
end;
with Ado_Query do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
begin
close;
sql.Clear;
sql.Text := 'update 表2 set aa=:aa, helo=:helo' + QuotedStr(edit1.Text);
parameters.ParamByName(aa').Value := trim(ComboBox1.Text);
parameters.ParamByName('helo').Value := trim(ComboBox2.Text);
ExecSQL;
end;
解决方案 »
- 个位,delphi能否实现网络截包? C语言可以么?
- Delphi里有没有像C 里的<表达式1>?<表达式2>:<表达式3>这样的三目运算呀?
- 关于DBGRID和DBCTRLGRID的问题
- delphi中将一个如何将一个word文件或图片写入ms_sql sever数据库
- “无效的类别字符串”是什么意思?
- 关于指针问题,高手进来!在线等待!!
- 关于如何使用消息的问题??
- 请教:通过TPrinterSetupDialog设置了打印机后, 如何得到当前打印机的纸张大小(A3还是A4等等)??
- TStoreProc中可不可以使用ftBlob类型的参数?
- 谁能解决????????????
- 将数据表字段内容显示至combobox中的问题,再提问
- delphi 导出Excel时出问题
procedure BuildReport(typepeople,topeople,brandname,quality1,quality2,str1,str2:string);
Var
tempquery: TADOQuery;
Begin
tempquery := TADOQuery.Create(nil); try
With tempquery Do
Begin
close;
sql.Clear;
sql.Text := 'update SO_1 set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(str1);
parameters.ParamByName('Type_of_sample').Value := trim(typepeople);
parameters.ParamByName('topeople').Value := trim(topeople);
parameters.ParamByName('Brand_name').Value := trim(brandname);
parameters.ParamByName('Yarn_quality').Value := trim(quality1);
parameters.ParamByName('Yarn_quality2').Value := trim(quality2);
ExecSQL; close;
sql.Clear;
sql.Text := 'update 表2 set aa=:aa, helo=:helo' + QuotedStr(str2);
parameters.ParamByName('aa').Value := trim(typepeople);
parameters.ParamByName('helo').Value := trim(topeople);
ExecSQL;
End;
Finally
tempquery.Free;
End;
End;
例如:在基类中定义 procedure LogData(value: string); //用来记录操作日志 还有常规定义为
procedure ExecSql(aSQL: string);
var
lQuery: TAdoQuery;
begin
lQuery := TAdoquery.create(nil);
try
lQueyr.SQL.Text := aSQL;
lQuery.Execute;
finally
lQuery.Free;
end;
end;
由于只是执行更新语句并不返回结果集, 这样就好了, 还可以定义成函数用来返回是否成功等.
调用时只需传入执行的SQL语句即可
procedure Update;
var
lSQL: string;
begin
lSQL := 'update SO_1 set ' +
' Type_of_sample = ' + ComboBox1.Text +
',topeople = ' + ComboBox2.Text +
',Brand_name = ' + ComboBox3.Text +
.....
' Where 你的条件 ';
ExecSQL(lSQL);
end;
这样看上去是不是好一点了, 另外为了以后维护起见,控件的命名最好应该起的有意义点
2、因为Update的每个表都不同,所以就要设置一个变量:Table_Name,主要是给用户传入一个表名
3、因为Where的每个条件都不同,所以就要设置一个变量:Where_Value,主要是用来给用户传入Where的条件值。
4、Sql语句里面,每一次Update的字段的数量不同,有的时候是一个,有的时候是两个,有的时候是十几个,所以自己觉
得要设置一个动态的数组来完成该动作,但具体的实现方便不懂实现,只有思路
5、因为每次传入的Update的字段数量不同,所以对应的parameters.ParamByName('字段').value也应该有很多句,自己的思路是,可以做一个循环
for i:=0 to 用户传入的数量 do
parameters.ParamByName(动态数组[i]]).value:=用户传入的字段然后,每个窗体想Update多个字段的时候,只需要类似下面的调用就可以了:
1、Update的只有一个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,Where的条件值,要Update的表名,要循环1次);
2、Update的只有两个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,字段1,Where的条件值,要Update的表名,要循环2次);
3、Update的N个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,字段1,.....,字段N,Where的条件值,要Update的表名,要循环N次); with Adoquery do
begin
close;
sql.Clear;
sql.Text := 'update Table1 set dye_id=:dye_id where name=' + QuotedStr(edit1.text);
parameters.ParamByName('dye_id').Value := trim(edit2.text);
ExecSQL;
end;
with Ado_Master_Frp do
begin
close;
sql.Clear;
sql.Text := 'update SO_Sample_Order1 set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name where Sample_order_no=' + QuotedStr(DBEditEh3.Text);
parameters.ParamByName('Type_of_sample').Value := trim(ComboBox1.Text);
parameters.ParamByName('topeople').Value := trim(ComboBox2.Text);
parameters.ParamByName('Brand_name').Value := trim(ComboBox3.Text);
ExecSQL;
end;
with ado_tmp do
begin
close;
sql.Clear;
sql.Text := 'update so_dkcbd1 set to_people=:to_people, rc=:rc, ZHI=:ZHI, sbzl=:sbzl, client=:client,' +
'jbrc=:jbrc,zbr=:zbr,yy=:yy where dh=' + QuotedStr(trim(dh));
parameters.ParamByName('to_people').Value := trim(ComboBox1.Text);
parameters.ParamByName('rc').Value := trim(rc);
parameters.ParamByName('ZHI').Value := trim(ComboBox2.Text);
parameters.ParamByName('sbzl').Value := trim(ComboBox3.Text);
parameters.ParamByName('client').Value := trim(ComboBox4.Text);
Parameters.ParamByName('jbrc').Value := trim(jbrc);
Parameters.ParamByName('zbr').Value := trim(ComboBox5.Text);
Parameters.ParamByName('yy').Value := trim(ComboBox6.Text);
ExecSQL;
end;
然后,每个窗体想Update多个字段的时候,只需要类似下面的调用就可以了:
1、Update的只有一个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,新值1,Where的条件值,要Update的表名,要循环1次);
2、Update的只有两个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,字段2,新值1,新值2,Where的条件值,要Update的表名,要循环2次);
3、Update的N个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,字段1,.....,字段N,新值1,新值2,新值N,Where的条件值,要Update的表名,要循环N次); 期待论坛上的各位朋友都能看一下,看看这样复杂的过程有没有办法能做得出来,谢谢了!
Name: string;
Value: Variant;
end;定义生成该Record的方法
function NewFieldValue(Name: string; Value: Variant);
begin
Result.Name := Name;
Result.Value := Value;
end;定义你的功能方法
procedure TheProc(UpdateFields: array of TFieldValue;OtherParam...)
begin
..循环UpdateFields...
end;调用
TheProc([NewFieldValue('Field1', fieldvalue1),
NewFieldValue('Field2', fieldvalue2),
...], OthterParamValue...);
但是會增加程式執行的負擔
然后,每个窗体想Update多个字段的时候,只需要类似下面的调用就可以了:
1、Update的只有一个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,Where的条件值,要Update的表名,要循环1次);
2、Update的只有两个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,字段1,Where的条件值,要Update的表名,要循环2次);
3、Update的N个字段,那么就写成
Update过程名(当前窗体的adoquery,字段1,字段1,.....,字段N,Where的条件值,要Update的表名,要循环N次);
為什么不先循環,然后依次UPDATE呢?
TFieldValue = record
Name: string;
Value: Variant;
end;//定义生成该Record的方法
function NewFieldValue(Name: string; Value: Variant): TFieldValue;
begin
Result.Name := Name;
Result.Value := Value;
end;procedure MyCommProc(MasterTable, QueryTable: string; MasterFields, QueryFields: array of TFieldValue);
var
i: Integer;
begin
with Ado_Master_Frp do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
begin
close;
sql.Clear;
sql.Text := 'update ' + MasterTable + ' set Type_of_sample=:Type_of_sample, topeople=:topeople, Brand_name=:Brand_name, Yarn_quality=:Yarn_quality, Yarn_quality2=:Yarn_quality2' + QuotedStr(DBEditEh3.Text);
for i := 0 to High(MasterFields) do
parameters.ParamByName(MasterFields[i].Name).Value := trim(MasterFields[i].Value);
ExecSQL;
end;
with Ado_Query do //原来的Ado_Master这个ADO就主要用来保存数据,这个Ado_Master_Frp主要用来生成报表
begin
close;
sql.Clear;
sql.Text := 'update ' + QueryTable + ' set aa=:aa, helo=:helo' + QuotedStr(edit1.Text);
for i := 0 to High(MasterFields) do
parameters.ParamByName(MasterFields[i].Name).Value := trim(MasterFields[i].Value);
ExecSQL;
end;
end;
我第一次回复给的是一种思路,第二次回复怕你不理解加的一些代码片断,剩下的就是拼Sql串的问题,以为你明白了。或者是我根本理解错你的问题了。