我正在尝试通过.Net Framework中的c#修改我的Microsoft Access数据库中的记录。这是我的执行代码,但我一直从INSERT语句中得到一个语法错误,无法找出原因,有人可以帮忙吗?我也遇到了一些问题,其中SELECT语句没有返回我知道数据库中存在的任何数据。我已经好几天了,需要一双新眼睛,我能做错什么?
这是错误:
错误:System.Data.OleDb.OleDbException(0x80040E14):INSERT INTO语句中的语法错误。 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object& executeResult)at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)at System。 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method)中的System.Data.OleDb.OleDbCommand.ExecuteNonQuery()处于ABM_Office_System.UpdateABMDatabaseWindow.b__0的Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior,Object& executeResult) ()...
for (int i = 0; i < hmuFinalList.Count; i++)
{
Dispatcher.Invoke(() =>
{
stsW.StatusLbl.Content = "Updating " + hmuFinalList[i].MName + "'s records";
});
readcmd.Parameters.Clear();
readcmd.CommandText = "SELECT * FROM [ABM] WHERE [M Name]=@";
readcmd.Parameters.AddWithValue("@", hmuFinalList[i].MName);
Console.WriteLine("READCMD: " + readcmd.CommandText);
Console.WriteLine("ReadcmdTRANSACTION: ");
affected = readcmd.ExecuteNonQuery();
Console.WriteLine("affected: " + affected.ToString());
if (affected > 0) //UPDATE TIME!
{
executecmd.Parameters.Clear();
executecmd.CommandText = "UPDATE [ABM] SET "+
"[M Type]=@," +
"[M Component]=@," +
"[M Birth Date]=@," +
"[M Visa Expiration Date]=@," +
"[M Residence Nation]=@," +
"[M MTC]=@," +
"[M MTC Date]=@," +
"[M Arrival Date]=@," +
"[M Release Date]=@, " +
"[M Past Position]=@," +
"[M Past Position Start Date]=@," +
"[M Current Position Start Date]=@," +
"[M Past Area]=@," +
"[M Past Area Start Date],=@" +
"[M Current Area Start Date]=@," +
"[M Title - English]=@," +
"[M Title - Non-English]=@," +
"[M HMU Correspondent Type]=@," +
"[M HMU Correspondent Last Name]=@," +
"[M HMU Correspondent Given Name]=@," +
"[M HMU Correspondent Language]=@," +
"[M HMU Correspondent Comment]=@," +
"[M HMU Correspondent Title]=@," +
"[M HMU Correspondent Relationship]=@," +
"[M HMU Correspondent Pronoun - Subject]=@," +
"[M HMU Correspondent Pronoun - Object]=@," +
"[M HMU Correspondent Pronoun - Possessive]=@," +
"[M HMU Correspondent Pronoun - Other]=@," +
"[M Previous Address Line 1]=@," +
"[M Previous Address City]=@," +
"[M Previous Address State/Province]=@,"
+ "[M Previous Address Postal Code]=@," +
"[M Previous Address Country]=@," +
"[M Type Plural]=@," +
"[M Transfer Effective Date]=@," +
"[M Position is a Promotion]=@," +
"[M HMU Correspondent Pronoun Cap - Subject]=@," +
"[M HMU Correspondent Pronoun Cap - Object]=@," +
"[M HMU Correspondent Pronoun Cap - Possessive]=@," +
"[M HMU Correspondent Pronoun Cap - Other]=@," +
"[M HMU Correspondent Title - Envelope]=@," +
"[M Where Born]=@" +
"WHERE [M Name]=@";
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MType) ? (object)DBNull.Value : hmuFinalList[i].MType);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MComponent) ? (object)DBNull.Value : hmuFinalList[i].MComponent);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MBirthDate) ? (object)DBNull.Value : hmuFinalList[i].MBirthDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MVisaExpirationDate) ? (object)DBNull.Value : hmuFinalList[i].MVisaExpirationDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MResidenceNation) ? (object)DBNull.Value : hmuFinalList[i].MResidenceNation);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MMTC) ? (object)DBNull.Value : hmuFinalList[i].MMTC);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MMTCDate) ? (object)DBNull.Value : hmuFinalList[i].MMTCDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MArrivalDate) ? (object)DBNull.Value : hmuFinalList[i].MArrivalDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MReleaseDate) ? (object)DBNull.Value : hmuFinalList[i].MReleaseDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastPosition) ? (object)DBNull.Value : hmuFinalList[i].MPastPosition);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastPositionStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MCurrentPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentPositionStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastArea) ? (object)DBNull.Value : hmuFinalList[i].MPastArea);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPastAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastAreaStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MCurrentAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentAreaStartDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTitleEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleEnglish);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTitleNonEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleNonEnglish);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentType) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentType);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLastName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLastName);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentGivenName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentGivenName);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLanguage) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLanguage);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentComment) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentComment);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitle) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitle);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentRelationship) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentRelationship);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounSubject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounObject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounPossessive);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounOther);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressLine1) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressLine1);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCity) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCity);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressStateProvince) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressStateProvince);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressPostalCode) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressPostalCode);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCountry) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCountry);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTypePlural) ? (object)DBNull.Value : hmuFinalList[i].MTypePlural);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MTransferEffectiveDate) ? (object)DBNull.Value : hmuFinalList[i].MTransferEffectiveDate);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MPositionisaPromotion) ? (object)DBNull.Value : hmuFinalList[i].MPositionisaPromotion);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapSubject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapObject);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapPossessive);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapOther);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitleEnvelope) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitleEnvelope);
executecmd.Parameters.AddWithValue("@", string.IsNullOrEmpty(hmuFinalList[i].MWhereBorn) ? (object)DBNull.Value : hmuFinalList[i].MWhereBorn);
executecmd.Parameters.AddWithValue("@", hmuFinalList[i].MName);
Console.WriteLine("HMU UPDATE EXECUTION: " + executecmd.CommandText);
executecmd.ExecuteNonQuery();
}
else //INSERT TIME!
{
executecmd.Parameters.Clear();
executecmd.CommandText = "INSERT INTO [ABM] ([M Name],[M Type],[M Component],[M Birth Date],[M Visa Expiration Date],[M Residence Nation],"+
"[M MTC]," +
"[M MTC Date],"+
"[M Arrival Date],"+
"[M Release Date], "+
"[M Past Position],"+
"[M Past Position Start Date]," +
"[M Current Position Start Date]," +
"[M Past Area],"+
"[M Past Area Start Date],"+
"[M Current Area Start Date],"+
"[M Title - English],"+
"[M Title - Non-English],"+
"[M HMU Correspondent Type],"+
"[M HMU Correspondent Last Name]," +
"[M HMU Correspondent Given Name]," +
"[M HMU Correspondent Language]," +
"[M HMU Correspondent Comment]," +
"[M HMU Correspondent Title]," +
"[M HMU Correspondent Address Line 1]," +
"[M HMU Correspondent Relationship]," +
"[M HMU Correspondent Pronoun - Subject]," +
"[M HMU Correspondent Pronoun - Object]," +
"[M HMU Correspondent Pronoun - Possessive]," +
"[M HMU Correspondent Pronoun - Other]," +
"[M Previous Address Line 1],"+
"[M Previous Address City]," +
"[M Previous Address State/Province],"+
"[M Previous Address Postal Code]," +
"[M Previous Address Country],"+
"[M Type Plural]," +
"[M Transfer Effective Date],"+
"[M Position is a Promotion]," +
"[M HMU Correspondent Pronoun Cap - Subject]," +
"[M HMU Correspondent Pronoun Cap - Object]," +
"[M HMU Correspondent Pronoun Cap - Possessive]," +
"[M HMU Correspondent Pronoun Cap - Other]," +
"[M HMU Correspondent Title - Envelope]," +
"[M Where Born],)"+
"VALUES(@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@,@)";
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MName) ? (object)DBNull.Value : hmuFinalList[i].MName);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MType) ? (object)DBNull.Value : hmuFinalList[i].MType);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MComponent) ? (object)DBNull.Value : hmuFinalList[i].MComponent);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MBirthDate) ? (object)DBNull.Value : hmuFinalList[i].MBirthDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MVisaExpirationDate) ? (object)DBNull.Value : hmuFinalList[i].MVisaExpirationDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MResidenceNation) ? (object)DBNull.Value : hmuFinalList[i].MResidenceNation);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MMTC) ? (object)DBNull.Value : hmuFinalList[i].MMTC);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MMTCDate) ? (object)DBNull.Value : hmuFinalList[i].MMTCDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MArrivalDate) ? (object)DBNull.Value : hmuFinalList[i].MArrivalDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MReleaseDate) ? (object)DBNull.Value : hmuFinalList[i].MReleaseDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastPosition) ? (object)DBNull.Value : hmuFinalList[i].MPastPosition);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastPositionStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MCurrentPositionStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentPositionStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastArea) ? (object)DBNull.Value : hmuFinalList[i].MPastArea);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPastAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MPastAreaStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MCurrentAreaStartDate) ? (object)DBNull.Value : hmuFinalList[i].MCurrentAreaStartDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTitleEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleEnglish);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTitleNonEnglish) ? (object)DBNull.Value : hmuFinalList[i].MTitleNonEnglish);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentType) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentType);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLastName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLastName);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentGivenName) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentGivenName);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentLanguage) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentLanguage);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentComment) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentComment);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitle) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitle);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentAddressLine1) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentAddressLine1);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentRelationship) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentRelationship);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounSubject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounObject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounPossessive);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounOther);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressLine1) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressLine1);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCity) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCity);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressStateProvince) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressStateProvince);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressPostalCode) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressPostalCode);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPreviousAddressCountry) ? (object)DBNull.Value : hmuFinalList[i].MPreviousAddressCountry);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTypePlural) ? (object)DBNull.Value : hmuFinalList[i].MTypePlural);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MTransferEffectiveDate) ? (object)DBNull.Value : hmuFinalList[i].MTransferEffectiveDate);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MPositionisaPromotion) ? (object)DBNull.Value : hmuFinalList[i].MPositionisaPromotion);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapSubject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapSubject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapObject) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapObject);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapPossessive) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapPossessive);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentPronounCapOther) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentPronounCapOther);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].CorrespondentTitleEnvelope) ? (object)DBNull.Value : hmuFinalList[i].CorrespondentTitleEnvelope);
executecmd.Parameters.AddWithValue("@",string.IsNullOrEmpty(hmuFinalList[i].MWhereBorn) ? (object)DBNull.Value : hmuFinalList[i].MWhereBorn);
Console.WriteLine("HMU INSERT EXECUTION: " + executecmd.CommandText);
executecmd.ExecuteNonQuery();
}
}
这让我抓狂!
答案 0 :(得分:1)
您的插入列列表以逗号结尾,不应该以逗号结尾。
替换
"[M Where Born],)"+
使用
"[M Where Born])"+