mysql参数添加了不求值
我有下面的代码,我试图调用一个mysql存储过程,因为我不能a)让存储过程在EF中工作,b)我也不能让EF LINQ查询工作,所以我要回到以前的基础。以下错误是VS要求ExecuteQuery行提供参数。这不是我打电话之前已经做过的吗mysql参数添加了不求值,mysql,stored-procedures,Mysql,Stored Procedures,我有下面的代码,我试图调用一个mysql存储过程,因为我不能a)让存储过程在EF中工作,b)我也不能让EF LINQ查询工作,所以我要回到以前的基础。以下错误是VS要求ExecuteQuery行提供参数。这不是我打电话之前已经做过的吗 Public Shared Function GetNearestWeatherStationSql(ByVal lat As Decimal, longi As Decimal) As String Const connectionString As S
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
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)
它并不漂亮,任何人都能感觉到它展示了一个合适的重构,但我将每个参数单独添加到集合中,然后使用阅读器访问
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
现在这样做会产生一个“数据库中找不到的过程或函数”