将 nvarchar 值 'OrgID' 转换为数据类型 int 时转换失败

Conversion failed when converting the nvarchar value 'OrgID' to data type int

所以我正在自学如何在 SQL Server Management Studio 中执行存储过程,并且我正在尝试创建一个来替换我在 Visual Studio 中的 C# 代码中的字符串查询.我原来的代码是

 InsertQuery = "INSERT INTO License (OrgID, LicensingKey) VALUES (@OrgID, @LicensingKey)"; // Query to insert into the database table.
 SqlCommand cmd = new SqlCommand(InsertQuery, con);
 cmd.Parameters.Add("@LicensingKey", SqlDbType.Int).Value = LicensingKey;  
 cmd.Parameters.Add("@OrgID", SqlDbType.Int).Value = OrgID;
 cmd.ExecuteNonQuery();

其中 OrgID 和 LicensingKey 是通过此代码所在的方法传递的两个整数。

我在数据库中创建的存储过程是这样的:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[LicenseInsert]
@OrgID int = OrgID,
@LicensingKey int = LicensingKey

AS
BEGIN
SET NOCOUNT ON;
INSERT INTO License (OrgID, LicensingKey) 
VALUES (@OrgID, @LicensingKey)
END

现在我用这个替换了相同的代码:

InsertQuery = "EXEC LicenseInsert "; // Query to insert into the database table.
SqlCommand cmd = new SqlCommand(InsertQuery, con);
cmd.ExecuteNonQuery();

但这给了我 "Conversion failed when converting the nvarchar value 'OrgID' to data type int" 的错误,它说

cmd.ExecuteNonQuery(); 

是罪魁祸首。

有人可以告诉我我做错了什么吗? 谢谢!

我预计您会收到某种缺少参数的错误,但要执行存储过程,您只需使用存储过程名称并将命令类型更改为 StoredProcedure

InsertQuery = "LicenseInsert"; // Query to insert into the database table.
SqlCommand cmd = new SqlCommand(InsertQuery, con);
cmd.CommandType = CommandType.StoredProcedure;  // <------------

cmd.Parameters.Add("@LicensingKey", SqlDbType.Int).Value = LicensingKey;  
cmd.Parameters.Add("@OrgID", SqlDbType.Int).Value = OrgID;

cmd.ExecuteNonQuery();

我还认为您的存储过程定义无效。应该是:

ALTER PROCEDURE [dbo].[LicenseInsert]
@OrgID int,
@LicensingKey int