简体   繁体   中英

simulate database vb.net

I search a way to simulate a database and link between table.

sample:

Table A
colA

And

Table B
colB
colD

linked by

Table C
colA
colB

and

Table D
colD

That made TableA linked to TableB by TableC. And tableD linked to tableB.

what I think to do its a kind of linkedlist.

a little like :

Class Table
    Private cols As List(Of Column)
End Class

Public Delegate Sub TableDelegate(ByVal tbl As Table)

Class Column
    Private name As String 'use for column name like colA,colB....

    Private fk As List(Of TableDelegate) 'linked foreign key
End Class

I never work with delegate, I usely do this in pointer, but I'm stuck in vb.

So its a beginning, if you have idea where to go or a complete other way to do it?

tank you

My big problem with your way is I can't relate it to what I assume the properties and operations of a foreign key are. The other bit I don't like is a table doesn't have to have a foreign key and a foreign key has state and expected behaviors which suggests to me it should be an object in it's own right. Looks like you are trying to write C in VB.Net, never going to work, that.

I'd be tempted by

Database With a Tables Property
Table Having an Owner property of Database
Database With a ForeignKeys Property that could be accessed through owner

though with interfaces not concrete classes.

eg Then in Delete for an id you could do something like Owner.ForeignKeys.CascadeDelete(Me.CurrentRecord)

Where CurrentRecord exposed at least a list of column names and values.

as a first go.

Mind you I'd need some convincing before I went for reinventing this wheel..

Be easier to bang sqllite on there and use it, for instance.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  'Table A'
  Dim dtbA As New DataTable("TableA")
  dtbA.Columns.Add(New DataColumn("ColA", GetType(String)))
  dtbA.PrimaryKey = {dtbA.Columns("ColA")}
  'Table B'
  Dim dtbB As New DataTable("TableB")
  dtbB.Columns.Add(New DataColumn("ColB", GetType(String)))
  dtbB.Columns.Add(New DataColumn("ColD", GetType(String)))
  dtbB.PrimaryKey = {dtbB.Columns("ColB")}
  'Table C'
  Dim dtbC As New DataTable("TableC")
  dtbC.Columns.Add(New DataColumn("ColA", GetType(String)))
  dtbC.Columns.Add(New DataColumn("ColB", GetType(String)))
  dtbC.PrimaryKey = {dtbC.Columns("ColA"), dtbC.Columns("ColB")}
  'Table D'
  Dim dtbD As New DataTable("TableD")
  dtbD.Columns.Add(New DataColumn("ColD", GetType(String)))
  'Dataset'
  Dim dst As New DataSet("MyDataset")
  dst.Tables.Add(dtbA)
  dst.Tables.Add(dtbB)
  dst.Tables.Add(dtbC)
  dst.Tables.Add(dtbD)
  dst.Relations.Add(New DataRelation("AC", dtbA.Columns("ColA"), dtbC.Columns("ColA"), True))
  dst.Relations.Add(New DataRelation("BC", dtbB.Columns("ColB"), dtbC.Columns("ColB"), True))
  dst.Relations.Add(New DataRelation("BD", dtbB.Columns("ColD"), dtbD.Columns("ColD"), True))
  'now add data'
  dst.Tables("TableA").Rows.Add("AA1")
  dst.Tables("TableA").Rows.Add("AA2")
  dst.Tables("TableA").Rows.Add("AA3")
  dst.Tables("TableB").Rows.Add("BB1", "DD1")
  dst.Tables("TableB").Rows.Add("BB2", "DD2")
  dst.Tables("TableB").Rows.Add("BB3", "DD3")
  dst.Tables("TableC").Rows.Add("AA1", "BB1")
  dst.Tables("TableC").Rows.Add("AA2", "BB2")
  dst.Tables("TableC").Rows.Add("AA3", "BB3")
  dst.Tables("TableD").Rows.Add("DD1")
  dst.Tables("TableD").Rows.Add("DD2")
  dst.Tables("TableD").Rows.Add("DD3")
  'query the data'
  Dim s As String = ""
  For Each drwA As DataRow In dst.Tables("TableA").Rows
    s &= "TableA:" & drwA("ColA").ToString & vbCrLf
    Dim drwC As DataRow = drwA.GetChildRows(dst.Relations("AC"))(0)
    s &= "--TableC:" & drwC("ColA").ToString() & drwC("ColB").ToString() & vbCrLf
    Dim drwB As DataRow = drwC.GetParentRow(dst.Relations("BC"))
    s &= "--TableB:" & drwB("ColB").ToString() & drwB("ColD").ToString() & vbCrLf
    Dim drwD As DataRow = drwB.GetChildRows(dst.Relations("BD"))(0)
    s &= "--TableD:" & drwD("ColD").ToString() & vbCrLf
  Next
  MsgBox(s)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM