简体   繁体   中英

'Procedure or function '...' expects parameter '...', which was not supplied.'

I was developing my project and this error occurred to me:

"System.Data.SqlClient.SqlException: 'Procedure or function' dbo.Bi_medicaoInset 'expects parameter' @ID_interno_disp ', which was not supplied.'"

I have already searched the internet but I can't find anything to help me.

C# code:

SqlConnection connString = new SqlConnection ("Data Source = (LocalDb) \\ MSSQLLocalDB; Initial Catalog = tupaidb; Integrated Security = True");
                    connString.Open ();
                    
SqlCommand command = new SqlCommand ();
command.Connection = connString;
command.CommandText = "[dbo]. [dbo.Bi_medicaoInset]";
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add ("@ID_interno_disp", SqlDbType.Int);
command.Parameters.Add ("@med_hum", SqlDbType.VarChar, (50)). Value = humidity + "%";
command.Parameters.Add ("@med_temp", SqlDbType.VarChar, (50)). Value = temperature + "ºC";
command.Parameters.Add ("@med_lpg", SqlDbType.VarChar, (50)). Value = lpgGas + "%";
command.Parameters.Add ("@med_co", SqlDbType.VarChar, (50)). Value = monoCarbo + "%";
command.Parameters.Add ("@med_fumo", SqlDbType.VarChar, (50)). Value = smoke + "%";
command.ExecuteNonQuery ();
connString.Close ();

Stored Procedure:

USE [tupaidb]
GO
/ ****** Object: StoredProcedure [dbo]. [Dbo.Bi_medicaoInset] Script Date: 5/18/2021 08:29:17 ****** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]. [Dbo.Bi_medicaoInset]
    @ID_interno_disp int,
    @med_hum Varchar (50),
    @med_temp Varchar (50),
    @med_lpg Varchar (50),
    @med_co Varchar (50),
    @med_fumo Varchar (50)
AT
BEGIN
    insert into Bi_medicao (ID_interno_disp, med_hum, med_temp, med_lpg, med_co, med_fumo)
    values ​​(@ ID_interno_disp, @ med_hum, @ med_temp, @ med_lpg, @ med_co, @ med_fumo)
END

I want the value of the column "ID_interno_disp" of the table "Bi_dispositivo" to pass to the other table with the same column name.

在此处输入图像描述

  1. take out the spaces
  2. supply a value (parameters with a null value are not sent )

so:

command.CommandText = "[dbo].[dbo.Bi_medicaoInset]";
// ...
command.Parameters.Add("@ID_interno_disp", SqlDbType.Int).Value = somethingHere;

You may also find it a lot easier to use tools like Dapper, which simplify the ADO.NET API:

using var conn = new SqlConnection ("...");
conn.Execute("[dbo].[dbo.Bi_medicaoInset]",
    new { ID_interno_disp = whatever,
       med_hum = humidity + "%",
       med_temp = temperature + "ºC",
       med_lpg = lpgGas + "%",
       med_co = monoCarbo + "%",
       med_fumo = smoke + "%"
    });

May be ID_interno_disp column is set as identity column. In that case there is no need to pass paramater for this column at all. so remove parameter for it. Add below after Insert statement

SELECT SCOPE_IDENTITY() AS 'Identity'

--this will return identity value inserted only within the current scope

SELECT @@IDENTITY AS 'Identity'; 

--this is not limited to a specific scope

This will give you the autoincremented Id for the latest record. And then you can use this id to insert in to other table Bi_dispositivo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM