กระทู้เก่าบอร์ด อ.สุภาพ ไชยา
327 1
URL.หัวข้อ /
URL
การใช้ ADO ในสร้างตาราง แล้วสร้าง Relation แบบ Cascade Delete/Update
วันนี้ผมได้รับ email ถามเกี่ยวกับการสร้างตารางด้วย ADO แล้วสร้าง Relation ให้เป็นแบบ Cascade Delete/Update
ลองศึกษาจากโค้ดนะครับ ถ้าไม่เข้าใจให้ถามเข้ามาได้
' You have to set a reference to Microsoft ADO Ext. 2.X for DDL and Security, before
' running this procedure.
Sub ADOCreateTable()
Dim cat As New ADOX.Catalog
Dim tbl1 As New ADOX.Table
Dim tbl2 As New ADOX.Table
Dim pk As New ADOX.Key
Dim pk2 As New ADOX.Key
' Open the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\test.mdb;"
' Create a new Contacts table object.
With tbl1
.Name = "Contacts"
' Create fields and append them to the new Table
' object. This must be done before appending the
' Table object to the Tables collection of the
' Catalog.
.Columns.Append "ContactID", adVarWChar, 10
.Columns.Append "ContactName", adVarWChar, 100
.Columns.Append "ContactTitle", adVarWChar, 100
.Columns.Append "Phone", adVarWChar, 12
.Columns.Append "Notes", adLongVarWChar
.Columns("Notes").Attributes = adColNullable
End With
' Add the new table to the database.
cat.Tables.Append tbl1
' Create the Primary Key and append table columns to it.
pk.Name = "PrimaryKey"
pk.Type = adKeyPrimary
pk.Columns.Append "ContactID"
' Append the Key object to the Keys collection of Table
tbl1.Keys.Append pk
' Create a new Company table object.
With tbl2
.Name = "Company"
' Create fields and append them to the new Table
' object. This must be done before appending the
' Table object to the Tables collection of the
' Catalog.
.Columns.Append "ComID", adVarWChar, 12
.Columns.Append "ComName", adVarWChar, 100
.Columns.Append "ContactID", adVarWChar, 10
End With
' Add the new table to the database.
cat.Tables.Append tbl2
' Create the Primary Key and append table columns to it.
pk2.Name = "PrimaryKey"
pk2.Type = adKeyPrimary
pk2.Columns.Append "ComID"
' Append the Key object to the Keys collection of Table
tbl2.Keys.Append pk2
Set cat = Nothing
End Sub
Sub ADOCreateForeignKeyCascade()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim fk As New ADOX.Key
' Open the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\test.mdb;"
' Get the table for the foreign side of the relationship
Set tbl = cat.Tables("Company")
' Create the Foreign Key
fk.Name = "ContactsCompany"
fk.Type = adKeyForeign
fk.RelatedTable = "Contacts"
' Specify cascading updates and deletes
fk.UpdateRule = adRICascade
fk.DeleteRule = adRICascade
' Append column(s) in the foreign table to it
fk.Columns.Append "ContactId"
' Set RelatedColumn property to the name of the corresponding
' column in the primary table
fk.Columns("ContactId").RelatedColumn = "ContactId"
' Append the Key object to the Keys collection of Table
tbl.Keys.Append fk
Set cat = Nothing
End Sub
ลองศึกษาจากโค้ดนะครับ ถ้าไม่เข้าใจให้ถามเข้ามาได้
' You have to set a reference to Microsoft ADO Ext. 2.X for DDL and Security, before
' running this procedure.
Sub ADOCreateTable()
Dim cat As New ADOX.Catalog
Dim tbl1 As New ADOX.Table
Dim tbl2 As New ADOX.Table
Dim pk As New ADOX.Key
Dim pk2 As New ADOX.Key
' Open the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\test.mdb;"
' Create a new Contacts table object.
With tbl1
.Name = "Contacts"
' Create fields and append them to the new Table
' object. This must be done before appending the
' Table object to the Tables collection of the
' Catalog.
.Columns.Append "ContactID", adVarWChar, 10
.Columns.Append "ContactName", adVarWChar, 100
.Columns.Append "ContactTitle", adVarWChar, 100
.Columns.Append "Phone", adVarWChar, 12
.Columns.Append "Notes", adLongVarWChar
.Columns("Notes").Attributes = adColNullable
End With
' Add the new table to the database.
cat.Tables.Append tbl1
' Create the Primary Key and append table columns to it.
pk.Name = "PrimaryKey"
pk.Type = adKeyPrimary
pk.Columns.Append "ContactID"
' Append the Key object to the Keys collection of Table
tbl1.Keys.Append pk
' Create a new Company table object.
With tbl2
.Name = "Company"
' Create fields and append them to the new Table
' object. This must be done before appending the
' Table object to the Tables collection of the
' Catalog.
.Columns.Append "ComID", adVarWChar, 12
.Columns.Append "ComName", adVarWChar, 100
.Columns.Append "ContactID", adVarWChar, 10
End With
' Add the new table to the database.
cat.Tables.Append tbl2
' Create the Primary Key and append table columns to it.
pk2.Name = "PrimaryKey"
pk2.Type = adKeyPrimary
pk2.Columns.Append "ComID"
' Append the Key object to the Keys collection of Table
tbl2.Keys.Append pk2
Set cat = Nothing
End Sub
Sub ADOCreateForeignKeyCascade()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim fk As New ADOX.Key
' Open the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\test.mdb;"
' Get the table for the foreign side of the relationship
Set tbl = cat.Tables("Company")
' Create the Foreign Key
fk.Name = "ContactsCompany"
fk.Type = adKeyForeign
fk.RelatedTable = "Contacts"
' Specify cascading updates and deletes
fk.UpdateRule = adRICascade
fk.DeleteRule = adRICascade
' Append column(s) in the foreign table to it
fk.Columns.Append "ContactId"
' Set RelatedColumn property to the name of the corresponding
' column in the primary table
fk.Columns("ContactId").RelatedColumn = "ContactId"
' Append the Key object to the Keys collection of Table
tbl.Keys.Append fk
Set cat = Nothing
End Sub
1 Reply in this Topic. Dispaly 1 pages and you are on page number 1
1 @R01832
ถ้าใช้ DAO ก็ต้องดูตัวอย่างที่ http://support.microsoft.com/default.aspx?scid=kb;en-us;193165 ครับ
Time: 0.1532s