簡體   English   中英

'程序或 function '...' 需要參數 '...',但未提供。

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

我正在開發我的項目,這個錯誤發生在我身上:

“System.Data.SqlClient.SqlException:'程序或函數'dbo.Bi_medicaoInset'期望參數'@ID_interno_disp',未提供。'”

我已經在互聯網上搜索過,但找不到任何可以幫助我的東西。

C# 代碼:

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 ();

存儲過程:

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

我希望表“Bi_dispositivo”的列“ID_interno_disp”的值傳遞給具有相同列名的另一個表。

在此處輸入圖像描述

  1. 取出空格
  2. 提供一個值(不發送具有null值的參數)

所以:

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

您可能還會發現使用像 Dapper 這樣的工具容易,它簡化了 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 + "%"
    });

可能是 ID_interno_disp 列被設置為標識列。 在這種情況下,根本不需要為此列傳遞參數。 所以刪除它的參數。 在插入語句之后添加以下內容

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

這將為您提供最新記錄的自動遞增 ID。 然后你可以使用這個 id 插入到其他表 Bi_dispositivo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM