Mysql 从数据库检索数据时出现奇怪的问题

Mysql 从数据库检索数据时出现奇怪的问题,mysql,database,vb.net,Mysql,Database,Vb.net,从表中读取数据时,我的一个数值返回为-1,而不是用户给定的值 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try Dim conn As New MySqlConnection("server=localhost;user=root;database=new_project;port=3306;password=********;") conn.O

从表中读取数据时,我的一个数值返回为-1,而不是用户给定的值

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim conn As New MySqlConnection("server=localhost;user=root;database=new_project;port=3306;password=********;")
        conn.Open()
        Dim command As New MySqlCommand("SELECT * FROM students;", conn)
        Dim dataSet As New DataSet()
        Dim dataAdapter As New MySqlDataAdapter()
        dataAdapter.SelectCommand = command
        dataAdapter.Fill(dataSet, "students")
        Dim dataTable As DataTable = dataSet.Tables("students")
        For x As Integer = 0 To dataTable.Rows.Count - 1
            Dim newStudent As New Student
            newStudent.intIDNum = dataTable.Rows(x).Item("idNumber")
            newStudent.strFirstName = dataTable.Rows(x).Item("firstName")
            newStudent.strLastName = dataTable.Rows(x).Item("lastName")
            newStudent.chrGender = dataTable.Rows(x).Item("gender")
            newStudent.dateDOB = dataTable.Rows(x).Item("dateOfBirth")
            newStudent.intAge = CInt(Today.Year - newStudent.dateDOB.Year)
            newStudent.intYearGroup = dataTable.Rows(x).Item("yearGroup")
            newStudent.intSkillLevel = dataTable.Rows(x).Item("skillLevel")
            studentList.Add(newStudent)
            lstStudents.Items.Add(newStudent.nameConcat(newStudent.strFirstName, newStudent.strLastName))
        Next
        conn.Close()
    Catch ex As Exception
        MsgBox("Error: " & ex.ToString())
    End Try
End Sub
当读取值“skillLevel”时,它应该在1到4之间读取,但它读取的是-1。通过MySQL直接添加到数据库的任何数据都没有出现这种情况,但在我的程序中使用这种方法时,似乎出现了这个问题

Dim newStudent As New Student
        newStudent.strFirstName = txtForename.Text
        newStudent.strLastName = txtSurname.Text
        newStudent.dateDOB = dtpStudentDOB.Value
        If rdbMale.Checked = True Then
            newStudent.chrGender = "M"
        Else
            newStudent.chrGender = "F"
        End If
        newStudent.intAge = newStudent.calcAge(dtpStudentDOB.Value)
        newStudent.intSkillLevel = cmbSkillLevel.SelectedItem
        newStudent.intYearGroup = cmbYearGroup.SelectedItem
        newStudent.intIDNum = CInt(txtStudentID.Text)
        Dim conn As New MySqlConnection("server=localhost;user=root;database=new_project;port=3306;password=********;")
        Try
            conn.Open()
            Dim command As MySqlCommand = New MySqlCommand
            command.Connection = conn
            command.CommandText = "INSERT INTO Students VALUES(@IdNumber, @firstName, @lastName, @gender, @dateOfBirth, @yearGroup, @skillLevel);"
            command.Prepare()
            command.Parameters.AddWithValue("@IdNumber", newStudent.intIDNum)
            command.Parameters.AddWithValue("@firstName", newStudent.strFirstName)
            command.Parameters.AddWithValue("@lastName", newStudent.strLastName)
            command.Parameters.AddWithValue("@gender", newStudent.chrGender)
            command.Parameters.AddWithValue("@dateOfBirth", newStudent.dateDOB)
            command.Parameters.AddWithValue("@yearGroup", newStudent.intYearGroup)
            command.Parameters.AddWithValue("@skillLevel", newStudent.intSkillLevel)
            command.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox("Error: " & ex.ToString())
        Finally
            conn.Close()
        End Try
这是将学生添加到系统的代码部分。究竟是什么原因导致出现此错误

作为参考,我在数据中添加的主要表单使用枚举:

Private Enum SkillLevels As Integer
    One = 1
    Two = 2
    Three = 3
    Four = 4
End Enum

然后我将它链接到下拉列表:
cmbSkillLevel.DataSource=System.Enum.GetValues(GetType(SkillLevels))

很明显,在技能级别下拉列表中没有选择,这就是为什么得到-1的原因,您可以通过确保它不是-1来检查下拉列表中是否做出了选择

例如,您的技能下拉列表可能如下所示

<select id="SkillLevel">
  <option value="0" selected="selected">Please Select</option>
  <option value="1">Low</option>
  <option value="2">Medium</option>
  <option value="3">High</option>
  <option value="4">Very-High</option>
</select>

请选择
低的
中等的
高的
很高
如果未进行选择,则返回的默认值将为零(0) 您可以通过查找大于0的返回值来检查有效值


据我所知,如果省略selected=“selected”,那么返回值将是-1,我想我误解了@tolanj的评论。似乎在加载我的数据时,系统无法读取我的skillLevel列。为了修复它,我将
newStudent.intSkillLevel=row.Item(“skillLevel”)
更改为
newStudent.intSkillLevel=SByte.Parse(row.Item(“skillLevel”)

我认为您需要
newStudent.intSkillLevel=cmbSkillLevel.SelectedItem.Value
SelectedItem
是一个对象,所以VB.NET可能隐式地强制转换它,并给您一个不可靠的值?您可以通过弹出一个带有
newStudent.intSkillLevel=cmbSkillLevel.SelectedItem
newStudent.intSkillLevel=cmbSkillLevel.SelectedItem.Value
的消息框轻松找到答案。我尝试使用
cmbSkillLevel.SelectedValue
,但问题仍然存在。但是,只有当我关闭程序然后重新打开它时才会发生这种情况。skillLevel列的数据库端类型是什么?假设它是整数类型,您是否尝试传递command.Parameters.AddWithValue(“@skillLevel”,CInt(newStudent.intSkillLevel))?这似乎也不起作用。在数据库端,skillLevel是一种TINYINT类型。显式使用command.Parameters.AddWithValue(“@skillLevel”,3)是否有效?下拉框没有零选项。该设计由四个选项组成,没有其他选项。让我在问题中添加详细信息。无论是否有零选项,当未进行选择时,返回值始终为-1,因此这意味着表单在提交技能级别之前提交。