我一直在使用带有 Ms Access 数据库的 Linq 数据映射。我像往常一样创建一个OleDbConnection
并将其传递给DataContext
.
到目前为止,这一直很好,可以根据复杂查询从表中检索数据,甚至关系也可以自动填充 1-N 关系中的子实体列表。
但是,当我尝试使用以下代码插入数据时:
[Table(Name = "test_table")]
public class test_item {
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int field1;
[Column]
public int field2;
}
public void Test() {
Table<test_item> tbl = this.GetTable<test_item>();
test_item x = new test_item();
x.field2 = 1222;
tbl.InsertOnSubmit(x);
this.SubmitChanges();
}
我收到以下错误:
"Missing semicolon (;) at end of SQL statement."
at 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.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at MyClass.Test() in C:\...\MyClass.cs:line 123
如果我删除该IsDbGenerated
标志,它不会崩溃,但在这种情况下,我被迫指定主键 ( x.field1 = 55
),但我希望它自动分配。
如何避免出现此异常?