1

我试图从 asp.net 向 sql 表添加行

参数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;
}

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

谢谢

4

1 回答 1

0

您定义了 7 个占位符

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

你传递了 6 个参数

于 2013-01-04T00:14:24.790 回答