全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2339
推到 Plurk!
推到 Facebook!

用DAO方式建立Access數據庫的方法的疑問

尚未結案
alex_soong
一般會員


發表:2
回覆:3
積分:1
註冊:2004-11-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-07 11:36:46 IP:218.2.xxx.xxx 未訂閱
我有一個很簡單的項目,使用了Access數據庫,打算在程式啓動時檢測是否有數据庫,並建立數據庫。結果卻引來了一堆問題。 1.建立一個Access數據庫 const accessstring = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s'; procedure CreateDatabase(DBName: string); var DB: OleVariant; tempstr: string; begin DB := CreateOleObject('ADOX.Catalog'); try tempstr := format(accessstring,[DBName]); DB.Create(tempstr); finally DB:=Unassigned; end; end; 黨建立好數據庫之後,該如何再次打開這個數據庫並添加表呢?? 2.黨要建立一個新表時,縂要先查詢一次是否有同名的表單,但爲什麽總是連系統表單一起返回了,怎麽辦? type TAccessTableDef = record Name, DateCreated, LastUpdated, Description: string; end; pTaccessTableDef = ^TAccessTableDef; procedure GetTableDefs(const DBName: string; TableDefsList: Tlist); var DBEngine, DB: OleVariant; I: Longint; temppointer: pTaccessTableDef; begin try DBEngine := CreateOleObject('DAO.DBEngine.36'); DB := DBEngine.OpenDatabase(DBName); if Longint(DB.TableDefs.Count) > 0 then begin for I := 0 to Longint(DB.TableDefs.Count)-1 do begin new(temppointer); temppointer.Name := DB.TableDefs[I].Name; temppointer.DateCreated := DB.TableDefs[I].DateCreated; temppointer.LastUpdated := DB.TableDefs[I].LastUpdated; try temppointer.Description := DB.TableDefs[I].Properties['Description'].Value; except temppointer.Description := ''; end; TableDefsList.Add(temppointer); end; //for I := 0 to Longint(DB.TableDefs.Count)-1 do end; // if Longint(DB.TableDefs.Count) > 0 then finally DB := Unassigned; DBEngine := Unassigned; end; end; 3.請教各位,何處能找到用DAO的方法建立Access數據庫的文檔資料,謝謝
alex_soong
一般會員


