Delphi 判断ClientDataSet控件的UpdateStatus属性类型为 (usUnmodified, usModified, usInserted, usDeleted))
2011-11-26 12:48
956 查看
Delphi 判断ClientDataSet控件的UpdateStatus属性类型为 (usUnmodified, usModified, usInserted, usDeleted))
根据ClientDataSet控件的UpdateStatus属性类型为(usUnmodified, usModified, usInserted, usDeleted),解析并拼成相应的增、删和改操作语句:
实例如下:
// ATableName : 将要修改的表名
// APrimaryKey : 表的主键值
// AIsMulti : 数据中包含的表的个数
// ADelta : 更新的数据集
procedure TForm1.Save(const ATableName, APrimaryKey, AIsMulti: WideString;
ADelta: OleVariant; var ErrorMsg: WideString);
var
ActualFieldList: TStringList;
PrimaryFieldList: TStringList;
i: Integer;
FieldNameList: string; //字段名称列表
FieldValueList: string; //字段值列表
FieldNameValueList: string; //字段值=名称列表
SetValueList: string; //Set字段值=名称列表, update的Sql用
SqlStr: string;
//返回欲修改表的字段名称列表
procedure GetFieldNameList(const ATableName: string);
var
QueryStr: string;
begin
QueryStr := 'select * from ' + ATableName + ' where 1 = 0 ';
try
FClientDataSet.Data := FDBoperation.cdsGetDataForRead(QueryStr, ErrorMsg);
except
Exit;
end;
FClientDataSet.GetFieldNames(ActualFieldList);
end;
//返回欲修改表的 主键字段名称=值串
function GetPKValueList: string;
var
j: Integer;
ValueStr: string;
ResultStr: string;
begin
ResultStr := ' ';
for j := 0 to PrimaryFieldList.Count - 1 do
begin
with FClientDataSet do
begin
case FieldByName(PrimaryFieldList[j]).DataType of
ftString, ftDate, ftDateTime:
ValueStr := QuotedStr(FieldByName(PrimaryFieldList[j]).AsString);
ftInteger:
ValueStr := FieldByName(PrimaryFieldList[j]).AsString;
end;
end;
if j > 0 then
ResultStr := ResultStr + ' and ';
ResultStr := ResultStr + PrimaryFieldList[j] + ' = ' + ValueStr;
end;
Result := ResultStr;
end;
begin
if AIsMulti = '1 ' then //Delta中仅包含一个表的数据
begin
FDBoperation.cdsUpdateDate(ATableName, ADelta, ErrorMsg);
end
else
begin //Delta中包含多个表的数据
ActualFieldList := TStringList.Create;
PrimaryFieldList := TStringList.Create;
try
{=== 得到欲修改表的字段名称列表 ===}
GetFieldNameList(ATableName);
FClientDataSet.Data := ADelta;
FClientDataSet.First;
with FClientDataSet do
begin
while not Eof do
begin
case UpdateStatus of
usModified:
begin
SetValueList := ' Set ';
for i := 0 to FieldCount - 1 do
begin
//是欲修改表的字段之一
if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then
begin
case Fields[i].DataType of
ftString, ftDate, ftDateTime:
begin
if not Fields[i].IsNull then
SetValueList := SetValueList + Fields[i].FieldName
+ '= ' + QuotedStr(Fields[i].AsString) + ', ';
end;
ftInteger:
begin
if not Fields[i].IsNull then
SetValueList := SetValueList + Fields[i].FieldName
+ '= ' + Fields[i].AsString + ', ';
end;
end;
end;
end;
System.Delete(SetValueList, Length(SetValueList), 1);
SqlStr := 'update ' + ATableName + SetValueList + ' where ' + FieldNameValueList;
end;
usInserted:
begin
FieldNameList := ' ';
FieldValueList := ' ';
for i := 0 to FieldCount - 1 do
begin
//是欲修改表的字段之一
if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then
begin
FieldNameList := FieldNameList + Fields[i].FieldName + ', ';
if Fields[i].IsNull then
FieldValueList := FieldValueList + ' null, '
else begin
case Fields[i].DataType of
ftString, ftDate:
begin
FieldValueList := FieldValueList + QuotedStr(Fields[i].AsString) + ', ';
end;
ftInteger:
begin
FieldValueList := FieldValueList + Fields[i].AsString + ', ';
end;
end;
end;
end;
end;
//删除末尾的逗号
System.Delete(FieldNameList, Length(FieldNameList), 1);
System.Delete(FieldValueList, Length(FieldValueList), 1);
SqlStr := 'insert into ' + ATableName + '( ' + FieldNameList + ') '
+ ' values ( ' + FieldValueList + ') ';
end;
usUnmodified, usDeleted:
begin
{=== 得到欲修改表的主键字段名称列表 ===}
PrimaryFieldList.CommaText := APrimaryKey;
{=== 得到欲修改表的主键字段名称=值串 ===}
FieldNameValueList := GetPKValueList;
if UpdateStatus = usDeleted then
SqlStr := 'delete from ' + ATableName + ' where ' + FieldNameValueList;
end;
end;
if UpdateStatus <> usUnmodified then
begin
try
FDBoperation.ExecSql(SqlStr, ErrorMsg);
except
raise;
end;
end;
Next;
end;
end;
finally
ActualFieldList.Free;
PrimaryFieldList.Free;
end;
end;
end;
根据ClientDataSet控件的UpdateStatus属性类型为(usUnmodified, usModified, usInserted, usDeleted),解析并拼成相应的增、删和改操作语句:
实例如下:
// ATableName : 将要修改的表名
// APrimaryKey : 表的主键值
// AIsMulti : 数据中包含的表的个数
// ADelta : 更新的数据集
procedure TForm1.Save(const ATableName, APrimaryKey, AIsMulti: WideString;
ADelta: OleVariant; var ErrorMsg: WideString);
var
ActualFieldList: TStringList;
PrimaryFieldList: TStringList;
i: Integer;
FieldNameList: string; //字段名称列表
FieldValueList: string; //字段值列表
FieldNameValueList: string; //字段值=名称列表
SetValueList: string; //Set字段值=名称列表, update的Sql用
SqlStr: string;
//返回欲修改表的字段名称列表
procedure GetFieldNameList(const ATableName: string);
var
QueryStr: string;
begin
QueryStr := 'select * from ' + ATableName + ' where 1 = 0 ';
try
FClientDataSet.Data := FDBoperation.cdsGetDataForRead(QueryStr, ErrorMsg);
except
Exit;
end;
FClientDataSet.GetFieldNames(ActualFieldList);
end;
//返回欲修改表的 主键字段名称=值串
function GetPKValueList: string;
var
j: Integer;
ValueStr: string;
ResultStr: string;
begin
ResultStr := ' ';
for j := 0 to PrimaryFieldList.Count - 1 do
begin
with FClientDataSet do
begin
case FieldByName(PrimaryFieldList[j]).DataType of
ftString, ftDate, ftDateTime:
ValueStr := QuotedStr(FieldByName(PrimaryFieldList[j]).AsString);
ftInteger:
ValueStr := FieldByName(PrimaryFieldList[j]).AsString;
end;
end;
if j > 0 then
ResultStr := ResultStr + ' and ';
ResultStr := ResultStr + PrimaryFieldList[j] + ' = ' + ValueStr;
end;
Result := ResultStr;
end;
begin
if AIsMulti = '1 ' then //Delta中仅包含一个表的数据
begin
FDBoperation.cdsUpdateDate(ATableName, ADelta, ErrorMsg);
end
else
begin //Delta中包含多个表的数据
ActualFieldList := TStringList.Create;
PrimaryFieldList := TStringList.Create;
try
{=== 得到欲修改表的字段名称列表 ===}
GetFieldNameList(ATableName);
FClientDataSet.Data := ADelta;
FClientDataSet.First;
with FClientDataSet do
begin
while not Eof do
begin
case UpdateStatus of
usModified:
begin
SetValueList := ' Set ';
for i := 0 to FieldCount - 1 do
begin
//是欲修改表的字段之一
if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then
begin
case Fields[i].DataType of
ftString, ftDate, ftDateTime:
begin
if not Fields[i].IsNull then
SetValueList := SetValueList + Fields[i].FieldName
+ '= ' + QuotedStr(Fields[i].AsString) + ', ';
end;
ftInteger:
begin
if not Fields[i].IsNull then
SetValueList := SetValueList + Fields[i].FieldName
+ '= ' + Fields[i].AsString + ', ';
end;
end;
end;
end;
System.Delete(SetValueList, Length(SetValueList), 1);
SqlStr := 'update ' + ATableName + SetValueList + ' where ' + FieldNameValueList;
end;
usInserted:
begin
FieldNameList := ' ';
FieldValueList := ' ';
for i := 0 to FieldCount - 1 do
begin
//是欲修改表的字段之一
if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then
begin
FieldNameList := FieldNameList + Fields[i].FieldName + ', ';
if Fields[i].IsNull then
FieldValueList := FieldValueList + ' null, '
else begin
case Fields[i].DataType of
ftString, ftDate:
begin
FieldValueList := FieldValueList + QuotedStr(Fields[i].AsString) + ', ';
end;
ftInteger:
begin
FieldValueList := FieldValueList + Fields[i].AsString + ', ';
end;
end;
end;
end;
end;
//删除末尾的逗号
System.Delete(FieldNameList, Length(FieldNameList), 1);
System.Delete(FieldValueList, Length(FieldValueList), 1);
SqlStr := 'insert into ' + ATableName + '( ' + FieldNameList + ') '
+ ' values ( ' + FieldValueList + ') ';
end;
usUnmodified, usDeleted:
begin
{=== 得到欲修改表的主键字段名称列表 ===}
PrimaryFieldList.CommaText := APrimaryKey;
{=== 得到欲修改表的主键字段名称=值串 ===}
FieldNameValueList := GetPKValueList;
if UpdateStatus = usDeleted then
SqlStr := 'delete from ' + ATableName + ' where ' + FieldNameValueList;
end;
end;
if UpdateStatus <> usUnmodified then
begin
try
FDBoperation.ExecSql(SqlStr, ErrorMsg);
except
raise;
end;
end;
Next;
end;
end;
finally
ActualFieldList.Free;
PrimaryFieldList.Free;
end;
end;
end;
相关文章推荐
- ClientDataSet通过UpdateStatus方法配合Delta属性查找已删除的记录
- 更改Delphi 中ClientDataSet中字段的数据类型
- delphi clientdataset判断字段存在是否存在
- delphi clientdataset判断字段存在是否存在
- Delphi中判断控件的详细类型
- Delphi中ClientDataSet浅析
- 解决类型“System.Web.UI.UpdatePanel”不具有名为“Gridview”的公共属性,
- Typed DataSet强类型Oracle的delete,update,insert
- JavaScript判断变量类型:typeof函数与constructor属性异同
- 【Android常用控件】EditText常用属性【二】:为文本输入框指定软键盘类型和软键盘回车键图标设置
- 关于UpdatePanel控件的UpdateMode属性的探讨
- 判断鼠标是否在某个控件范围内 ScreenToClient PtInRect
- delphi编程如何判断图片文件的真实类型?
- delphi控件属性大全-详解-简介
- WinForm控件开发之为控件的复杂属性提供类型转换器
- ClientDataSet的隐含功能------转载《Delphi 从入门到精通》
- Python 继承、多重继承、多态、isinstance判断类型、type()获取变量类型、dir()获取变量的所有属性
- Delphi中避免使用ClassName判断对象的类型
- ASP.NET2.0服务器控件之类型化样式属性
- ASP.NET判断控件类型