我需要知道为什么我仍然遇到此错误
存储过程需要未提供的参数
但是我实际上是在发送此参数。
数据库中的存储过程如下:
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();
有什么想法吗?
答案 0 :(得分:0)
由于某种原因,您两次创建了命令,第二个实例化替换了第一个实例,但是在第二个实例中,您没有设置命令类型,因此您的参数被忽略了。
尝试:
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = SVC_BUSCA_MEDIO_LANDING;
command.Parameters.AddWithValue("@rut", rut);
答案 1 :(得分:-2)
您正在使用:
SqlCommand command = new SqlCommand(SVC_BUSCA_MEDIO_LANDING);
但是您要在以下位置重置命令
:command = new SqlCommand(command.CommandText, connection);
请尝试:
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "SVC_BUSCA_MEDIO_LANDING";
command = new SqlCommand(command.CommandText, connection);
command.Parameters.AddWithValue("@rut", rut);