2016-11-03 152 views
2

我在將數據插入到SQL Server數據庫時遇到問題。我正在研究學校管理系統。我有2個用戶(管理員和工作人員)。管理員創建/設置員工處理的所有基本需求。我無法將數據插入到SQL Server數據庫中

在這裏,管理員正在添加類的詳細信息。所以,第一步是在STDMS_ClassDetails數據庫中添加一個類,部分和強度。之後,科目字段將被更新。現在

,添加新的類流是這樣的:

  1. 在創建類選項卡,管理員可設置類的範圍。例如:1級到10級。

  2. 然後管理員選擇如何添加課程,課程的強度。

    2.i.如果他選擇1by1選項,那麼他需要選擇每節課&課的實力。

    2.ii.如果他選擇了羣體選項,那麼所有上述類別將具有相同的課程和課程強度。

這是我的數據庫。

Database

&這是我的前端。

Front End

沒有錯誤消息,但數據庫沒有更新。所以,在這段代碼中一定有一些我缺少的東西。請幫我糾正並使此代碼正常工作。

我的代碼如下所示:

Private Sub btnCCSave_Click(sender As Object, e As EventArgs) Handles btnCCSave.Click 
    If MsgBox("You are going to add new classes into your database." + vbCrLf + vbCrLf + "Are you sure?", vbInformation + vbYesNo, "Add new classes.") = vbYes Then 
     If rb_1by1.Checked = True Then 
      con.Open() 
      Try 
       Using cmd As New SqlCommand("INSERT INTO STDMS_ClassDetails(Class_ID, Section, Class_Strength) VALUES(@d1, @d2, @d3)", con) 
        With cmd 
         For i As Integer = 1 To updownTotalSections.Value 
          .Parameters.AddWithValue("@d1", Integer.Parse(cbCCClass.Text)) 
          Select Case i 
           Case 1 
            .Parameters.AddWithValue("@d2", "A") 
            Exit Select 
           Case 2 
            .Parameters.AddWithValue("@d2", "B") 
            Exit Select 
           Case 3 
            .Parameters.AddWithValue("@d2", "C") 
            Exit Select 
           Case 4 
            .Parameters.AddWithValue("@d2", "D") 
            Exit Select 
           Case 5 
            .Parameters.AddWithValue("@d2", "E") 
            Exit Select 
          End Select 
          .Parameters.AddWithValue("@d3", updownTotalStrength.Value) 
         Next 
        End With 
       End Using 
       con.Close() 
      Catch ex As Exception 
       MsgBox(ex.ToString) 
      End Try 
     ElseIf rb_Group.Checked = True Then 
      Try 
       MsgBox("try") 
       For i As Integer = updownLB.Value To updownUP.Value 
        MsgBox("for i = " + i.ToString) 
        For j As Integer = 1 To updownTotalSections.Value 
         MsgBox("for j = " + j.ToString) 
         con.Open() 
         MsgBox("con open") 
         Using cmd As New SqlCommand("INSERT INTO STDMS_ClassDetails(Class_ID, Section, Class_Strength) VALUES(@d1, @d2, @d3)", con) 
          With cmd 
           MsgBox("Ready query and cmd") 
           .Parameters.AddWithValue("@d1", i) 
           MsgBox("added first col") 
           Select Case j 
            Case 1 
             MsgBox("case1") 
             .Parameters.AddWithValue("@d2", "A") 
             MsgBox("added1") 
             Exit Select 
            Case 2 
             MsgBox("case2") 
             .Parameters.AddWithValue("@d2", "B") 
             MsgBox("added2") 
             Exit Select 
            Case 3 
             MsgBox("case3") 
             .Parameters.AddWithValue("@d2", "C") 
             MsgBox("added3") 
             Exit Select 
            Case 4 
             MsgBox("case4") 
             .Parameters.AddWithValue("@d2", "D") 
             MsgBox("added4") 
             Exit Select 
            Case 5 
             MsgBox("case5") 
             .Parameters.AddWithValue("@d2", "E") 
             MsgBox("added5") 
             Exit Select 
           End Select 
           MsgBox("out of select") 
           .Parameters.AddWithValue("@d3", updownTotalStrength.Value) 
           MsgBox("added strength") 
           con.Close() 
           MsgBox("closed") 
          End With 
         End Using 
         MsgBox("end") 

        Next 
       Next 

      Catch ex As Exception 
       MsgBox(ex.ToString) 
      End Try 
     End If 
     End If 
    End Sub 
End Class 
+2

你永遠不執行查詢 – Plutonix

+0

哈哈...我的壞...謝謝 –

+1

你需要執行你的命令。只需在循環中添加cmd.ExecuteNonQuery(最後)即可。 – shanyour

回答

0

End Using

cmd.ExecuteNonQuery() 

前只需添加下面的命令,因爲你建立你的命令,但你沒有執行它

相關問題