新聞中心
在Delphi中使用數(shù)據(jù)庫是一項(xiàng)非常有用的技能。如果你不了解如何操作數(shù)據(jù)庫,那么本文提供的30個步驟將會幫助你掌握Delphi中的數(shù)據(jù)庫創(chuàng)建技能。下面就跟我一起來看看吧!

第1步:打開Delphi的IDE(集成開發(fā)環(huán)境)。
第2步:在項(xiàng)目選項(xiàng)卡中選擇新建項(xiàng)目。
第3步:選擇Delphi VCL應(yīng)用程序,并單擊“下一步”。
第4步:接下來,輸入項(xiàng)目名稱,例如“myDBproject”,并選擇項(xiàng)目的保存位置。單擊“下一步”。
第5步:現(xiàn)在你將會看到一個新的窗口,這是一個空的主窗口。在窗口上添加一個按鈕,用于連接到數(shù)據(jù)庫。單擊“按鈕”按鈕,然后單擊運(yùn)行圖標(biāo)來顯示主窗口。
第6步:在工具欄上選擇“TADOConnection”控件,將其拖放到主窗口中。
第7步:單擊剛剛添加的TADOConnection控件,右側(cè)將會顯示一個屬性窗口。在“連接字符串”字段中輸入與你的數(shù)據(jù)庫對應(yīng)的連接字符串。
第8步:在工具欄中選擇“TADOTable”控件,將其拖放到主窗口中。
第9步:選擇一個字段,然后在屬性窗口中找到“連接”字段,并選擇剛剛創(chuàng)建的TADOConnection。
第10步:在“表名”字段中輸入數(shù)據(jù)庫中的表名,并按Enter鍵。
第11步:現(xiàn)在,點(diǎn)擊“TADOTable”控件下的“Active”屬性來連接到數(shù)據(jù)庫并激活所選表。
第12步:在主窗口中添加一個列表框控件來顯示表中的數(shù)據(jù)。
第13步:在工具欄中選擇“TDataSource”控件并將其拖動到主窗口中。
第14步:點(diǎn)擊TDataSource控件并在右側(cè)屬性窗口中找到“DataSet”字段并選擇剛剛創(chuàng)建的TADOTable控件。
第15步:雙擊列表框控件以打開代碼編輯器。
第16步:在代碼編輯器中添加以下代碼:
ListBox1.Clear;
while not (MyTable.EOF) do
begin
ListBox1.Items.Add(MyTable.FieldByName(‘MyColumn’).AsString);
MyTable.Next;
end;
在這里,“MyTable”是TADOTable控件的名稱,“MyColumn”是字段名。
第17步:單擊運(yùn)行鍵,并看到列表框中顯示來自數(shù)據(jù)庫的數(shù)據(jù)。
第18步:現(xiàn)在添加一個編輯框,以便用戶可以在查看數(shù)據(jù)后在數(shù)據(jù)庫中更改數(shù)據(jù)。
第19步:雙擊編輯框并為其添加以下代碼:
MyTable.Edit;
MyTable.FieldByName(‘MyColumn’).AsString := Edit1.text;
MyTable.Post;
在這里,“MyColumn”是字段名,而“Edit1.text”是用戶在編輯框中改變的值。
第20步:現(xiàn)在,添加另一個按鈕,并在單擊按鈕時向數(shù)據(jù)庫添加新數(shù)據(jù)。
第21步:雙擊該按鈕,為其添加以下代碼:
MyTable.Append;
MyTable.FieldByName(‘MyColumn1’).AsString := …
MyTable.FieldByName(‘MyColumn2’).AsString := …
MyTable.Post;
在這里,“MyColumn1”和“MyColumn2”是要向其添加數(shù)據(jù)的字段名。
第22步:為編輯框和按鈕添加標(biāo)簽以說明其用途。
第23步:現(xiàn)在在數(shù)據(jù)庫中刪除數(shù)據(jù)。
第24步:添加一個刪除按鈕,打開其編輯器并為其添加以下代碼:
MyTable.Delete;
第25步:現(xiàn)在添加一個搜索框以查找與特定關(guān)鍵字匹配的數(shù)據(jù)。
第26步:為搜索框添加搜索按鈕。
第27步:添加以下代碼:
MyTable.Locate(‘MyColumn’, Edit1.Text, []);
在這里,“MyColumn”是要搜索的字段名。
第28步:添加一個過濾器,以便按照指定的條件來過濾數(shù)據(jù)。
第29步:為過濾器添加一個過濾按鈕。
第30步:添加以下代碼:
MyTable.Filter := ‘MyColumn1 > 10’;
MyTable.Filtered := true;
在這里,“MyColumn1 > 10”是要過濾的條件。
現(xiàn)在你已經(jīng)完成了Delphi中的數(shù)據(jù)庫創(chuàng)建教程的30個步驟。這些步驟將幫助你在Delphi中操縱數(shù)據(jù)庫,無論你是初學(xué)者還是有經(jīng)驗(yàn)的開發(fā)人員都能受益于它們。所以,盡管試試吧!
相關(guān)問題拓展閱讀:
- 在Delphi中如何動態(tài)創(chuàng)建dbf數(shù)據(jù)庫
- delphi7中怎樣實(shí)現(xiàn)自動創(chuàng)建數(shù)據(jù)源并連接sql server數(shù)據(jù)庫
在Delphi中如何動態(tài)創(chuàng)建dbf數(shù)據(jù)庫
以下是一個示例。供參考。 (它是由 dBASEvierer 自動生成的單元)
unit 1;
{***********************************************************************}
{ }
{ The following is a function that creates one or more tables in the }
{ folder “aDbase” specified as a parameter. If the tables are}
{ successfully created and opened, True is returned. “AOwner” is a }
{ component that is to own Table0 used to define the various tables. }
{ }
{ This function may be used as is or the appropriate sections may be }
{ copied into code of the writer’s choice. }
{ }
{ Code generated by dBASEviewer, written by Douglas P. Baird}
{ Please give a credit if this code is used. }
{ }
{***********************************************************************}
interface
uses classes;
function CreateTables(aDbase: string; AOwner: TComponent): Boolean;
implementation
uses dbtables, SysUtils, db, Dialogs;
function CreateTables(aDbase: string; AOwner: TComponent): Boolean;
var
Table0: TTable;
begin
Table0 := TTable.Create(AOwner);
{ Create 1.DBF }
with Table0 do begin
try
DatabaseName := aDbase;
TableName := ‘1.DBF’;
TableType := ttDBase;
TableLevel := 7;
with FieldDefs do begin
Clear;
with AddFieldDef do begin
Name := ‘自動’;
DataType := ftAutoInc;
end;
with AddFieldDef do begin
Name := ‘日期’;
DataType :=ftDateTime;
end;
with AddFieldDef do begin
Name := ‘分類’;
DataType :=ftString;
Size := 16;
end;
with AddFieldDef do begin
Name := ‘知識主題’;
DataType :=ftString;
Size := 60;
end;
with AddFieldDef do begin
Name := ‘索引關(guān)鍵字’;
DataType :=ftString;
Size := 20;
end;
with AddFieldDef do begin
Name := ‘知識內(nèi)容’;
DataType :=ftMemo;
end;
with AddFieldDef do begin
Name := ‘提交’;
DataType :=ftBoolean;
end;
with AddFieldDef do begin
Name := ‘blob’;
DataType := ftTypedBinary;
end;
with AddFieldDef do begin
Name := ‘ext’;
DataType :=ftString;
Size := 5;
end;
end;
{od with}
IndexDefs.Clear;
with IndexDefs.AddIndexDef do begin
Name := ‘分類’;
Fields := ‘分類’;
Options := ;
end;
{od with}
with IndexDefs.AddIndexDef do begin
Name := ‘日期’;
Fields := ‘日期’;
Options := ;
end;
{od with}
except
on E: Exception do begin
MessageDlg(E.Message,mtError,,0);
Result := False;
Exit;
end;
{od on}
end; {try}
Result := True;
try
CreateTable;
except
on E: Exception do begin
MessageDlg(‘Error creating table. ‘+E.Message,mtError,,0);
Result := False;
end;
{od on}
end; {try}
end;
{od with Table0}
Table0.Free;
end;
delphi7中怎樣實(shí)現(xiàn)自動創(chuàng)建數(shù)據(jù)源并連接sql server數(shù)據(jù)庫
delphi 通過odbc連接sql server
一、數(shù)據(jù)庫的準(zhǔn)備
打開“開始/程序/Microsoft SQL Server/企業(yè)管理器”展開“控制臺根目錄”的目錄,在標(biāo)有“數(shù)據(jù)庫”的目錄項(xiàng)上點(diǎn)擊右鍵,再點(diǎn)“新建數(shù)據(jù)庫”,如“TEST”。再右鍵點(diǎn)擊表,新建表,如“TABLE1”。
二、設(shè)置ODBC數(shù)據(jù)源
打開“開始/設(shè)置/控制面板/管理工具/數(shù)據(jù)源(ODBC)”,在“User DSN”下,點(diǎn)Add,選擇“SQLServer”,完成,輸入數(shù)據(jù)源名稱,如“TEST-ODBC”和SQL Server服務(wù)器名稱,后面一直點(diǎn)“下一步”到最后一頁,再點(diǎn)“Test data source..”檢測設(shè)置是否正確。如果正確顯示“tests completed successfully!”的信息。
三、設(shè)置數(shù)據(jù)引擎
打開“開始/程序/Borland Delphi7/BDE Administrator”,\在”Databases”頁面下,可以看到,剛才設(shè)置的ODBC數(shù)據(jù)源“TEST-ODBC”,右鍵,選擇“OPEN”,出現(xiàn)輸入”user name”、”Password”的對話框,輸入用戶名Administrator及密碼后點(diǎn)擊“OK”使它有效。這時,如果連接正常,右側(cè)的項(xiàng)目名稱的字體變成粗體。注意:如要修改參數(shù)值,應(yīng)先鼠標(biāo)右鍵點(diǎn)擊左側(cè)的數(shù)據(jù)庫名稱,選擇“Close”,在修改參數(shù)值后,用鼠標(biāo)右鍵點(diǎn)擊左側(cè)的數(shù)據(jù)庫名稱,選擇“Apply”保存,再選擇“OPen”使新的參數(shù)值生效。
關(guān)于delphi 數(shù)據(jù)庫如何創(chuàng)建的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
本文標(biāo)題:Delphi數(shù)據(jù)庫創(chuàng)建教程:簡單易懂的30步驟(delphi數(shù)據(jù)庫如何創(chuàng)建)
瀏覽地址:http://m.fisionsoft.com.cn/article/coissdh.html


咨詢
建站咨詢
