Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Insert values into two tables with identity

$
0
0

Hi all,

I looked around the net for an example but I am not getting much. 

What I need to do is insert into two different tables in my 2008 SQL databse.

Insert 1 will save data into table #1 using Scope identity. 

Insert 2 will use the ID from the first insert and then do another insert into table #2.

Here is my single insert code:

 Using conDB1 As SqlConnection = New SqlConnection("connectionstring")
            Dim cmdDB1 As SqlCommand
            Dim strSQL1 As String = "INSERT INTO Log_tbl (ID1,test1)" & _
            "VALUES (@ID1,@test1);" & "Select Scope_Identity()"
            Dim ID As Integer
            cmdDB1 = New SqlCommand(strSQL1, conDB1)
            conDB1.Open()
            With cmdDB1.Parameters
                .Add(New SqlParameter("@ID1", ID.Text))
                .Add(New SqlParameter("@test1", editor1.Html.ToString))
                        
            End With
            ID = cmdDB1.ExecuteScalar()
              conDB1.Close()
 
 
thanks!

Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>