0

我正在尝试进行更新,以便可以将 XML 文件中的数据上传到 SQL Server。但是,会引发以下异常Must declare the scalar variable...。我想我已经声明了所需的变量。我已经检查了大约 10 次,但我看不出哪里出错了。你能?如果是,请提供对我的代码的编辑以及答案和我哪里出错的解释。感谢您的帮助和您的时间。

sqltext="SET IDENTITY_INSERT HomeCareVisit ON update HomeCareVisit set PatientNo=@PatientNo,ScheduledDateTime= @ScheduledDateTime,TreatmentInstructions=@TreatmentInstructions, MedicalStaffID=@MedicalStaffID, Priority=@Priority,ActualVisitDateTime=@ActualVisitDateTime,TreatmentProvided=@TreatmentProvided,Prescription=@Prescription,AdvisoryNotes=@AdvisoryNotes,FurtherVisitRequired=@FurtherVisitRequired where VisitRefNo=@VisitRefNo";

SqlCommand Insertcommand = new SqlCommand(sqltext, conn);
//SqlCommand Insertcommand = new SqlCommand( "SET IDENTITY_INSERT HomeCareVisit ON update HomeCareVisit set PatientNo=@PatientNo,ScheduledDateTime= @ScheduledDateTime,TreatmentInstructions=@TreatmentInstructions, MedicalStaffID=@MedicalStaffID, Priority=@Priority,ActualVisitDateTime=@ActualVisitDateTime,TreatmentProvided=@TreatmentProvided,Prescription=@Prescription,AdvisoryNotes=@AdvisoryNotes,FurtherVisitRequired=@FurtherVisitRequired where VisitRefNo=@VisitRefNo");

adpter.InsertCommand = Insertcommand;
adpter.InsertCommand.ExecuteNonQuery();

try
{
    using (Insertcommand)
    {                         
        Insertcommand.Parameters.AddWithValue("@PatientNo", PatientNo);
        Insertcommand.Parameters.AddWithValue("@FurtherVisitRequired", FurtherVisitRequired);
        Insertcommand.Parameters.AddWithValue("@AdvisoryNotes", AdvisoryNotes);
        Insertcommand.Parameters.AddWithValue("@Prescription", Prescription);
        Insertcommand.Parameters.AddWithValue("@TreatmentProvided", TreatmentProvided);
        Insertcommand.Parameters.AddWithValue("@ActualVisitDateTime",ActualVisitDateTime);
        Insertcommand.Parameters.AddWithValue("@Priority", Priority);
        Insertcommand.Parameters.AddWithValue("@ScheduledDateTime", ScheduledDateTime);
        Insertcommand.Parameters.AddWithValue("@TreatmentInstructions", TreatmentInstructions);
        Insertcommand.Parameters.AddWithValue("@MedicalStaffID", MedicalStaffID);

        Insertcommand.ExecuteNonQuery();

        MessageBox.Show("updated");
    }

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

conn.Close();
MessageBox.Show("Done .. ");
4

1 回答 1

2

主要问题是

adpter.InsertCommand.ExecuteNonQuery();

在添加参数之前发生。我对数据适配器不是特别熟悉,但我想您应该在调用时删除该行

Insertcommand.ExecuteNonQuery();

反正以后。

一旦你确定你的 SQL 是

SET IDENTITY_INSERT HomeCareVisit ON;

UPDATE HomeCareVisit
SET    PatientNo = @PatientNo,
       ScheduledDateTime = @ScheduledDateTime,
       TreatmentInstructions = @TreatmentInstructions,
       MedicalStaffID = @MedicalStaffID,
       Priority = @Priority,
       ActualVisitDateTime = @ActualVisitDateTime,
       TreatmentProvided = @TreatmentProvided,
       Prescription = @Prescription,
       AdvisoryNotes = @AdvisoryNotes,
       FurtherVisitRequired = @FurtherVisitRequired
WHERE  VisitRefNo = @VisitRefNo 

但是您没有@VisitRefNo作为参数传入。你需要。

SET IDENTITY_INSERT对于声明也是毫无意义的UPDATE

于 2013-08-27T12:45:55.920 回答