将数据插入关系表

时间:2015-12-06 03:28:04

标签: vb.net sql-server-2008-r2

我有三个关系表,这是我想做的。

  1. 将数据插入表格
  2. 获取最后插入的行ID
  3. 将最后插入的行ID插入关系表
  4. 我收到此错误:列名LastInsertID

    无效
     Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
    
        cn.Open()
    
        Using cmd As New SqlClient.SqlCommand("INSERT INTO ParentInformation(father_firstname,father_lastname,father_mi,father_occupation " _
                                             & " father_telnum,mother_firstName,mother_lastname,mother_mi,mother_occupation,mother_telnum," _
                                             & " contact_firstname, contact_lastname,contact_MI, contact_Address, contact_telnum) " _
                                             & " VALUES('" & txtFatherGN.Text & "', '" & txtFatherLN.Text & "','" & txtFatherMI.Text & "'," _
                                             & " '" & txtFatherOccupation.Text & "','" & txtFatherCP.Text & "','" & txtMotherGN.Text & "' ," _
                                             & " '" & txtMotherLN.Text & "','" & txtMotherMI.Text & "','" & txtMotherOccupation.Text & "'," _
                                             & "'" & txtMotherCP.Text & "','" & txtContactGN.Text & "','" & txtContactLN.Text & "'," _
                                             & "'" & txtContactMI.Text & "', '" & txtContactAddress.Text & "','" & txtContactCP.Text & "'; dim lastInsertedID as string = SELECT LAST_INSERT_ID())", cn)
        End Using
        Using cmd As New SqlClient.SqlCommand("INSERT INTO studentInformation(Surname,firstName, " _
                                              & " MiddleName,Address,Birthday,Gender,Nationality,Birthplace, " _
                                              & " Telnum,SchoolLastAttended,Note,Image,ParentID) " _
                                              & " VALUES('" & txtStudLN.Text & "', '" & txtStudFN.Text & "','" & txtStudMN.Text & "'," _
                                              & " '" & txtAddress.Text & "','" & dtpBirthday.Text & "','" & newStudent & "' ," _
                                              & " '" & cboNationality.Text & "','" & txtPlaceOfBirth.Text & "','" & txtStudentCP.Text & "'," _
                                              & "'" & cboSchoolYear.Text & "','" & cboGradeLevel.Text & "','" & txtSWG.Text & "'," _
                                              & "'" & txtSchoolAddress.Text & "', '" & txtNote.Text & "',@StudentPic,lastInsertedID;dim lastInsertedID as string = SELECT LAST_INSERT_ID())", cn)
    
        End Using
        Using cmd As New SqlClient.SqlCommand("INSERT INTO StudentHistory(SchoolYear,Levels,Section,DateEnrolled ,StudentID) " _
                                              & " VALUES('" & cboSchoolYear.Text & "','" & cboGradeLevel.Text & "', " _
                                              & "'" & cboSection.Text & "','" & dtpEnrollment.Text & "', lastInsertedID)", cn)
            cmd.Parameters.Add(New SqlClient.SqlParameter("@StudentPic", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName)
            i = cmd.ExecuteNonQuery
            If (i > 0) Then
                MsgBox("Save " & i & "Record Successfully")
                'clear()
            End If
    
        End Using
    
    
        cn.Close()
    End Sub
    

    Relationship of my database

    任何人都可以帮我解决这个问题。提前谢谢

1 个答案:

答案 0 :(得分:0)

尝试替换

,LAST_INSERT_ID()

_this.http.post(...).map is not a function

数据库服务器将了解LAST_INSERT_ID()

的最后一个值