2016-10-09 1 views
-1

시스템을 리메이크하려고하는데 이전 시스템이 100 % 작동하지만, 변경하면 (ms 액세스 데이터베이스에서 많은 열을 추가했습니다) 각 텍스트 상자의 데이터를 삽입 할 때 올바른 형식이지만 "명령문에 삽입하는 동안 오류가 발생했습니다"라는 메시지가 계속 나타납니다.C# windowsform - 문에서 삽입 할 때 구문 오류가 발생합니다.

이것은 내 코드입니다. 제발 그 오류에 대한 쿼리를 읽고 시간을내어 가지고있어. 나는 데이터베이스뿐만 아니라 여기에서 각 필드의 영문 철자 또는 대문자를 두 번 확인했습니다.

try 
     { 
      connection.Open();          //open connection 
      OleDbCommand command = new OleDbCommand();    // command object , we can execute to validate our database 
      command.Connection = connection;      // make a connection for the command 

      command.CommandText = " insert into StudentsRecord([StudentID],Name,Section,Semester,MathPrelim,MathMidterm,MathFinals,MathAverage,MathFinalGrade,EnglishPrelim,EnglishMidterm,EnglishFinals,EnglishAverage,EnglishFinalGrade,SciencePrelim,ScienceMidterm,ScienceFinals,ScienceAverage,ScienceFinalGrade,StatisticsPrelim,StatisticsMidterm,StatisticsFinals,StatisticsAverage,StatisticsFinalGrade,ReadandWritePrelim,ReadandWriteMidterm,ReadandWriteFinals,ReadandWriteAverage,ReadandWriteFinalGrade) values ('" + txtStudentID.Text + "' , '" + txtName.Text + "' , '" + txtSection.Text + "' , '" + cmbSemester.SelectedItem + "', '" + txtMathp.Text + "' , '" + txtMathm.Text + "' , '" + txtMathf.Text + "' , '" + txtMatha.Text + "' , '" + txtMathFG.Text + "' , '" + txtEnglishp.Text + "' , '" + txtEnglishm.Text + "', '" + txtEnglishf.Text + "','" + txtEnglisha.Text + "','" + txtEnglishFG.Text + "','" + txtMathFG.Text + "','" + txtSciencep.Text + "','" +txtSciencem.Text+ "','" + txtSciencef.Text + "','" + txtSciencea.Text + "','" + txtScienceFG.Text + "','" + txtStatisticsp.Text + "','" + txtStatisticsm.Text + "','" + txtStatisticsf.Text + "','" + txtStatisticsa.Text + "','" + txtStatisticsFG.Text + "','" + txtReadandWritep.Text + "','" + txtReadandWritem.Text + "','" + txtReadandWritef.Text + "','" + txtReadandWritea.Text + "','" + txtReadandWriteFG.Text + "')"; 
      /* this is a string or a query used to execute. asterisk is used 
      to give you all column data from your database ,declaration of query */ 


      command.ExecuteNonQuery();   // this is used to inserting data , updating or deleting data , this command will execute the above query 
      MessageBox.Show(" Saved! "); 

     } 
     catch (Exception a) 
     { 
      MessageBox.Show(" Error " + a.Message); 
     } 
     connection.Close(); 
+0

오류 메시지 란 무엇이며 command.CommandText의 내용은 무엇입니까? – NineBerry

+0

오류 메시지가 "INSERT INTO 문에서 오류"이미 두 번 필드, 맞춤법 검사 및 대/소문자의 위치뿐만 아니라 그 위치를 확인했지만 여전히 오류가 발생합니다. commandtext는 쿼리입니다.이 구문은 이전 버전의 그레이딩 시스템에서 사용 된 구문입니다. 나는 일한다. 왜 그것이 지금 작동하지 않는지 전혀 모른다. – Sky

답변

2

난 당신이 29 개 필드가 첫 번째 부분에서 볼 수 있지만 삽입 된 필드는 또한 30 ... 을, 당신은 SQL 주입을 방지하기 위해 parameterized queries를 사용해야합니다.

+0

안녕하세요, 이미 삽입 된 필드의 추가 필드를 삭제했지만 오류가 여전히 발생합니다. 도대체, 나 do not는 무엇을 이제는해야하는지에 관해 안다. 이 구문은 내 이전 시스템 도움말들에 대해서만 작동하는 것 같습니다. – Sky

+0

Btw는이 시스템이 학교용으로 만 사용 되었기 때문에 어떤 종류의 쿼리도 가질 수 있으므로 걱정하지 않아도됩니다. 특히 이것은 방어입니다. 나는 이것을 다른 사람들이 사용하는 형식 체계로 만들지 않을 것이다. – Sky

0

오류를 알아 내기 위해 코드를 디버그하고 CommandText를 SSMS (SQL Sever Management Studio)에 붙여 넣어야합니다.

다른 제안 :

  • command.CommandText = 및 String.format ("StudentsRecord (삽입 ..) VALUES (@ ...)); SQL PARAMS는 (사용도록 SqlConnection를 SQL 인젝션
  • 피해야 할 CONN = ...)
0

이미 각 필드에 [ ]을 넣어 지금 문제를 해결 이런 일이 될 것이다 :.

insert into StudentsRecord(
    [StudentID],[Name],[Section],[Semester], 
    [MathPrelim],[MathMidterm],[MathFinals] 
) values ('"++"'..) ... etc. 

나를 도우 려던 모든 분들께서 좋은 하루 보내십시오!

관련 문제