国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

用AdoDataSet實現(xiàn)數(shù)據(jù)表的導(dǎo)入導(dǎo)出

2019-11-18 18:40:01
字體:
供稿:網(wǎng)友
Delphi中的AdoDataSet是支持ADO訪問的主要組件,它支持從數(shù)據(jù)表直接獲取數(shù)據(jù),支持用SQL語句獲取數(shù)據(jù)。最重要的是,它定義和實現(xiàn)了兩個重要的例程:
PRocedure LoadFromFile(const FileName: WideString);它從文件中加載數(shù)據(jù)集。
procedure SaveToFile(const FileName: String = ''; Format: TPersistFormat = pfADTG);它將數(shù)據(jù)集保存到文件中。Format確定文件中數(shù)據(jù)集的保存格式,可以使用的有pfADTG (Advanced Data Tablegram format)、pfxml(Extendable Markup Language)。
因此AdoDataSet是實現(xiàn)導(dǎo)入導(dǎo)出的良好的基礎(chǔ)。
 
1.  數(shù)據(jù)表的導(dǎo)出
導(dǎo)出數(shù)據(jù)表的操作如下:
1)打開數(shù)據(jù)表,設(shè)置需要導(dǎo)出的條件;
2)使用AdoDataSet,調(diào)用SaveToFile導(dǎo)出記錄;
下面是一個導(dǎo)出操作的示例(假定導(dǎo)出指定數(shù)據(jù)表的全部記錄)。
procedure  ExportData(strFileName, strTableName: string);
begin
with AdoDataSet1 do
begin
Close;
CommandText := ‘select * from ’ + strTableName;
Open;
SaveToFile(strFileName);
Close;
end;
end;
2.?dāng)?shù)據(jù)表的導(dǎo)入
下面是一個導(dǎo)入操作的示例(假定存在相同主鍵記錄時更新目的表;假定數(shù)據(jù)表為單主鍵字段,且其字段類型為字符串型)。
Procedure  ImportData(strFileName, strTableName, strKeyFieldName: string);
begin
with AdoDataSet1 do
begin
Close;
LoadFromFile(strFileName);
First;
While not eof do
begin
StrKeyValue := FieldByName(strKeyFieldName).AsString;
If RecordInDest(strTableName, strKeyFieldName, strKeyValue) then
begin
AdoDataDest.Close;
AdoDataSetDest.CommandText := Format(‘select * from %s where %s=%s’,[strTableName, strKeyFieldName, QuotedStr(strKeyValue)]);
AdoDataSetDest.Open;
AdoDataSetDest.First;
AdoDataSetDest.Edit;
for I:=0 to FieldList.Count-1 do
AdoDataSetDest.Fields[I] := Fields[I];
AdoDataSetDest.Post;
end
else         // 添加記錄
begin
AdoDataDest.Close;
AdoDataSetDest.CommandText := Format(‘select * from %s where 1=0’,[strTableName]);  // 獲取字段列表
AdoDataSetDest.Open;
AdoDataSetDest.Insert;
for i:=0 to FieldList.Count-1 do
AdoDataSetDest.Fields[i] := Fields[i];
AdoDataSetDest.Post;
end;
Next;
end;
end;
 
// 判斷指定主鍵值的記錄在表中是否存在
function RecordInDest(strTableName, strKeyFieldName, strKeyValue: string): boolean;
begin
with AdoQuery1 do
begin
Close;
SQL.Clear;
SQL.Add(Format(‘select count(*) from %s where %s=%s, [strTableName, strKeyFieldName, QuotedStr(strKeyValue)]));
Open;
result := Fields[0].AsInteger > 0;
Close;
end;
end;
 
如果對數(shù)據(jù)表的情況進行進一步的考慮,并結(jié)合更周密的導(dǎo)入導(dǎo)出方案,比如導(dǎo)入指定字段、導(dǎo)入指定字段、導(dǎo)入指定記錄等等,對導(dǎo)入導(dǎo)出過程進行更詳細的控制,就可以實現(xiàn)強大的、通用的數(shù)據(jù)表的導(dǎo)入導(dǎo)出工具。

上一篇:組件制作之三(圖形控件)

下一篇:組件制作之一(概念)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 即墨市| 新竹市| 望城县| 泰顺县| 长治县| 普安县| 玉门市| 河间市| 宕昌县| 隆昌县| 浮梁县| 西贡区| 宜都市| 宣化县| 房产| 齐河县| 苍山县| 双流县| 吴忠市| 克什克腾旗| 广宗县| 大安市| 中牟县| 永泰县| 太仓市| 商丘市| 清镇市| 林周县| 营口市| 湘阴县| 岐山县| 卓尼县| 嵊泗县| 泸溪县| 腾冲县| 旬邑县| 高淳县| 永靖县| 库车县| 慈溪市| 黑水县|