2013-08-27 2 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 전달되지 않도록 고정되면. 너 필요해.

또한 UPDATE 문에 대한 SET IDENTITY_INSERT은 무의미합니다.

+0

그 점을 발견해 주셔서 감사합니다. 그러나 그것은 문제를 해결하지 못했습니다. 나는 여전히 같은 예외를 받고있다. –

+0

""@PatientNo "스칼라 변수를 선언해야 해당 행을 제거하기 전에 메시지가" "로 이동했습니다."스칼라 변수 "@VisitRefNo" "를 선언해야합니다. –

+0

@GeorgeIanGuyMarsden - 또한 'adpter.InsertCommand .ExecuteNonQuery();'매개 변수를 추가하기 전에. –

관련 문제