小巧玲珑&功放; MS访问 - 读的作品,写不 [英] Dapper & MS Access - Read works, Write doesn't

查看:192
本文介绍了小巧玲珑&功放; MS访问 - 读的作品,写不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们开始得到这个出路:我使用的是MS Access数据库卡住,我无法改变它

这工作得很好:

 使用(OleDbConnection的康恩= ConnectionHelper.GetConnection())
{
  conn.Open();
  VAR的结果= conn.Query<字符串>(
    从学生那里姓氏= @LastName选择名字,
    新{lastName的=史密斯}
  );
  conn.Close();
}
 

这工作得很好:

 使用(OleDbConnection的康恩= ConnectionHelper.GetConnection())
{
  OleDbCommand的CMD =新的OleDbCommand(
    更新学生树立姓= @firstName,市= @city其中,姓氏= @LastName
    康涅狄格州
  );
  cmd.Parameters.AddWithValue(名字,约翰);
  cmd.Parameters.AddWithValue(城市,SomeCity);
  cmd.Parameters.AddWithValue(姓氏,史密斯);

  conn.Open();
  VAR的结果= cmd.ExecuteNonQuery();
  conn.Close();
}
 

这不...它的执行没有错误,但它在DB和城市为约翰设置的名字为SomeCity:

 使用(OleDbConnection的康恩= ConnectionHelper.GetConnection())
{
  conn.Open();
  VAR的结果= conn.Query<字符串>(
    更新学生树立姓= @firstName,市= @city其中,姓氏= @LastName
    新{的firstName =约翰,全市=SomeCity,姓氏=史密斯}
  );
  conn.Close();
}
 

任何想法?

编辑下面

小巧精致的作品,如果我用DynamicParameters:

 使用(OleDbConnection的康恩= ConnectionHelper.GetConnection())
{
  DynamicParameters参数=新DynamicParameters();
  parameters.Add(名字,约翰);
  parameters.Add(城市,SomeCity);
  parameters.Add(姓氏,史密斯);

  conn.Open();
  VAR的结果= conn.Query<字符串>(
    更新学生树立姓= @firstName,市= @city其中,姓氏= @LastName
    参数
  );
  conn.Close();
}
 

解决方案

一些挖后,我能找原因:

下面是CreateParamInfoGenerator代表从短小精悍的SqlMapper:

 公共静态动作< IDbCommand的,对象> CreateParamInfoGenerator(身份识别)
    {

        // code以上在这里
        IEnumerable的<的PropertyInfo>道具= type.GetProperties()排序依据(P => p.Name)。
 

道具就是你的参数一致而被重新排序排序依据(P => p.Name),这会使城市的前期。

  {新的firstName =约翰,全市=SomeCity,姓氏=史密斯}
 

道具,然后被添加到IDbCommand的参数,其中的顺序是非常重要的。

如果我注释掉的OrderBy()子句,则一切正常。

我还测试DynamicParameters并有意重新排序的属性移动城市前期:

  VAR参数=新DynamicParameters();
        parameters.Add(城市,SomeCity);
        parameters.Add(名字,约翰);
        parameters.Add(姓氏,史密斯);

        VAR的结果= dbConnection.Query<字符串>(
          更新学生树立姓= @firstName,市= @city其中,姓氏= @LastName
          参数
        );
 

以上没有工作一样,所以属性的顺序就是这个道理!

我猜你现在可以修改SqlMapper的本地拷贝和删除排序依据(),并等待来自马克的官方定论...

希望这有助于。

Let's start by getting this out of the way: I'm stuck using an MS Access DB and I can't change it.

This works fine:

using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
  conn.Open();
  var results = conn.Query<string>(
    "select FirstName from Students where LastName = @lastName", 
    new { lastName= "Smith" }
  );
  conn.Close();
}

This works fine:

using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
  OleDbCommand cmd = new OleDbCommand(
    "update Students set FirstName = @firstName, City = @city where LastName = @lastName", 
    conn
  );
  cmd.Parameters.AddWithValue("firstName", "John");
  cmd.Parameters.AddWithValue("city", "SomeCity");
  cmd.Parameters.AddWithValue("lastName", "Smith");

  conn.Open();
  var result = cmd.ExecuteNonQuery();
  conn.Close();
}

This doesn't... it executes without error but it sets the FirstName as "SomeCity" in the DB and the City as "John":

using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
  conn.Open();
  var results = conn.Query<string>(
    "update Students set FirstName = @firstName, City = @city where LastName = @lastName", 
    new { firstName = "John", city = "SomeCity", lastName = "Smith" }
  );
  conn.Close();
}

Any ideas?

EDIT BELOW

Dapper works if I use DynamicParameters:

using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
  DynamicParameters parameters = new DynamicParameters();
  parameters.Add("firstName", "John");
  parameters.Add("city", "SomeCity");
  parameters.Add("lastName", "Smith");

  conn.Open();
  var result = conn.Query<string>(
    "update Students set FirstName = @firstName, City = @city where LastName = @lastName",
    parameters
  );
  conn.Close();
}

解决方案

After some digging, I was able to find a cause:

Below is CreateParamInfoGenerator delegate from dapper's SqlMapper:

    public static Action<IDbCommand, object> CreateParamInfoGenerator(Identity identity)
    {

        // code above here
        IEnumerable<PropertyInfo> props = type.GetProperties().OrderBy(p => p.Name); 

The props is your unanimous param which gets re-ordered by OrderBy(p => p.Name), which moves city upfront.

new { firstName = "John", city = "SomeCity", lastName = "Smith" }

Props is then being added to the IDbCommand Parameters where the order is important.

If I comment out OrderBy() clause, then everything works.

I also tested DynamicParameters and intentionally re-ordered the attributes to move city upfront:

        var parameters = new DynamicParameters();
        parameters.Add("city", "SomeCity");
        parameters.Add("firstName", "John");
        parameters.Add("lastName", "Smith");

        var result = dbConnection.Query<string>(
          "update Students set FirstName = @firstName, City = @city where LastName = @lastName",
          parameters
        );

The above did not work as well, so the order of attributes is the reason!

I guess you can modify your local copy of SqlMapper for now and remove OrderBy() and wait for an official verdict from Marc...

Hope this helps.

这篇关于小巧玲珑&功放; MS访问 - 读的作品,写不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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