C#windowsform-在Insert into语句中获取语法错误
我正在尝试重新改造我的系统,我的旧系统100%正常工作,但当我更改某些内容(我在ms access数据库中添加了许多列)时,我在插入每个文本框的数据时使用了正确的格式,但它仍然显示“insert into语句中出错” 这是我的密码。请慢慢阅读查询,因为这只是我得到的错误。我仔细检查了数据库和此处每个字段的拼写或大小写C#windowsform-在Insert into语句中获取语法错误,c#,ms-access,C#,Ms Access,我正在尝试重新改造我的系统,我的旧系统100%正常工作,但当我更改某些内容(我在ms access数据库中添加了许多列)时,我在插入每个文本框的数据时使用了正确的格式,但它仍然显示“insert into语句中出错” 这是我的密码。请慢慢阅读查询,因为这只是我得到的错误。我仔细检查了数据库和此处每个字段的拼写或大小写 try { connection.Open(); //op
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();
我在第一部分看到有29个字段,但插入的字段是30。。。
此外,您应该使用以避免sql注入。您需要调试代码并将CommandText粘贴到SSMS(sql Server Management Studio)以找出错误 其他建议:
- command.CommandText=string.format(“插入StudentsRecord(…)值(@…));避免sql注入的sql参数
- 使用(SqlConnection conn=…)
[]
,如下所示:
insert into StudentsRecord(
[StudentID],[Name],[Section],[Semester],
[MathPrelim],[MathMidterm],[MathFinals]
) values ('"++"'..) ... etc.
感谢所有试图帮助我的人,祝你们愉快!什么是错误消息,command.CommandText的内容是什么?错误消息是“插入到语句中出错”“已经对字段、拼写和大小写以及它们的位置进行了双重检查,但仍然给了我这个错误。”。commandtext是查询,该语法是我在以前版本的评分系统中使用的语法。我工作。不知道为什么它现在不工作。嗨,我已经删除了插入字段上的额外字段,但是,错误仍然发生。见鬼,我不知道该怎么办了。这个语法似乎适用于我以前的系统help guysBtw我们不必担心有任何类型的查询,因为这个系统只用于学校用途,特别是这是一种防御。我不会让其他人正式使用这个系统。