索引(从零开始)必须大于或等于零且小于参数列表的大小

时间:2013-01-04 00:05:36

标签: asp.net sql-server-2008

我试图从asp.net

添加到sql表的行

paremters sqlServer是:

[id] smallint IDENTITY (1, 1) NOT NULL ,
[purchaseNum] int NULL,
[purchaseDate] DateTime  NULL,
[prodId] smallint NULL,
[amount] int NULL,
[price] decimal NULL,
[paidStatus] char NULL,
Primary key (id)

并且asp.net中的代码是:

  private String BuildInsertCommand(Sale sale)
{
    String command;


    StringBuilder sb = new StringBuilder();
    // use a string builder to create the dynamic string
    sb.AppendFormat("Values('{0}', '{1}' ,'{2}', '{3}', '{4}','{5}', '{6}')", Convert.ToInt16(sale.ProductId), Convert.ToDateTime(sale.PurchaseDate), Convert.ToInt16(sale.ProductId), Convert.ToDecimal(sale.AmountOfItems),sale.TotalPrice, Convert.ToChar(sale.PaidStatus));
    String prefix = "INSERT INTO sale " + "(purchaseNum, purchaseDate,prodId, amount, price,paidStatus) ";

    command = prefix + sb.ToString();

    return command;
}

我不明白为什么会出现错误消息,

感谢

1 个答案:

答案 0 :(得分:0)

您定义了7个占位符

{0}', '{1}' ,'{2}', '{3}', '{4}','{5}', '{6}'

你传递了6个参数