解析查询以从C#中的SQLCE获取letest添加的记录ID时出错

时间:2014-02-03 06:33:47

标签: c# sql sql-server-ce

您好我正在尝试使用以下代码获取最新添加的记录ID: 此代码按This Link

开发
var con = db.OpenConnection();
try
 {
  var refund = "";
  if (rbRef.Checked)
  {
   refund = "Refundable";
  }
  else
  {
   refund = "Non-Refundable";
  }
  var cmd = new SqlCeCommand();
  const string sqlAddNew = @"INSERT INTO Income(iAmount,iByHand,iRecDate,iDesc,pId,iRefundable)"+"OUTPUT INSERTED.iId values (@amount,@byHand,@recDate,@description,@pId,@iRefundable)";

  var amount = txtInAmount.Text.Trim();
  var byHand = txtRecByHand.Text.Trim();
  var recDate = dtpExpenceDate.Value;
  var descrip = txtInDesc.Text.Trim();
  var pId = cmbPayee.SelectedValue;

  cmd=new SqlCeCommand(sqlAddNew,con);
  cmd.Parameters.AddWithValue("@amount", SqlDbType.Float).Value = amount;
  cmd.Parameters.AddWithValue("@byHand", SqlDbType.NVarChar).Value = byHand;
  cmd.Parameters.AddWithValue("@recDate", SqlDbType.DateTime).Value = recDate;
  cmd.Parameters.AddWithValue("@description", SqlDbType.NVarChar).Value = descrip;
  cmd.Parameters.AddWithValue("@pId", SqlDbType.Int).Value = pId;
  cmd.Parameters.AddWithValue("@iRefundable", SqlDbType.NVarChar).Value=refund;
  var count = cmd.ExecuteNonQuery();
  var id = (int) cmd.ExecuteScalar();
  MessageBox.Show(id.ToString());
  if (count > 0)
  {
   MessageBox.Show(@"Record inserted into Income");
   db.CloseConnection(con);
  }
  else
  {
   db.CloseConnection(con);
  }
 }
 catch (Exception e)
 {
  MessageBox.Show(e.ToString());
 }

当我运行此代码时,我收到以下错误:

There was an error parsing the query. 
[ Token line number = 1,Token line offset = 67,Token in error = OUTPUT ]

我不知道为什么我会收到此错误,这也是最简单的方法,或者还有其他方法可以做到这一点。

0 个答案:

没有答案