0

我有以下代码,我试图在其中调用 mysql 存储过程,因为我不能 a)让存储过程在 EF 中工作,b)我不能让 EF LINQ 查询工作,所以我要回到旧的基础知识. 以下错误作为 VS 要求 ExecuteQuery 提供参数的行。这不是我在打电话之前已经做过的吗?

Public Shared Function GetNearestWeatherStationSql(ByVal lat As Decimal, longi As Decimal) As String
    Const connectionString As String = "server=xxxxxx;user=xxxxxx;database=xxxxxxx;port=xxxx;password=xxxxxxxx;"
    Dim conn As New MySqlConnection()
    conn.ConnectionString = connectionString

    Dim command As New MySqlCommand()
    command.CommandType = CommandType.StoredProcedure
    command.CommandText = "Call GetNearestWeatherStation()"
    command.Parameters.AddWithValue("@iLat", lat)
    command.Parameters.AddWithValue("@iLong", longi)
    command.Parameters.AddWithValue("@iWithinMiles", 15)
    command.Parameters.AddWithValue("@iTopN", 2)

    command.Connection = conn
    conn.Open()
    command.ExecuteNonQuery()

    conn.Close()


    Return "p"  'bogus value until I get SP to work
End Function
4

2 回答 2

0

AddWithValue提供命名占位符的替换。但是您没有在查询中放置占位符。

command.CommandText = "Call GetNearestWeatherStation(@iLat, @iLong, @iWithinMiles, @iTopN)"
command.Parameters.AddWithValue("@iLat", lat)
command.Parameters.AddWithValue("@iLong", longi)
command.Parameters.AddWithValue("@iWithinMiles", 15)
command.Parameters.AddWithValue("@iTopN", 2)
于 2013-11-08T17:50:59.277 回答
0

好吧,它并不漂亮,任何人都可以感觉到展示合适的重构,但我将每个参数单独添加到集合中,然后使用阅读器访问。

Public Shared Function GetNearestWeatherStationSql(ByVal lat As Decimal, longi As Decimal) As String
    Const connectionString As String = "xxxxxx"
    Dim conn As New MySqlConnection()
    conn.ConnectionString = connectionString

    Dim command As New MySqlCommand()
    command.CommandText = "GetNearestWeatherStation"
    command.CommandType = CommandType.StoredProcedure
    Dim param As New MySqlParameter()
    With param
        .ParameterName = "@iLat"
        .MySqlDbType = MySqlDbType.Decimal
        .Direction = ParameterDirection.Input
        .Value = lat
    End With
    command.Parameters.Add(param)
    Dim param1 As New MySqlParameter()
    With param1
        .ParameterName = "@iLong"
        .MySqlDbType = MySqlDbType.Decimal
        .Direction = ParameterDirection.Input
        .Value = longi
    End With
    command.Parameters.Add(param1)
    Dim param2 As New MySqlParameter()
    With param2
        .ParameterName = "@iWithinMiles"
        .MySqlDbType = MySqlDbType.Int32
        .Direction = ParameterDirection.Input
        .Value = 15
    End With
    command.Parameters.Add(param2)
    Dim param3 As New MySqlParameter()
    With param3
        .ParameterName = "@iTopN"
        .MySqlDbType = MySqlDbType.Int32
        .Direction = ParameterDirection.Input
        .Value = 2
    End With
    command.Parameters.Add(param3)
    command.Connection = conn
    conn.Open()
    Dim reader As MySqlDataReader = command.ExecuteReader()
    Dim stationlist As New List(Of String)
    If reader.HasRows() Then
        While reader.Read()
            stationlist.Add(reader("station_id").ToString())
        End While
    End If
    conn.Close()

    Return stationlist(0)  'bogus value until I get SP to work
End Function
于 2013-11-08T18:47:46.797 回答