将数据插入2个表中

时间:2014-03-23 01:36:39

标签: mysql sql vb.net

这是我注册学生的代码:

首先,我想将学生信息存储到tblStudentRecord,我希望将其存储在另一个表tblSection1中。

这是我的代码:

If (MsgBox("The records you entered will be saved to the database. Do you want to proceed to the enrolment?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
      Dim cmd As New OleDb.OleDbCommand
      If Not cnn.State = ConnectionState.Open Then
           cnn.Open()
      End If
      Dim converter As New ImageConverter
      Dim iByte As Byte()
      iByte = converter.ConvertTo(picStud.Image, GetType(Byte()))
      cmd.Connection = cnn
      Try
           With cnn
           If .State <> ConnectionState.Open Then
                .Open()
           End If
           strsql = "INSERT INTO tblStudRecord(LName,FName,MName,BMonth,BDay,BYear,StudAge,StudGender,StudNationality,StudReligion,TownCity,Province,StudPNum,StudAddress,FatherName,FOccupation,FContact,FAddress,MotherName,MOccupation,MContact, MAddress, GuardianName, GContact, GAddress, StudStatus, LastSchAtt, sPlace, GrdYr, DateofReg, ForGrdYrLvl,studPic,sStatus)" & _
                     "VALUES (@LName,@FName,@MName,@BMonth,@BDay,@BYear,@StudAge,@StudGender,@StudNationality, @StudReligion , @TownCity , @Province , @StudPNum,@studAddress , @FatherName , @FOccupation , @FContact , @FAddress , @MotherName , @MOccupation, @MContact , @MAddress , @GuardianName , @GContact , @GAddress , @StudStatus , @LastSchAtt , @sPlace , @GrdYr , @DateofReg , @ForGrdYr,@picStud,'Enrolled')"

          cmd.CommandText = strsql
          cmd.Connection = cnn

          cmd.Parameters.AddWithValue("@LName", txtLName.Text) '2
          cmd.Parameters.AddWithValue("@FName", txtFName.Text) '3
          cmd.Parameters.AddWithValue("@MName", txtMName.Text) '4
          cmd.Parameters.AddWithValue("@BMonth", cboBMonth.SelectedItem) '5
          cmd.Parameters.AddWithValue("@BDay", cboBDay.SelectedItem) '6
          cmd.Parameters.AddWithValue("@BYear", cboBYear.SelectedItem) '7
          cmd.Parameters.AddWithValue("@StudAge", txtAge.Text) '8
          cmd.Parameters.AddWithValue("@StudGender", cboGender.SelectedItem) '9
          cmd.Parameters.AddWithValue("@StudNationality", txtNationality.Text) '10
          cmd.Parameters.AddWithValue("@StudReligion", txtReligion.Text) '11
          cmd.Parameters.AddWithValue("@TownCity", txtTownCity.Text) '12
          cmd.Parameters.AddWithValue("@Province", txtProvince.Text) '13
          cmd.Parameters.AddWithValue("@StudPNum", txtMobileNumber.Text) '14
          cmd.Parameters.AddWithValue("@studAddress", txtHomeAddress.Text) '15
          cmd.Parameters.AddWithValue("@FatherName", txtFatherName.Text) '16
          cmd.Parameters.AddWithValue("@FOccupation", txtFatherOccupation.Text) '17
          cmd.Parameters.AddWithValue("@FContact", txtFatherContact.Text) '18
          cmd.Parameters.AddWithValue("@FAddress", txtFatherAddress.Text) '19
          cmd.Parameters.AddWithValue("@MotherName", txtMotherName.Text) '20
          cmd.Parameters.AddWithValue("@MOccupation", txtMotherOccupation.Text) '21
          cmd.Parameters.AddWithValue("@MContact", txtMotherContact.Text) '22
          cmd.Parameters.AddWithValue("@MAddress", txtMotherAddress.Text) '23
          cmd.Parameters.AddWithValue("@GuardianName", txtGuardianName.Text) '24
          cmd.Parameters.AddWithValue("@GContact", txtGuardianContact.Text) '25
          cmd.Parameters.AddWithValue("@GAddress", txtGuardianAddress.Text) '26
          cmd.Parameters.AddWithValue("@StudStatus", cboStatus.SelectedItem) '27
          cmd.Parameters.AddWithValue("@LastSchAtt", txtLastSchoolAttended.Text) '28
          cmd.Parameters.AddWithValue("@sPlace", txtPlace.Text) '29
          cmd.Parameters.AddWithValue("@GrdYr", txtGradeYearLvl.Text) '30
          cmd.Parameters.AddWithValue("@DateofReg", txtDateofRegistration.Text) '31
          cmd.Parameters.AddWithValue("@ForGrdYr", cboYearLvl.SelectedItem) '32
          cmd.Parameters.AddWithValue("@picStud", iByte)

2 个答案:

答案 0 :(得分:1)

最后调用cmd.ExecuteNonQuery()函数。

答案 1 :(得分:0)

如果表格tblStudentRecordtblSection1具有相同的字段或列,请尝试此操作...

strsql = "INSERT INTO tblStudRecord(LName,FName,MName,BMonth,BDay,BYear,StudAge,StudGender,StudNationality,StudReligion,TownCity,Province,StudPNum,StudAddress,FatherName,FOccupation,FContact,FAddress,MotherName,MOccupation,MContact, MAddress, GuardianName, GContact, GAddress, StudStatus, LastSchAtt, sPlace, GrdYr, DateofReg, ForGrdYrLvl,studPic,sStatus)" & _
         "VALUES (@LName,@FName,@MName,@BMonth,@BDay,@BYear,@StudAge,@StudGender,@StudNationality, @StudReligion , @TownCity , @Province , @StudPNum,@studAddress , @FatherName , @FOccupation , @FContact , @FAddress , @MotherName , @MOccupation, @MContact , @MAddress , @GuardianName , @GContact , @GAddress , @StudStatus , @LastSchAtt , @sPlace , @GrdYr , @DateofReg , @ForGrdYr,@picStud,'Enrolled')" & _
         ";" & _
         "INSERT INTO tblSection1(LName,FName,MName,BMonth,BDay,BYear,StudAge,StudGender,StudNationality,StudReligion,TownCity,Province,StudPNum,StudAddress,FatherName,FOccupation,FContact,FAddress,MotherName,MOccupation,MContact, MAddress, GuardianName, GContact, GAddress, StudStatus, LastSchAtt, sPlace, GrdYr, DateofReg, ForGrdYrLvl,studPic,sStatus)" & _
         "VALUES (@LName,@FName,@MName,@BMonth,@BDay,@BYear,@StudAge,@StudGender,@StudNationality, @StudReligion , @TownCity , @Province , @StudPNum,@studAddress , @FatherName , @FOccupation , @FContact , @FAddress , @MotherName , @MOccupation, @MContact , @MAddress , @GuardianName , @GContact , @GAddress , @StudStatus , @LastSchAtt , @sPlace , @GrdYr , @DateofReg , @ForGrdYr,@picStud,'Enrolled')" & _
         ";"