2013-08-27 132 views
0

我正在嘗試進行更新,以便我可以將數據從XML文件上載到SQL Server。但是,拋出以下異常Must declare the scalar variable...。我想我已經聲明瞭所需的變量。我查了大概10次,但是我看不出我出錯的地方。你可以嗎?如果是的話,請提供我的代碼編輯和答案,並解釋我出錯的地方。感謝您的幫助和您的時間。如何聲明一個標量變量?

sqltext="SET IDENTITY_INSERT HomeCareVisit ON update HomeCareVisit set [email protected],ScheduledDateTime= @ScheduledDateTime,[email protected], [email protected], [email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]"; 

SqlCommand Insertcommand = new SqlCommand(sqltext, conn); 
//SqlCommand Insertcommand = new SqlCommand("SET IDENTITY_INSERT HomeCareVisit ON update HomeCareVisit set [email protected],ScheduledDateTime= @ScheduledDateTime,[email protected], [email protected], [email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]"); 

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 .. "); 
+1

它告訴你你需要聲明哪個標量變量? – McKay

+0

@PatientNo但是從代碼中可以看出它是我聲明的第一個。 –

+0

你是說錯誤信息是@PatientNo變量沒有被聲明? – McKay

回答

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_INSERTUPDATE聲明沒有意義。

+0

謝謝你發現。但是它沒有解決問題。我仍然得到相同的例外。 –

+0

「必須聲明標量變量」@PatientNo「」是在刪除該行之前的消息,它已經轉移到「」必須聲明標量變量「@VisitRefNo」「。 –

+0

@GeorgeIanGuyMarsden - 同樣擺脫'adpter.InsertCommand .ExecuteNonQuery();'在添加參數之前。 –