我正在尝试进行更新,以便我可以将数据从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 .. ");
答案 0 :(得分: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
语句毫无意义。