//******************************************************************************
// 函数功能: 执行SQL查询语句,影响ClientDataSet控件
// 函数名称: cf_dbSelectSQL
// 函数参数: var oClientDataSet:TClientDataSet; 对象(ClientdataSet)
// sSQL String SQL结构化语言
// sCon String 数据连接字符串
// 返回值: 返回是否操作成功(Boolean);
//******************************************************************************
function cf_dbSelectSQL(var oClientDataSet:TClientDataSet;sCon,sSQL: string): Boolean;overload;
var
oADOQuery:TADOQuery;
iField:Integer;
begin
Result:=False;
if (oClientDataSet=nil) then Exit;
if (Trim(sCon)='') or (Trim(sSql)='') then Exit;
try
oADOQuery:=TADOQuery.Create(nil);
oClientDataSet.Fields.Clear;
oADOQuery.SQL.Clear;
oADOQuery.SQL.Add(sSQL);
oADOQuery.ConnectionString:=sCon;
oADOQuery.Open;
if oADOQuery.Fields.Count>0 then
begin
for iField:=0 to oADOQuery.Fields.Count-1 do
begin
with oClientDataSet.FieldDefs.AddFieldDef do
begin
Name:=oADOQuery.Fields[iField].DisplayName;
DataType:=oADOQuery.Fields[iField].DataType;
end;
end;
//建立结构。
oClientDataSet.CreateDataSet;
oClientDataSet.Open;
if not oADOQuery.IsEmpty then
begin
//循环加入列表中
oADOQuery.First;
while not oADOQuery.Eof do
begin
oClientDataSet.Append;
for iField:=0 to oADOQuery.Fields.Count-1 do
begin
oClientDataSet.FieldByName(oADOQuery.Fields[iField].DisplayName).AsString:=oADOQuery.FieldByName(oADOQuery.Fields[iField].DisplayName).AsString;
end;
oClientDataSet.Post;
oADOQuery.Next;
end;
end;
end;
oClientDataSet.Open;
Result:=True;
except
Result:=False;
end;
end;