ExecuteScalar和ExecuteNonQuery冲突

时间:2019-04-05 14:00:27

标签: c# sql sql-server wpf

我遇到了ExecuteScalar的问题,它不允许我使用ExecuteNonQuery,但是我必须执行两个查询来选择最小日期(dateStart9和另一个表中所选项目的日期范围之间的最大date(dateEnd)。

sqliteCon.Open();
  if (sqliteCon.State == System.Data.ConnectionState.Open)
  {
    //query date non va a causa dell'execute scalar
    SqlCommand cmdDS = new SqlCommand("SELECT MIN(DateStartSE) AS DATESE FROM tabStoreExec JOIN tabList ON tabStoreExec.idSE=tabList.idL WHERE tabList.selection=1", sqliteCon);
    SqlCommand cmdDE = new SqlCommand("SELECT MAX(DateEndSE) AS DATEED FROM tabStoreExec JOIN tabList ON tabStoreExec.idSE=tabList.idL WHERE tabList.selection=1", sqliteCon);
    cmdDS.Parameters.AddWithValue("@DATESE", " DATESE");
    cmdDE.Parameters.AddWithValue("@DATEED", "DATEED");
    cmdDS.ExecuteNonQuery();
    cmdDE.ExecuteNonQuery();


  SqlCommand cmd1 = new SqlCommand("INSERT INTO tabStoricoDetail(NomeItem,ResItemDet,DateStartDet,DateEndDet) OUTPUT inserted.Id VALUES ('Prova','RProva',@DATESE,@DATEED)", sqliteCon);
  int generatedId = Convert.ToInt32(cmd1.ExecuteScalar());
  cmd1.Parameters.Clear();
  SqlCommand cmd2 = new SqlCommand("UPDATE tabStoreExec SET FK_TSD_id = @tsdId FROM tabList JOIN tabStoreExec ON tabList.idL = tabStoreExec.idSE WHERE tabList.selection=1 ", sqliteCon);
  cmd2.Parameters.AddWithValue("@tsdId", generatedId);
  cmd2.ExecuteNonQuery();

    MessageBox.Show("Dato Aggiunto");

  }
  sqliteCon.Close();

我已经解决了,但是错误是[声明了标量变量“ @DATESE”。 ]

2 个答案:

答案 0 :(得分:2)

似乎您应该将参数添加到cmd1cmdDS的{​​{1}} instad中。您应该通过以下命令调用cmdDE来获取值:

ExecuteScalar()

答案 1 :(得分:0)

在执行之前,您需要将参数@DATESE,@DATEED添加到cmd1