-1

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

  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;
4

2 回答 2

1

这是一个只见树木不见森林的例子:

您正在构建您的 SqlCommand 命令就好了,但您从不使用它。相反,您创建一个正在使用的新 SqlCommand 命令。参数未添加到此 SqlCommand 对象。

于 2013-08-14T12:07:01.537 回答
1

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

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

检查以了解存储过程如何使用 c#

如何使用 C# 执行存储过程

于 2013-08-14T11:21:12.543 回答