我正在构建一个C#WPF应用程序。我的应用包含带有多个列的ListView。我在Listview中添加了列和绑定与XAML
<ListView x:Name="receiptList" HorizontalAlignment="Left" Height="209.987" VerticalAlignment="Top" Width="645" Margin="111,135.748,0,0" IsEnabled="False" >
<ListView.View>
<GridView>
<GridViewColumn Header="ReceiptID" Width="60" DisplayMemberBinding="{Binding ReceiptID}"/>
<GridViewColumn Header="ItemNo" Width="50" DisplayMemberBinding="{Binding ItemNo}"/>
<GridViewColumn Header="ItemName" Width="240" DisplayMemberBinding="{Binding ItemName}"/>
<GridViewColumn Header="PackingSize" Width="100" DisplayMemberBinding="{Binding PackingSize}"/>
<GridViewColumn Header="Quantity" Width="50" DisplayMemberBinding="{Binding Quantity}"/>
<GridViewColumn Header="Price" Width="50" DisplayMemberBinding="{Binding Price}"/>
<GridViewColumn Header="ExpiryDate" Width="100" DisplayMemberBinding="{Binding ExpiryDate}"/>
</GridView>
</ListView.View>
</ListView>
现在借助按钮我在Listview中添加行:
private void addItems_Click(object sender, RoutedEventArgs e)
{
AddItems addItems = new AddItems(hhk,2,"ABC","36X63",3,opop,"22-11-2014");
receiptList.Items.Add(addItems);
}
AddItems类:
class AddItems
{
public int ReceiptID { get; set; }
public int ItemNo { get; set; }
public String ItemName { get; set; }
public String PackingSize { get; set; }
public int Quantity { get; set; }
public float Price { get; set; }
public String ExpiryDate { get; set; }
public AddItems()
{
}
public AddItems(int oid, int itNo, string itName, string itSize, int qua, float pri, String eDate)
{
this.ReceiptID = oid;
this.ItemNo = itNo;
this.ItemName = itName;
this.PackingSize = itSize;
this.Quantity = qua;
this.Price = pri;
this.ExpiryDate = eDate;
}
}
现在我想添加一个方法来计算在AddItems中添加了多少项,并且我还希望借助循环从AddItems中检索项目。
答案 0 :(得分:2)
这是 NOT 一个好主意。我会解释原因:
请自己和您的用户帮忙,从一开始就正确地做到:让数据库在插入时分配订单ID或创建更新为新订单的数字系列表查询订单ID时的ID。
实施例
表NumberSeries可能包含以下字段:
SeriesName MinID MaxID CurrentValue
Orders 1 999999 817
以下存储过程将安全地获取下一个可能的订单ID !
CREATE PROCEDURE spGetNextNo
@seriesName
AS BEGIN
DECLARE @newNo TABLE (Value INT)
UPDATE NumberSeries
SET
CurrentValue = CurrentValue + 1
OUTPUT INSERTED.CurrentValue INTO @newNo
WHERE
SeriesName = @seriesName AND
CurrentValue < MaxID
SELECT TOP 1 Value FROM @newNo
END
然后您可以在C#中使用它,如下所示:
using (SqlCommand cmd = new SqlCommand("spGetNextNo", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@seriesName", "Orders");
object res = cmd.ExecuteScalar();
if (res == null || res == DBNull.Value)
throw new InvalidOperationException("Number series is used up");
return (int)res;
}
编辑我从评论和以下作品中检查了HABO的方法:
CREATE PROCEDURE spGetNextNo
@seriesName
AS BEGIN
DECLARE @newNo INT
UPDATE NumberSeries
SET
@newNo = CurrentValue += 1
WHERE
SeriesName = @seriesName AND
CurrentValue < MaxID
SELECT @newNo
END
编辑您在评论中询问是否可以在不使用存储过程的情况下进行此操作。我实际上没有从C#中尝试过这个,但我在SQL Management Studio中使用测试表输入了以下内容并且它有效。
exec('declare @var int; update tTest set @var = test += 1; select @var;')
在我的情况下,这会产生正确的结果。您现在可以尝试使用SqlCommand
执行上述语句,如下所示:
using (SqlCommand cmd = new SqlCommand("exec('declare @var int; update tTest set @var = test += 1; select @var;')", conn)
{
object o = cmd.ExecuteScalar();
...
}
当然您也可以使用我上面建议的数字系列表,并根据需要调整查询。
这也值得一试。 OTOH,你为什么不想使用存储过程?