我无法将数据插入到SQL Server数据库中

问题描述:

我在将数据插入到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

End Using线

cmd.ExecuteNonQuery() 

前只需添加下面的命令,因为你建立你的命令,但你没有执行它