Mysql查询在vb.net上显示空结果

时间:2015-01-26 05:46:20

标签: mysql vb.net

我有一个vb.net应用程序,它使用mysql作为数据库。

我遇到的问题是查询的结果总是空的。

以下是我的代码。

Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click

    Dim con As String = ConfigurationManager.ConnectionStrings("prcConnectionString").ConnectionString
    Dim Sql As New MySqlConnection(con)
    Dim reader2 As Object

    Sql.Open()
    Dim theQuery2 As String = "Select max(shipmentdate) from prc.tbsrparts t where Substring(partsn, 17, 11) =' " & tbPartSN.Text.Substring(16, 11) & "' "  'get latest shipment date from database

    Dim command2 As New MySqlCommand(theQuery2, Sql)

    reader2 = command2.ExecuteScalar
    lbl90days.Text = reader2.ToString


End Sub

结果应显示在标签lbl90days。

但我的总是空的。

任何人都可以指导我吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

试试这个:

Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
    Dim con As String = ConfigurationManager.ConnectionStrings("prcConnectionString").ConnectionString
    Dim Sql As New MySqlConnection(con)
    Dim reader2 As Object

    Sql.Open()
    Dim theQuery2 As String = "Select max(shipmentdate) from prc.tbsrparts t where Substring(partsn, 16, 11) = @PartSN"

    Dim command2 As New MySqlCommand(theQuery2, Sql)
    command2.CommandType = CommandType.Text
    command2.Parameters.AddWithValue("@PartSN", tbPartSN.Text.Substring(16, 11))        

    reader2 = command2.ExecuteScalar
    lbl90days.Text = reader2.ToString
    Sql.close()
End Sub

答案 1 :(得分:0)

能够通过

解决
Dim PartSN As String = tbPartSN.Text.Substring(16, 11)

替换Query2代码
Dim theQuery2 As String = "SELECT Max(shipmentdate) FROM prc.tbsrparts t WHERE Substring(partsn, 17, 11) = '" + PartSN.ToString + "'"