MySQL WHERE IN仅返回最后一行

时间:2017-01-30 12:05:10

标签: mysql

我有一张邮政编码表以及他们的纬度和经度。 用户将邮政编码列表发布到多行文本框中,并创建一个列表并将其输入sql = "SELECT postcode, latitude,longitude FROM geocodes WHERE postcode IN (" & PCList & ")"

PCList的格式为

 'AL7 2BQ ','B12 9BS ','B14 7QU ','B21 9SF ','B24 9PJ ','B27 7RR '
,'B33 0NG ','B42 1NN ','B63 4RH ','B64 5AB ','B65 0LG ','B70 9QL '
,'B73 5AB ','B79 7AG ','BH1 1EN ','BH1 4SX ','BH9 2HE ','BL1 8TH '
,'BL3 6JR ','BL4 9HF ','BL5 3YY ','BL8 2EQ ','BN2 5TB ','BN9 0AD' etc.

我只收到最后一个邮政编码。我试过MysqlDatareader,Datasets,mySQL字符串返回工作台中的完整列表。

如果我用某些东西替换sql" SELECT postcode, latitude,longitude FROM geocodes WHERE postcode LIKE 'B65%'"然后我得到一个完整的邮政编码列表,在datareader或数据集中启动B65,但无法使它与mySQL WHERE IN一起使用。

以下是代码: -

Protected Sub ShowLocsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowLocsButton.Click
    Dim PCList As String = Replace(Me.PostcodeListTextBox.Text, vbCrLf, "','")
    PCList = "'" & PCList & "'"
    GetMarkers(PCList) 
End Sub

Protected Sub GetMarkers(ByVal PCList As String)
Dim PointsDs As New DataSet
    Dim PointsDa As MySqlDataAdapter
    Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnString").ConnectionString
    Dim conn As New MySqlConnection(ConnString)
    command.Connection = conn
    If conn.State <> ConnectionState.Open Then
        conn.Open()
    End If
    sql = "SELECT postcode, latitude,longitude FROM geocodes WHERE postcode IN (" & PCList & ")"
    command.CommandText = sql
    PointsDR = command.ExecuteReader()
    'Dim myrow As Integer = 0
    Me.VRPMap.Layers.Clear()
    'MsgBox(PointsDs.Tables(0).Rows.Count)
    While PointsDR.Read
        Dim myPostcode As String = PointsDR.GetString(0)
        Dim myLatitude As String = PointsDR.GetValue(1)
        Dim myLongitude As String = PointsDR.GetValue(2)
    End While
    conn.Close()

结束子

2 个答案:

答案 0 :(得分:0)

sql = "SELECT postcode, latitude,longitude FROM geocodes WHERE postcode IN (" % PCList % ")"

答案 1 :(得分:0)

它是训练空间,在我的字符串创建中用vbcrlf替换了vblf,问题解决了。