delphi4arab منتديات دلفي للعرب

نسخة كاملة : إضافة جدول أو حقول لقاعدة بيانات اكسيس
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم و رحمة الله

لاإضافة جدول أو حقول لقاعدة بيانات اكسيس.
الكود التالي يعرض محتوى وحدة uJet_AddTable وفيه دالتين إحداها لإظافة جدول و الأخرى لإضافة حقل:
كود :
unit uJet_AddTable; interface uses Variants, ComObj, DB, ADODB, ADOInt; type TFieldProp = record Name: string; FieldType: integer; FieldTypeDB: TFieldType; Size: integer; Required: boolean; IsId: boolean; AutoInc: boolean; DefaultValue: string; AllowZeroLength: boolean; end; const adColNullable = $00000002; procedure AppendTable(const sConnection, TableName: string); procedure AppendField(const sConnection, TableName: string; Fld: TFieldProp); implementation procedure AppendTable(const sConnection, TableName: string); var cat: OleVariant; tb: OleVariant; Col: OleVariant; begin cat := CreateOleObject('ADOX.Catalog'); Cat.ActiveConnection := sConnection; tb := CreateOleObject('ADOX.Table'); tb.Name := TableName; Cat.Tables.Append(tb); // من المستحسن دائما إضافة حقل فريد Col := CreateOleObject('ADOX.Column'); Col.Name := 'OID'; col.Type := adInteger; Col.ParentCatalog := cat; col.Properties('AutoIncrement') := True; col.Properties('Seed') := 1; col.Properties('Increment') := 1; tb.Columns.Append(col); tb := Unassigned; Cat := Unassigned; end; procedure AppendField(const sConnection, TableName: string; Fld: TFieldProp); var cat: OleVariant; tb: OleVariant; Col: OleVariant; begin cat := CreateOleObject('ADOX.Catalog'); Cat.ActiveConnection := sConnection; tb := Cat.Tables[TableName]; Col := CreateOleObject('ADOX.Column'); if Fld.IsId then begin col.Name := Fld.Name; col.Type := adInteger; Col.ParentCatalog := cat; col.Properties('AutoIncrement') := True; col.Properties('Seed') := 1; col.Properties('Increment') := 1; tb.Columns.Append(col); tb := Unassigned; Cat := Unassigned; exit; end; col.Name := Fld.Name; col.Type := Fld.FieldType; col.DefinedSize := Fld.Size; if not Fld.Required then col.Attributes := adColNullable; try if Fld.FieldType = adBoolean then tb.Columns.Append(Fld.Name, Fld.FieldType) else tb.Columns.Append(col); except end; tb := Unassigned; Cat := Unassigned; end; end.

الكود التالي لوحدة نموذج يتم فيها استدعاء الدالتين:

كود :
implementation uses uJet_AddTable, ADOInt; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var sConnection: string; FileName: string; TableName: string; FldProp: TFieldProp; begin FileName := 'C:\Trash\TestDb.mdb'; sConnection := 'Provider=Microsoft.Jet.OLEDB.4.0;'+ 'Data Source=' + FileName + ';' + 'Persist Security Info=true'; TableName := 'MyTable'; AppendTable(sConnection, TableName); FillChar(FldProp, SizeOf(FldProp), #0); FldProp.Name := 'P_Name'; FldProp.FieldType := adVarWChar; FldProp.Size := 20; FldProp.Required := false; AppendField(sConnection, TableName, FldProp); FillChar(FldProp, SizeOf(FldProp), #0); FldProp.Name := 'IsOK'; FldProp.FieldType := adBoolean; //11;// FldProp.Size := 1; AppendField(sConnection, TableName, FldProp); end;
أعتقد أن التعامل بـ SQL سيكون أيسر
Create Table...
أسهل بكثير ..