我需要知道为什么我仍然收到此错误
Stored procedure expects parameter which was not supplied
但我实际上是在发送这个参数。
数据库中的存储过程如下所示:
CREATE PROCEDURE SVC_BUSCA_MEDIO_LANDING
(@rut VARCHAR)
AS
BEGIN
SELECT utm_source
FROM landing_formulario
WHERE rut = @rut
END
还有我的 .net 代码:
string result = string.Empty;
string connString = System.Configuration.ConfigurationManager.AppSettings["StPazWeb"].ToString();
string SVC_BUSCA_MEDIO_LANDING = "SVC_BUSCA_MEDIO_LANDING";
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
try
{
SqlCommand command = new SqlCommand(SVC_BUSCA_MEDIO_LANDING);
command.CommandType = CommandType.StoredProcedure;
command = new SqlCommand(command.CommandText, connection);
command.Parameters.AddWithValue("@rut", rut);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
result = (string)reader["utm_source"];
}
}
catch (SqlException ex)
{
throw new Exception("Oops!." + ex.Message);
}
}
return result.ToString();
知道会发生什么吗?
最佳答案
出于某种原因,您创建了两次命令,第二个实例化替换了第一个实例化,但是在第二个实例化中您没有设置命令类型,因此您的参数被忽略。
尝试:
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = SVC_BUSCA_MEDIO_LANDING;
command.Parameters.AddWithValue("@rut", rut);
关于c# - 存储过程需要参数但实际上提供了参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59326452/