Presentation is loading. Please wait.

Presentation is loading. Please wait.

資料庫操作2.

Similar presentations


Presentation on theme: "資料庫操作2."— Presentation transcript:

1 資料庫操作2

2 Table元件 完全使用Table (BDE元件)來操作資料庫 開啟Open與關閉Close Bookmark 資料移動
新增、刪除、修改與查詢

3 Table元件-開啟與關閉 Table設定: DatabaseName TableName
Active (相當於Table1->Open) 資料欄位顯示 Table1->FieldValues[“欄位名稱”]; Table1->FieldByName(“欄位名稱”)->型別轉換; Table1->Fields->Fields[索引]->型別轉換; 關閉資料庫 Table1->Close();

4 Table元件-資料錄的移動 Table1->First(); //首筆 Table1->Last(); //末筆
Table1->Next(); //下一筆 Table1->Prior(); //上一筆 Table1->MoveBy(n); //向下或上移動n筆 Table1->Bof; Table1->Eof; Table1->GetBookmark(); //取得書籤 Table1->GotoBookmark(); //移動致書籤處 Table1->FreeBookmark(); //釋放書籤 Ex17-3a, p.17-31

5 Table元件-新增、修改與刪除 Table1->Insert(); //新增 Table1->Delete(); //刪除
Table1->Append(); //附加新增 Table1->Edit(); //修改 Table1->Post(); //確定修改

6 Table元件-新增、修改與刪除2 Table1->Edit();
Table1->FieldValues["姓名"]="楊文德"; Table1->FieldValues["學號"]="D "; Table1->FieldValues["性別"]=False; Table1->Post(); // 顯示 Table1->IndexFieldNames=“學號”; //資料庫內有設定索引的欄位 if (Table1->FindKey(new TVarRec("D "),0)) { Label1->Caption=Table1->FieldValues["姓名"]; Label2->Caption=Table1->Fields->Fields[0]->AsString; }

7 Table元件-查詢 不用索引檔 Table1->Locate(); Table1->Loookup(); 利用索引檔
Table1->FindKey(); Table1->SetKey(); Table1->GotoKey();

8 Table元件-Locate查詢 //設定尋找方式 TLocateOptions searchoptions;
searchoptions << loPartialKey; If (Table1->Locate(“姓名”,”楊文德”,searchoptions)) { Label1->Caption=Table1->FieldValues["姓名"]; Label2->Caption=Table1->Fields->Fields[0]->AsString; }

9 Table元件-Lookup查詢 Locate 可以檢查沒有資料
Label1->Caption=Table1->Lookup("姓名","楊文德","姓名"); Label2->Caption=Table1->Lookup("姓名","楊文德","學號"); Locate 可以檢查沒有資料

10 TQuery元件 BDE元件 可以下達SQL指令 速度快(相對於Table)

11 TQuery元件

12 TQuery元件 Query1->SQL->Text 無法修改資料  RequestLive屬性

13 TQuery元件 加上參數 SQL語法 執行階段才執行SQL
void __fastcall TForm1::FormCreate(TObject *Sender) { Query1->Close(); Query1->SQL->Clear(); Query1->SQL->Add("select * from student"); Query1->Open(); // 查尋 //Query1->ExecSQL(); //新增 刪除 修改 } 加上參數 SQL語法

14 TQuery元件 SQL語法: 新增:insert into 表單 (欄位1,欄位2) values (值1,值2)
刪除:delete from 表單 where 條件 修改:update 表單 set 欄位=值 where 條件 查詢:跨表查詢 select a.欄位,b.欄位 from 表單1 as a, 表單2 as b where 條件 Ex 17-4eg, p.17-50

15 DBChart元件

16 DBChart元件

17 DBChart元件 設定Series1的DataSource, XLabelSource, YValue.ValuesSource…

18 Master/Detail QuickReport

19 Master/Detail ADOTable1 ConnectionString TableName ADOTable2
MasterSource MasterFields

20 QuickReport 設定DataSet

21 QuickReport 加入QRDBText 設定DataSet DataField 右鍵Preview

22 QuickReport QRExpr  Expression屬性

23 QuickReport 程式驅動報表: QuickRep1->Preview(); QuickRep1->Print();

24 QuickReport 加入SubDetail band 設定DataSet=ADOTable2 Master=QuickRep1
ConnectionString TableName 加入SubDetail band 設定DataSet=ADOTable2 Master=QuickRep1 HasChild=true ADOTable2 ConnectionString TableName MasterSource MasterFields

25 相對路徑: ShowMessage(ExtractFilePath(ParamStr(0)));
void __fastcall TForm1::FormCreate(TObject *Sender) { String pth; pth=ExtractFilePath(ParamStr(0))+"\student.mdb"; ADOTable1->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=“ + pth +";Persist Security Info=False"; ADOTable1->TableName="table1"; ADOTable1->Open(); }

26 InstallShield 不然會出現缺少vcl60.bpl的訊息
project compiler 成 standalone 以下是設定的方法: (1) 選擇 Project/Options 顯示 Project Options 的 dialog (2) 選擇 Packages TAB 把 build with runtime packages 這個打勾取消 (3) 選擇 Linker TAB 把 Use dynamic RTL 這個打勾取消 不然會出現缺少vcl60.bpl的訊息


Download ppt "資料庫操作2."

Similar presentations


Ads by Google