我有一个ExecuteScalar问题,它不允许我使用ExecuteNonQuery,但是我必须执行两个查询来选择之间的最小日期(dateStart9和max 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”。
]

最佳答案

似乎您应该将参数添加到cmd1而不是cmdDScmdDE。您应该通过以下命令调用ExecuteScalar()来获取值:

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);
var DATESE = cmdDS.ExecuteScalar();
var DATEED = cmdDE.ExecuteScalar();


SqlCommand cmd1 = new SqlCommand("INSERT INTO tabStoricoDetail(NomeItem,ResItemDet,DateStartDet,DateEndDet) OUTPUT inserted.Id VALUES ('Prova','RProva',@DATESE,@DATEED)", sqliteCon);
cmd1.Parameters.AddWithValue("@DATESE", DATESE);
cmd1.Parameters.AddWithValue("@DATEED", DATEED);

10-07 16:48