0

我有一个Item_Order列的表

orderId, clintName, companyName, itemName, orderDate, qauntity, clintId_FK, itemId_FK, status

其中clintId_FK和分别是表和表item_FK的主键。ClintItem_Configuration

我为Item_Order表中的插入记录创建了一个存储过程。IE

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# 代码是

private void btnOrder_Click(object sender, EventArgs e)
{
        SqlConnection conn1 = new SqlConnection();
        try
        {
            SqlCommand inserOrder= new SqlCommand("Order_Create_SP", conn1);
            inserOrder.CommandType = CommandType.StoredProcedure;

            inserOrder.Parameters.Add("@orderId", SqlDbType.VarChar);
            inserOrder.Parameters["@orderId"].Value = oredrId.Text;

            inserOrder.Parameters.Add("@clintName", SqlDbType.VarChar);
            inserOrder.Parameters["@clintName"].Value = comboClint.Text;

            inserOrder.Parameters.Add("@itemName", SqlDbType.VarChar);
            inserOrder.Parameters["@itemName"].Value = comboItem.Text;

            inserOrder.Parameters.Add("@orderDate", SqlDbType.DateTime);

          inserOrder.Parameters["@orderDate"].Value=Convert.ToDateTime(orderDate.Text);
          inserOrder.Parameters.Add("@qauntity", SqlDbType.Int);
            inserOrder.Parameters["@qauntity"].Value = Convert.ToInt32(qauntity.Text);

            inserOrder.Parameters.Add("@stauts", SqlDbType.VarChar);
            inserOrder.Parameters["@stauts"].Value = "OPEN";

            inserOrder.Parameters.Add("@companyName", SqlDbType.VarChar);
            inserOrder.Parameters["@companyName"].Value = companyName.Text;

            inserOrder.Parameters.Add("@operation", SqlDbType.VarChar);
            inserOrder.Parameters["@operation"].Value = 0;

            inserOrder.ExecuteNonQuery();

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

谢谢你。

4

1 回答 1

0

不确定这是否是您的问题,但您的存储过程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
于 2012-07-24T16:54:26.977 回答