C# 在C中使用存储过程时,如何将数据保存在具有多个外键的表中#

C# 在C中使用存储过程时,如何将数据保存在具有多个外键的表中#,c#,stored-procedures,foreign-keys,C#,Stored Procedures,Foreign Keys,我有一个带有列的表Item\u Order orderId, clintName, companyName, itemName, orderDate, qauntity, clintId_FK, itemId_FK, status 其中,clintId_FK和item_FK分别是Clint和item_配置表的主键 我已经为Item\u Order表中的插入记录创建了一个存储过程。i、 e ALTER PROCEDURE [dbo].[Order_Create_SP] @orderId varc

我有一个带有列的表
Item\u Order

orderId, clintName, companyName, itemName, orderDate, qauntity, clintId_FK, itemId_FK, status
其中,
clintId_FK
item_FK
分别是
Clint
item_配置
表的主键

我已经为
Item\u Order
表中的插入记录创建了一个存储过程。i、 e

ALTER PROCEDURE [dbo].[Order_Create_SP]
@orderId varchar(50),
@clintName varchar(50),
@companyName varchar(50),
@itemName  varchar(50),
@orderDate datetime,
@qauntity int,
@status char(10),
@operation int

AS
DECLARE @id1 varchar(50)
DECLARE @id2 varchar(50)
BEGIN
SET NOCOUNT ON;

if @operation = 0
BEGIN

SELECT top(1)@id1 = clintId FROM dbo.Clint order by clintId desc
select top(1)@id2 =  itemId from dbo.Item_Configuration order by itemId desc

insert into Item_Order(orderId, clintName, companyName, itemName, orderDate, qauntity, clintId_FK, itemId_FK, status)
 values(@orderId, @clintName, @companyName, @itemName, @orderDate, @qauntity, @id1, @id2, 'OPEN')

END
END
我的C代码是

我正在做一些不兼容的事情?它不能正常工作。请帮忙


谢谢。

不确定这是否是您的问题,但在查找外键时,您的存储过程缺少
WHERE
子句。试着换成像这样的

SELECT top(1)@id1 = clintId FROM dbo.Clint WHERE Name = @clintName order by clintId desc
SELECT top(1)@id2 =  itemId from dbo.Item_Configuration WHERE name = @itemName order by itemId desc

是否有特定的错误消息发生?请澄清
工作不正常
。您是否有错误、意外行为、不安的感觉?您的c#代码不完整,请您完成后查看按钮的所有代码。你能给出更具体的错误消息吗?Meessage是关于SqlException和tds解析器的,你在哪里打开连接?
SELECT top(1)@id1 = clintId FROM dbo.Clint WHERE Name = @clintName order by clintId desc
SELECT top(1)@id2 =  itemId from dbo.Item_Configuration WHERE name = @itemName order by itemId desc