当我将项目输入数据库表并在网页上添加这些项目时,我会遇到此问题。调试后我发现它是这行item.Add(item);.存储过程确实返回数据库中的项目数量和请购单号码上的项目数。这就是enter image description here的样子,这就是我所期望的enter image description here。我只输入了两个输入的项目,因为我输入了四个项目
存储过程
@Req_No varchar (50)
AS
BEGIN
SELECT a.ITEMNMBR, a.ITEMDESC, ab.employee_id, ab.department,ab.employee_name,quantity_requested,b.expense_acc, c.ACTDESCR+'/'+c.ACTNUMBR_1+'-'+c.ACTNUMBR_2 [Expense_Acc],
b.unit_of_measure
FROM [TWCL].[dbo].IV00101 a inner Join RequisitionItem b on a.ITEMNMBR = b.item_no
Inner Join Requisition ab
on ab.Req_No = b.Req_No
Inner Join [TWCL].dbo.GL00100 c
on b.expense_acc = c.ACTINDX
where b.Req_No = @Req_No
模型
public List<Item> getRequestItemByRquisition(string Req_No)
{
List<Item> items = new List<Item>();
SqlConnection TWCLOPConnect = new SqlConnection(connectionString.ToString());
SqlCommand itemscommand = new SqlCommand();
SqlDataReader itemRdr;
itemscommand.CommandText = "requisition_sp_getItemNum ";
itemscommand.CommandType = CommandType.StoredProcedure;
itemscommand.Connection = TWCLOPConnect;
itemscommand.Parameters.Add("@Req_No", SqlDbType.VarChar).Value = Req_No;
try
{
TWCLOPConnect.Open();
itemRdr = itemscommand.ExecuteReader();
while (itemRdr.Read())
{
Item item = new Item();
item.itemNumber = itemRdr.GetString(0);
item.description = itemRdr.GetString(1);
item.price = Convert.ToDouble(itemRdr[3]);
item.quantity = Convert.ToDouble(itemRdr[4]);
item.expense_account.index = itemRdr.GetInt32(5);
item.expense_account.account_desc = itemRdr.GetString(6);
item.selecteduomtext = itemRdr.GetString(8);
items.Add(item);
}
itemRdr.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
TWCLOPConnect.Close();
}
return items;
}
答案 0 :(得分:0)
您的存储过程返回重复的行:
SELECT a.ITEMNMBR, a.ITEMDESC, ab.employee_id, ab.department,ab.employee_name,quantity_requested,b.expense_acc, c.ACTDESCR+'/'+c.ACTNUMBR_1+'-'+c.ACTNUMBR_2 [Expense_Acc],
b.unit_of_measure
FROM [TWCL].[dbo].IV00101 a inner Join RequisitionItem b on a.ITEMNMBR = b.item_no
Inner Join Requisition ab
on ab.Req_No = b.Req_No
Inner Join [TWCL].dbo.GL00100 c
on b.expense_acc = c.ACTINDX
where b.Req_No = @Req_No
在sql中运行此存储过程,您将看到它。
有时使用内连接从DB返回的不匹配或额外数据很容易。 一次删除一个内部连接并检查。
所有这一行代码都是:
itemRdr = itemscommand.ExecuteReader();
while (itemRdr.Read())
{
Item item = new Item();
item.itemNumber = itemRdr.GetString(0);
item.description = itemRdr.GetString(1);
item.price = Convert.ToDouble(itemRdr[3]);
item.quantity = Convert.ToDouble(itemRdr[4]);
item.expense_account.index = itemRdr.GetInt32(5);
item.expense_account.account_desc = itemRdr.GetString(6);
item.selecteduomtext = itemRdr.GetString(8);
items.Add(item);
}
读取您返回的行,当它仍在读取时,它将添加行。 这意味着您的存储过程返回了重复的行数。