有人可以告诉我为什么我得到以下错误,当我运行我的代码时,我不确定我的 sql 语句是否有问题,因为这似乎没问题,但我在下面添加了它,以便我可以得到一个第二个意见
“从字符串转换日期/时间时转换失败”
public static int GetConveyorProductionCount(string machineNameV, string StartTimeV, string EndTimeV)
{
try
{
int count;
SqlParameter param01 = new SqlParameter("@param01", SqlDbType.VarChar, 5);
param01.Value = machineNameV;
SqlParameter param02 = new SqlParameter("@param02", SqlDbType.VarChar, 5);
param02.Value = StartTimeV;
SqlParameter param03 = new SqlParameter("@param03", SqlDbType.VarChar, 5);
param03.Value = EndTimeV;
SqlCommand getConveyorProductionSC = new SqlCommand("SELECT cast([" + machineNameV + "] as int) FROM VWCONVEYORPRODUCTION WHERE([DA_TE] BETWEEN @param02 AND @param03)", myConnection);
getConveyorProductionSC.Parameters.Add(param01);
getConveyorProductionSC.Parameters.Add(param02);
getConveyorProductionSC.Parameters.Add(param03);
myConnection.Open();
object result = getConveyorProductionSC.ExecuteScalar();
myConnection.Close();
if (result == DBNull.Value)
{
count = 0;
}
else
{
count = Convert.ToInt32(result);
}
return count;
}
catch (Exception e)
{
throw new Exception("Error retrieving the Conveyor production count. Error: " + e.Message);
}