必须声明标量变量@resident

时间:2013-08-14 10:33:13

标签: c# .net sql sql-server

我已经声明了变量并且它返回了正确的值但是我不明白为什么我仍然会收到此错误。

  sqlString.Append( " SELECT  Medication.Description + ' - ' + Medication.Strength + ' - ' + Medication.MedicationCode as DrugName, Resident.Title + ' ' + Resident.firstName + ' ' + Resident.Surname AS Resident, MedicationDispenseTimeslot.MedicationDosageBarcode, 0 AS checkBox " );
             sqlString.Append( " FROM Medication INNER JOIN MedicationDispense ON Medication.MedicationID = MedicationDispense.MedicationID INNER JOIN Resident ON MedicationDispense.ResidentID = Resident.ResidentID INNER JOIN MedicationDispenseStatus ON MedicationDispense.MedicationDispenseStatusID = MedicationDispenseStatus.MedicationDispenseStatusID INNER JOIN MedicationDispenseTimeslot ON MedicationDispense.MedicationDispenseID = MedicationDispenseTimeslot.MedicationDispenseID WHERE     (MedicationDispenseStatus.MedicationDispenseStatusID = 2) and  MedicationDispense.MedicationDispenseID in (select MedicationDispenseID from dbo.MedicationDispenseHistory where DATEDIFF(d, 0, DateTimeStamp) =DATEDIFF(d, 0, GETDATE()) and MedicationDispenseStatusID=2) AND MedicationDispense.ResidentID = @residentID " );
             sqlString.Append( " AND   (MedicationDispense.MedicationDispenseStatusID > 1) AND ");
             sqlString.Append( "    1 = (CASE WHEN MedicationDispense.StopDate IS NULL " );
             sqlString.Append( "                 THEN (CASE WHEN MedicationDispense.StartDate <= @periodEndDate THEN 1 ELSE 0 END) " );
             sqlString.Append( "            ELSE " );
             sqlString.Append( "                CASE WHEN (MedicationDispense.StartDate between @periodStartDate and @periodEndDate) " );
             sqlString.Append( "                                OR " );
             sqlString.Append( "                          (MedicationDispense.StopDate between @periodStartDate and @periodEndDate) " );
             sqlString.Append( "                                OR " );
             sqlString.Append( "                          (MedicationDispense.StartDate < @periodStartDate and MedicationDispense.StopDate > @periodEndDate) " );
             sqlString.Append( "    GROUP BY Medication.Description + ' - ' + Medication.Strength + ' - ' + Medication.MedicationCode, Resident.Title + ' ' + Resident.firstName + ' ' + Resident.Surname , MedicationDispenseTimeslot.MedicationDosageBarcode " );

             command = new SqlCommand(sqlString.ToString(), connection);
             command.Parameters.Add( "@residentID", SqlDbType.Int ).Value = residentID.ToString();
             command.Parameters.Add("@periodStartDate", SqlDbType.DateTime).Value = Helper.GetValue(periodStartDate);
             command.Parameters.Add("@periodEndDate", SqlDbType.DateTime).Value = Helper.GetValue(periodEndDate);

修改

  command = new SqlCommand( sqlString.ToString(), connection );

     SqlDataAdapter dataAdapter = new SqlDataAdapter( command );
     dataAdapter.Fill( ds, "dataTable" );

     return ds;

2 个答案:

答案 0 :(得分:1)

如果你在串联字符串中编写sql会出现这个问题,你可以将静态查询编写为连接字符串,因为它会转到SQL并直接执行但是在这里我可以理解,如果你看到 sqlString的输出。 ToString()它包含一些变量,如 @residentID,@periodEndDate等,当它执行时它在contextvand中不可用,也没有定义为参数,这就是你得到错误的原因必须声明标量变量@resident ,即使你将它作为参数传递,但我认为在连接字符串的情况下你不能传递参数,。

因此,最好使用存储过程,这是为了一次执行动态查询和多个查询。在存储参数中,您可以定义参数并为它们传递值。

使用c#

检查存储过程的工作原理

How to execute stored procedure using C#

答案 1 :(得分:1)

这是一个看不到树木的例子:

您正在构建SqlCommand命令,但是您从未使用它。而是创建一个正在使用的新SqlCommand命令。参数不会添加到此SqlCommand对象。