尝试将数据存储到sql数据库时,我收到以下异常:
Procedure or function 'myFunction' expects parameter '@username', which was not supplied.
这是我正在调用的存储过程:
@username varchar(20),
@password varchar(20),
@puntos int,
@partidaGanada bit
AS
BEGIN
DECLARE @id int
SET @id = (SELECT COUNT(*) AS id FROM Juegos)
if @id > 0
BEGIN
SET @id = (SELECT MAX(Juegos.id) FROM Juegos)
END
SET @id = @id + 1
INSERT INTO Juegos(Juegos.id, jugador_id, Juegos.partidaGanada, Juegos.puntos) values (@id, (SELECT (Jugador.id) FROM Jugador WHERE Jugador.username = @username AND Jugador.password = @password), @partidaGanada, @puntos)
END
这是'myFunction',我提供参数:
...
Dim parametros(3) As SqlParameter
parametros(0) = New SqlParameter("@username", game.Jugador.Nombre) //string
parametros(1) = New SqlParameter("@password", game.Jugador.Password) //string
parametros(2) = New SqlParameter("@puntos", game.Puntos) //integer
parametros(3) = New SqlParameter("@partidaGanada", game.PartidaGanada) //boolean
Dim cmd As New SqlCommand
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = procedureName
If (parameters IsNot Nothing) Then //parameters isnot nothing, I checked
cmd.Parameters.AddRange(parameters)
End If
openCn() //there are no issues with the connection to the database, I checked that aswell
cmd.ExecuteNonQuery()
closeCn()
...
这些是异常发生之前参数的值:
@username = "hello"
@password = "123"
@puntos = 50
@partidaGanada = false
尽管为所有参数提供了值,但为什么会出现此错误?
答案 0 :(得分:1)
在评论中提到 vishal ,您需要将这些参数添加到命令中。
在创建/定义所有参数后添加以下语句。
cmd.Parameters.AddRange(parametros)
答案 1 :(得分:1)
您是否在命令中加入了CommandType
?
答案 2 :(得分:1)
将命令类型更改为存储过程。 :)