如何声明标量变量

本文关键字:标量 变量 声明 何声明 | 更新日期: 2023-09-27 18:29:06

我正在尝试进行更新,以便将数据从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 .. ");

如何声明标量变量

主要问题是

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语句来说也是毫无意义的。