数据库
首页 > 数据库> > c#-与Ms Access数据库的Linq数据映射:“在SQL语句末尾缺少分号(;).”

c#-与Ms Access数据库的Linq数据映射:“在SQL语句末尾缺少分号(;).”

作者:互联网

我一直在使用带有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),但是我希望可以对其进行自动分配.

如何避免出现此异常?

解决方法:

我看过这个:).您似乎对Linq2SQL缺乏连接类型强制;)感到不满. Access不支持Linq2SQL,尽管大多数读取查询和某些插入查询由于Access SQL和TSQL的相似性而起作用,但其他事情根本不可行,而其中之一就是带有自动标识列的插入.
原因是Linq会在同一插入命令中生成两个查询(一个用于插入记录,第二个用于检索新生成的标识),而这与Access不太匹配.可能的解决方法是,但很丑陋,您需要将DataContext子类化,然后创建所有有问题的Insert *方法(在您的情况下为InsertTest_Table),然后使用这些方法在同一事务内发出两个相应的命令

void InsertTest_Table(Test_Table t)
{
    IDbCommand cmd;
    cmd = Connection.CreateCommand();
    cmd.Transaction = this.Transaction;
    cmd.CommandText = "INSERT INTO [Test_Table] ([Field2]) VALUES (@p0)";
    cmd.Parameters.Add(new OleDbParameter("p0", t.field2));
    cmd.ExecuteNonQuery();

    cmd = Connection.CreateCommand();
    cmd.Transaction = this.Transaction;
    cmd.CommandText = "SELECT @@IDENTITY";
    t.field1 = Convert.ToInt32(cmd.ExecuteScalar());
}

我的建议是,如果可以的话,请转储Access并切换到SQLExpress(甚至.SDF更好)

标签:linq-to-objects,data-mapping,linq,c,ms-access
来源: https://codeday.me/bug/20191102/1995218.html