發表:2
回覆:3
積分:1
註冊:2004-11-04

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-08 09:30:46 IP:218.2.xxx.xxx 未訂閱
鬱悶了好久,終于搞定了。哈哈 獻上整個函數單元供大家參考; unit DatabaseUnit; interface uses Windows, Messages, SysUtils, Variants, Classes,comobj,DB,ADODB,Controls; procedure CreateDatabase(DBName: string); //建立Access数据库 Procedure _CreateDatabaseTable(DBName: string; Tablenames:Tstrings; FieldDefsListArray: array of Tlist; arraycount: integer); //建立Access数据库中的表 procedure GetTableDefs(const DBName: string; TableDefsList: Tlist); //获得Access数据库中的表信息 procedure GetFieldDefs(const DBName, TableName: string; FieldDefsList: Tlist); //获得Access数据库中的每个表中的字段信息 procedure RenameField(const TableName, OldColName, NewColName: string; ADOConnection:TADOConnection ); //动态修改Access数据库字段内容 function CheckTablename(Const DBName, TableName: string): boolean; implementation uses VariableUnit; procedure CreateDatabase(DBName: string); //建立access数据库 var DB: OleVariant; tempstr: string; begin DB := CreateOleObject('ADOX.Catalog'); try tempstr := format(accessstring,[DBName]); DB.Create(tempstr); finally DB:=Unassigned; end; end; Procedure _CreateDatabaseTable(DBName: string; Tablenames:Tstrings; FieldDefsListArray: array of Tlist; arraycount: integer); //建立Access数据库中的表 var DB,tables,Cols: OleVariant; //DBEngine, temploop1,temploop2: integer; temppointer: pTAccessFieldDef; tempADOConnection:TADOConnection; tempstr: string; begin if not FileExists(DBName) then //如果没有发现数据库就建立数据库 CreateDatabase(DBName); if TableNames.Count <= 0 then exit; if TableNames.Count <> arraycount then exit; //名称和字段信息个数不一致,就退出 // DBEngine := CreateOleObject('DAO.DBEngine.36'); //DB := DBEngine.OpenDatabase(DBName); DB := CreateOleObject('ADOX.Catalog'); tempADOConnection := TADOConnection.Create(nil); tempstr := format(accessstring,[DBName]); tempADOConnection.ConnectionString := tempstr; tempADOConnection.Open; DB.ActiveConnection := tempADOConnection.ConnectionObject; try for temploop1 := 0 to TableNames.Count -1 do begin if CheckTablename(DBName,TableNames[temploop1]) then //如果没有同名就建立 begin tables := CreateOleObject('ADOX.Table'); tables.ParentCatalog := DB; tables.name := TableNames[temploop1]; //表建立好之后,就开始建立字段 if FieldDefsListArray[temploop1].Count > 0 then begin for temploop2 := 0 to FieldDefsListArray[temploop1].Count -1 do begin temppointer := FieldDefsListArray[temploop1].Items[temploop2]; Cols := CreateOleObject('ADOX.Column'); Cols.ParentCatalog := DB; case temppointer.Types of adInteger: begin Cols.Name := temppointer.Name; Cols.type := temppointer.Types; Cols.DefinedSize := temppointer.Size; Cols.Properties['Autoincrement'].Value := strtobool(temppointer.Autoincrement); Cols.Properties['Description'].Value := temppointer.Description; tables.Columns.Append(Cols, adEmpty, 0); Cols := Unassigned; end; adVarWChar: begin Cols.Name := temppointer.Name; Cols.type := temppointer.Types; Cols.DefinedSize := temppointer.Size; Cols.Properties['Description'].Value := temppointer.Description; tables.Columns.Append(Cols, adEmpty, 0); Cols := Unassigned; end; adLongVarWChar: begin Cols.Name := temppointer.Name; Cols.type := temppointer.Types; Cols.DefinedSize := temppointer.Size; Cols.Properties['Description'].Value := temppointer.Description; tables.Columns.Append(Cols, adEmpty, 0); Cols := Unassigned; end; end; //case temppointer.Types of end; //for temploop2 := 0 to FieldDefsListArray[temploop1].Count -1 do DB.Tables.Append(tables); tables:= Unassigned; //每个表的所有字段都建立好之后,就注销,进入下一个表 Cols:= Unassigned; end else //if FieldDefsListArray[temploop1].Count > 0 then 如果字段小于等于0 就不要建立这个表的字段, begin //进入下一张表 tables:= Unassigned; Cols:= Unassigned; end; end //if CheckTablename(DBName,TableNames[temploop1]) then //如果没有同名就建立 end; //for temploop1 := 0 to TableNames.Count -1 do finally //DBEngine:= Unassigned; tempADOConnection.Close; DB:= Unassigned; tables:= Unassigned; Cols:= Unassigned; tempADOConnection.Free; end; end; procedure GetTableDefs(const DBName: string; TableDefsList: Tlist); //获得Access数据库中的表信息 var DBEngine, DB: OleVariant; I: Longint; temppointer: pTaccessTableDef; begin try DBEngine := CreateOleObject('DAO.DBEngine.36'); DB := DBEngine.OpenDatabase(DBName); if Longint(DB.TableDefs.Count) > 0 then begin for I := 0 to Longint(DB.TableDefs.Count)-1 do begin new(temppointer); temppointer.Name := DB.TableDefs[I].Name; temppointer.DateCreated := DB.TableDefs[I].DateCreated; temppointer.LastUpdated := DB.TableDefs[I].LastUpdated; try temppointer.Description := DB.TableDefs[I].Properties['Description'].Value; except temppointer.Description := ''; end; TableDefsList.Add(temppointer); end; //for I := 0 to Longint(DB.TableDefs.Count)-1 do end; // if Longint(DB.TableDefs.Count) > 0 then finally DB := Unassigned; DBEngine := Unassigned; end; end; procedure GetFieldDefs(const DBName, TableName: string; FieldDefsList: Tlist); //获得Access数据库中的每个表中的字段信息 var DBEngine, DB: OleVariant; I: Longint; temppointer: pTAccessFieldDef; begin try DBEngine := CreateOleObject('DAO.DBEngine.36'); DB := DBEngine.OpenDatabase(DBName); if Longint(DB.TableDefs[TableName].Fields.Count) > 0 then begin for I := 0 to Longint(DB.TableDefs[TableName].Fields.Count) -1 do begin new(temppointer); temppointer.Name := DB.TableDefs[TableName].Fields[I].Name; temppointer.Types := DB.TableDefs[TableName].Fields[I].Type; temppointer.Size := DB.TableDefs[TableName].Fields[I].Size; try temppointer.Description := DB.TableDefs[TableName].Fields[I].Properties['Description'].Value; except temppointer.Description := ''; end; FieldDefsList.Add(temppointer); end; //Longint(DB.TableDefs[TableName].Fields.Count) > 0 end; // Longint(DB.TableDefs[TableName].Fields.Count) > 0 finally DB := Unassigned; DBEngine := Unassigned; end; end; //TableName: 表名; OldColName: 原字段名; NewColName: 新字段名; procedure RenameField(const TableName, OldColName, NewColName: string; ADOConnection:TADOConnection ); var DB, Col: OleVariant; begin try DB := CreateOleObject('ADOX.Catalog'); DB.ActiveConnection := ADOConnection.ConnectionObject; Col := CreateOleObject('ADOX.Column'); Col := DB.Tables[TableName].Columns[OldColName]; Col.Name := NewColName; finally DB := Unassigned; Col := Unassigned; end; end; function CheckTablename(Const DBName, TableName: string): boolean; //true 表示没有检测到相同表名,就可以建立, var //false 表示检测到相同表名,不可以建立 tableNamelist: Tlist; temploop: integer; temppointer: pTaccessTableDef; begin result := false; if not FileExists(DBName) then CreateDatabase(DBName); tableNamelist := Tlist.Create; try GetTableDefs(DBName,tableNameList); if tableNamelist.Count > 0 then begin for temploop := 0 to TableNameList.Count -1 do begin temppointer := TableNamelist.Items[temploop]; if UpperCase(tableName) = UpperCase(temppointer.Name) then exit; end; end; result := true; //如果里面没有表也可以返回true finally tableNamelist.Clear; tablenamelist.Free; end; end; end.
系統時間:2024-06-29 14:37:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!