มี2ตารางดังนี้ครับ
GUES(GUES_ID,GUES_NAME,GNDR,NATN,IDEN,E_MAIL,ADDR,TELP_NO)
RESV(RESV_ID,GUES_ID,ROOM_ID,DATE_FROM,DATE_TO,RESV_STTS,DATE_RESV)
มีปํญหาตรงนี้ครับ GUES_ID(PK) ผมตั้งเป็น auto number ไว้บันทึกในตาราง GUES ได้ไม่มีปัญหา
แต่ในตารางRESV GUES_ID(FK) ไม่รู้จะบันทึกเป็นค่าเดียวกันได้ยังไง Code (VB.NET)
Dim connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
Dim conn As New SqlConnection(connection)
conn.Open()
Dim comm As String = "INSERT INTO GUES(GUES_NAME,GNDR,NATN,IDEN,E_MAIL,ADDR,TELP_NO)"
comm &= "VALUES('" & TextBox1.Text & "',"
comm &= "'" & TextBox2.Text & "',"
comm &= "'" & TextBox3.Text & "',"
comm &= "'" & TextBox4.Text & "',"
comm &= "'" & TextBox5.Text & "',"
comm &= "'" & TextBox6.Text & "',"
comm &= "'" & TextBox7.Text & "')"
Dim command As New SqlCommand(comm, conn)
command.ExecuteNonQuery()
comm = "INSERT INTO RESV(GUES_ID,ROOM_ID,DATE_FROM,DATE_TO,RESV_STTS,DATE_RESV)"
comm &= "VALUES('" & ??????? & "',"
comm &= "'" & lblSeleceRoom.Text & "',"
comm &= "Convert(DateTime, '" & lblSeleceDate.Text & "', 103),"
comm &= "Convert(DateTime, '" & lblSeleceDate1.Text & "', 103),"
comm &= "'A',"
comm &= "'" & DateNow & "')"
Dim acommand As New SqlCommand(comm, conn)
acommand.ExecuteNonQuery()
conn.Close()
End Sub
End Class
Dim Query As String = "Insert Into Table (Fields) Values (@Fields); Select Scope_Identity();"
Dim LastID As Integer
Dim StrCon As String = "Server=.\SQLExpress;............................................................................................."
Using DbCon As New SqlConnection(StrCon)
Using Cmd As New SqlCommand(Query, DbCon)
Cmd.Parameters.AddWithValue("@Fields", Fields.Text)
DbCon.Open()
LastID = Cmd.ExecuteScalar()
End Using
End Using