Results 1 to 3 of 3

Thread: how to insert data into multiple tables?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    208

    how to insert data into multiple tables?

    I am using asp.net,c# and ms sql for my web application.
    i have two tables
    tableone
    idno int identity(1,1),fullname varchar(200),mobileno varchar(200)

    tabletwo code int idenity(1,1),totalpayment float, paidamount float,unpaidamount float, idno foreign key
    the column idno in tableone is auto increment by one, but the idno in tabletwo should be added from tableone.
    now i want to insert data at the same time to both of the tables.
    but i get it hard how to insert the foreign key values
    please help?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to insert data into multiple tables?

    Use a DataSet with a DataRelation between the two DataTables. You can configure the DataRelation to cascade on updates and that will allow you to propagate the generated IDs from the parent table to the child table.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to insert data into multiple tables?

    E.g.
    vb.net Code:
    1. Dim data As New DataSet
    2. Dim parentTable = data.Tables.Add()
    3. Dim childTable = data.Tables.Add()
    4. Dim parentKey As DataColumn
    5. Dim childKey As DataColumn
    6. Dim foreignKey As DataColumn
    7.  
    8. With parentTable.Columns
    9.     parentKey = .Add("ParentId", GetType(Integer))
    10.     .Add("ParentName", GetType(String))
    11. End With
    12.  
    13. With childTable.Columns
    14.     childKey = .Add("ChildId", GetType(Integer))
    15.     foreignKey = .Add("ParentId", GetType(Integer))
    16.     .Add("ChildName", GetType(String))
    17. End With
    18.  
    19. parentKey.AutoIncrement = True
    20. childKey.AutoIncrement = True
    21.  
    22. parentTable.PrimaryKey = {parentKey}
    23. childTable.PrimaryKey = {childKey}
    24.  
    25. Dim relation As New DataRelation("ParentChild", parentKey, foreignKey, True)
    26.  
    27. relation.ChildKeyConstraint.UpdateRule = Rule.Cascade
    28.  
    29. 'Add a parent row and let a temporary parent ID be generated.
    30. Dim parentRow = parentTable.Rows.Add(DBNull.Value, "First Parent")
    31.  
    32. 'Add a child row and use the temporary parent ID as the foreign key value.
    33. Dim childRow = childTable.Rows.Add(DBNull.Value, parentRow("ParentId"), "FirstChild")
    34.  
    35. 'After insert a parent record, select the last generated ID back into the ParentId column of parentTable.
    36. Dim parentSql = "INSERT INTO Parent (ParentName) VALUES (@ParentName); SELECT ParentId = SCOPE_IDENTITY()"
    37.  
    38. Dim childSql = "INSERT INTO Child (ParentId, ParentName) VALUES (@ParentId, ChildName)"
    39.  
    40. Using connection As New SqlConnection("connection string here"),
    41.       parentCommand As New SqlCommand(parentSql, connection),
    42.       childCommand As New SqlCommand(childSql, connection),
    43.       parentAdapter As New SqlDataAdapter With {.InsertCommand = parentCommand},
    44.       childAdapter As New SqlDataAdapter With {.InsertCommand = childCommand}
    45.     parentCommand.Parameters.Add("@ParentName", SqlDbType.VarChar, 50, "ParentName")
    46.  
    47.     With childCommand.Parameters
    48.         .Add("@ParentId", SqlDbType.Int, 0, "ParentId")
    49.         .Add("@ChildName", SqlDbType.VarChar, 50, "ChildName")
    50.     End With
    51.  
    52.     connection.Open()
    53.  
    54.     'Save the new parent record(s), which will also retrieve the generated ID(s),
    55.     'which will be propagated to the child table by the DataRelation.
    56.     parentAdapter.Update(parentTable)
    57.  
    58.     'Save the new child record(s), which now contain the correct foreign key value(s).
    59.     childAdapter.Update(childTable)
    60. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width