选择Case语句不起作用从sql server获取数据时

时间:2013-04-21 19:10:30

标签: sql-server vb.net

请原谅我,我被困在我的if else和select case语句只会转到ELSE选项。以下是我的代码

lblStudentName.Text = CStr(studentInfo.stuFullName)
lblStudentBirthCert.Text = CStr(studentInfo.birthCert)

Dim conn As New SqlConnection
Dim cmd As New SqlCommand

conn.ConnectionString = ("server = A-PC\SQLEXPRESS;database=studentManagement;integrated security=true")
conn.Open()

cmd.Connection = conn
cmd.Parameters.AddWithValue("@id", lblStudentBirthCert.Text)
cmd.CommandText = ("SELECT paymentType From payment Where birthcert=@id")

Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim tempPaymentType As String

While reader.Read()
    Dim tempPaymentType = reader.GetString(0)
    Select Case tempPaymentType
        Case "a"
            txtFeeA.Text = tempPaymentType
        Case "b"
            txtFeeB.Text = tempPaymentType
        Case "c"
            txtFeeC.Text = tempPaymentType
        Case "d"
            txtFeeD.Text = tempPaymentType
        Case "e"
            txtFeeE.Text = tempPaymentType
        Case Else
            txtFeeF.Text = tempPaymentType
    End Select
End While
reader.Close()

出于某种原因,它只会转到else并在txtFeeF.text中打印出来。查询将返回6结果btw,a到f。 if else语句也是如此。 如果没有if else和select语句,输出将是f,这是读者的最后一个。 非常感谢任何帮助。

If tempPaymentType.Equals("a") Then
            txtFeeA.Text = tempPaymentType.ToString
        End If

2 个答案:

答案 0 :(得分:2)

添加一行以帮助您调试:

    Dim tempPaymentType = reader.GetString(0)
    Debug.Print (String.Format("tempPaymentType: {0}", tempPaymentType))
    Select Case tempPaymentType

可能有很多原因,但您没有提供足够的信息。例如字母不同的情况? (“a”与“A”)

更改此代码可能有所帮助:

Dim tempPaymentType As String

While reader.Read()
    tempPaymentType = reader.GetString(0)

我很惊讶它甚至用额外的Dim语句构建。

答案 1 :(得分:2)

尝试添加

Trim().ToLower() 

之后

GetString(0) 

查看是否存在案例问题或额外空格。如果数据库字段是一个字符字段,它将添加额外的空格,但如果是VARCHAR(),它应该修剪它..