Dapper扩展女士访问System.Data.OleDb.OleDbException [英] Dapper Extension Ms Access System.Data.OleDb.OleDbException

查看:73
本文介绍了Dapper扩展女士访问System.Data.OleDb.OleDbException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Dapper.小巧玲珑的作品.下一步,当我尝试与Dapper Extension集成时.它生成一个名为System.Data.OleDb.OleDbException "Additional information: Characters found after end of SQL statement."的异常,为什么呢? Dapper Extension不支持Access女士(由于结尾字符)或我的代码有问题,或者我丢失了某些内容.我的代码在下面

I just started to use Dapper. Dapper works fine. As a next step when I tried to integrate with Dapper Extension. It generates an exception called System.Data.OleDb.OleDbException "Additional information: Characters found after end of SQL statement." Why is that? Dapper Extension doesn't support Ms Access (because of the end character) or problem with my code or I am missing something. My code is below

using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myAccessFile.accdb;"))
{
    conn.Open();
    conn.Insert<Person>(new Person { Name = "John Stan", Age = 20 });
}

推荐答案

根据

某些数据库引擎(例如Microsoft Access Jet数据库引擎)不支持输出参数,并且无法在单个批处理中处理多个语句.

Some database engines, such as the Microsoft Access Jet database engine, do not support output parameters and cannot process multiple statements in a single batch.

所以问题在于Insert方法正在生成诸如

So the problem is that the Insert method is generating a statement such as

INSERT INTO [Person] ([Person].[PersonName]) VALUES (@PersonName);
SELECT CAST(SCOPE_IDENTITY()  AS BIGINT) AS [Id]

Access不能处理它.

and Access can't deal with it.

通读一遍,似乎有很多关于在处理Access时如何进行插入并获取新记录键的建议(该MSDN文章建议了第二条SELECT语句),但是如果您正在使用DapperExtensions库,因为这正是为您生成查询的原因.

Reading around, it seems like that are various suggestions as to how to do insert-and-get-new-record-key when dealing with Access (that MSDN article suggests a second SELECT statement) but that doesn't help if you're using the DapperExtensions library, since that is what generates the query for you.

因此,基本上,我认为DapperExtensions无法与Access配合使用是正确的.

So, basically, I think that you are correct in thinking that DapperExtensions won't work with Access.

另一方面,我经历了一场噩梦,试图找出正在生成的查询.有各种各样的文章谈论注册表黑客将"JETSHOWPLAN"值设置为"ON",但是我无法使它们中的任何一个起作用.最后,我创建了包装的数据库连接和命令类,以便可以在出局时捕获查询.万一这对将来的任何人有用,我将其包括在下面.

On a side note, I had a nightmare trying to find out what queries were being generated. There are various articles that talk about a registry hack to set a "JETSHOWPLAN" value to "ON" but I couldn't make any of them work. In the end, I created wrapped database connection and command classes so that the queries could be captured on the way out. In case this is of any use to anyone in the future, I'm including it below..

数据库连接初始化代码需要稍作更改-例如.

The database connection initialisation code needs to change slightly - eg.

var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database2.mdb;";
using (var conn = new WrappedDbConnection(new OleDbConnection(connectionString)))
{
    conn.Insert<Person>(new Person { PersonName = "Dan" });
}

和以下两个类需要定义-

and the following two classes need to be defined -

public class WrappedDbConnection : IDbConnection
{
    private readonly IDbConnection _conn;
    public WrappedDbConnection(IDbConnection connection)
    {
        if (connection == null)
            throw new ArgumentNullException(nameof(connection));

        _conn = connection;
    }

    public string ConnectionString
    {
        get { return _conn.ConnectionString; }
        set { _conn.ConnectionString = value; }
    }

    public int ConnectionTimeout
    {
        get { return _conn.ConnectionTimeout; }
    }

    public string Database
    {
        get { return _conn.Database; }
    }

    public ConnectionState State
    {
        get { return _conn.State; }
    }

    public IDbTransaction BeginTransaction()
    {
        return _conn.BeginTransaction();
    }

    public IDbTransaction BeginTransaction(IsolationLevel il)
    {
        return _conn.BeginTransaction(il);
    }

    public void ChangeDatabase(string databaseName)
    {
        _conn.ChangeDatabase(databaseName);
    }

    public void Close()
    {
        _conn.Close();
    }

    public IDbCommand CreateCommand()
    {
        return new WrappedDbCommand(_conn.CreateCommand());
    }

    public void Dispose()
    {
        _conn.Dispose();
    }

    public void Open()
    {
        _conn.Open();
    }
}

public class WrappedDbCommand : IDbCommand
{
    private readonly IDbCommand _cmd;
    public WrappedDbCommand(IDbCommand command)
    {
        if (command == null)
            throw new ArgumentNullException(nameof(command));

        _cmd = command;
    }

    public string CommandText
    {
        get { return _cmd.CommandText; }
        set { _cmd.CommandText = value; }
    }

    public int CommandTimeout
    {
        get { return _cmd.CommandTimeout; }
        set { _cmd.CommandTimeout = value; }
    }

    public CommandType CommandType
    {
        get { return _cmd.CommandType; }
        set { _cmd.CommandType = value; }
    }

    public IDbConnection Connection
    {
        get { return _cmd.Connection; }
        set { _cmd.Connection = value; }
    }

    public IDataParameterCollection Parameters
    {
        get { return _cmd.Parameters; }
    }

    public IDbTransaction Transaction
    {
        get { return _cmd.Transaction; }
        set { _cmd.Transaction = value; }
    }

    public UpdateRowSource UpdatedRowSource
    {
        get { return _cmd.UpdatedRowSource; }
        set { _cmd.UpdatedRowSource = value; }
    }

    public void Cancel()
    {
        _cmd.Cancel();
    }

    public IDbDataParameter CreateParameter()
    {
        return _cmd.CreateParameter();
    }

    public void Dispose()
    {
        _cmd.Dispose();
    }

    public int ExecuteNonQuery()
    {
        Console.WriteLine($"[ExecuteNonQuery] {_cmd.CommandText}");
        return _cmd.ExecuteNonQuery();
    }

    public IDataReader ExecuteReader()
    {
        Console.WriteLine($"[ExecuteReader] {_cmd.CommandText}");
        return _cmd.ExecuteReader();
    }

    public IDataReader ExecuteReader(CommandBehavior behavior)
    {
        Console.WriteLine($"[ExecuteReader({behavior})] {_cmd.CommandText}");
        return _cmd.ExecuteReader();
    }

    public object ExecuteScalar()
    {
        Console.WriteLine($"[ExecuteScalar] {_cmd.CommandText}");
        return _cmd.ExecuteScalar();
    }

    public void Prepare()
    {
        _cmd.Prepare();
    }
}

现在,查询在发送到数据库之前已写入控制台.

Now, the queries are written to the console before being sent to the database.

这篇关于Dapper扩展女士访问System.Data.OleDb.OleDbException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