简体   繁体   English

更新查询无济于事

[英]Update Query Does Nothing

I am trying to call the following query (parameters are in alphabetical order): 我正在尝试调用以下查询(参数按字母顺序排列):

public const string UpdateSample =
    @"UPDATE subReceivingQC
      SET Clerk=@Clerk, Comments=@Comments, CommentsProd=@CommentsProd, GRV=@GRV, 
          MassOff=@MassOff, PalletID=@PalletID, PalletSeq=@PalletSeq, PONo=@PONo, 
          QCDate=@QCDate, QtyInspected=@QtyInspected, SampleNo=@SampleNo, 
          StatusClerk=@StatusClerk, StatusSupervisor=@StatusSupervisor, Supervisor=@Supervisor
      WHERE GRV=@GRV AND PalletSeq=@PalletSeq AND SampleNo=@SampleNo;";

Using this method: 使用此方法:

internal int UpdateSample(Sample sample)
{
    using (var db = new OleDbConnection(ConnectionString))
    {
        var query = Constants.UpdateSample;
        return db.Execute(query, sample);
    }
}

The Execute() command is successful (in that no exceptions appear), but UpdateSample keeps returning 0 , and the database entry remains unchanged. Execute()命令成功Execute()因为没有出现异常),但是UpdateSample始终返回0 ,并且数据库条目保持不变。

I have used this exact patter for other db operations ( INSERT and SELECT ) without any issue. 我已经将此精确模式用于其他数据库操作( INSERTSELECT ),没有任何问题。

The only related issues I can find were resolved by sorting the parameters in the query (mine already sorted). 我可以找到的唯一相关问题是通过对查询中的参数进行排序(已经排序的)解决了。

Does anyone have any insight on what is going on here, or how I can go about debugging this? 是否有人对这里发生的事情有任何见解,或者我该如何进行调试?

Following on from Steve's comment, I have not included any code for adding parameters to the command, as I am under the impression that these are obtained automatically from the Model . 在Steve的评论之后,我没有包含任何用于向命令添加参数的代码,因为我的印象是,这些参数是从Model自动获得的。


Properties available in the Sample model: Sample模型中可用的属性:

public class Sample : IGriddable
{
    public string[] ColumnHeaders { get; } = new string[] { "SampleNo", "QCDate", "StatusClerk", "StatusSupervisor" };
    public string RowLinkPrefix { get { return $"/receiving/{Pallet.Grv.GRVNo}/{Pallet.PalletSeq}/"; } }
    public bool Selectable { get; } = true;

    public Pallet Pallet { get; set; }

    public string Clerk { get; set; }
    public string MassOff { get; set; }
    public string CommentsProd { get; set; }
    public string QtyInspected { get; set; }
    public string Supervisor { get; set; }
    public string Comments { get; set; } 

    public string SampleNo { get; set; }
    public string StatusClerk { get; set; }
    public string StatusSupervisor { get; set; }
    public string ProductSpec { get; set; }

    // For required db params
    public string GRV { get; set; }
    public string PalletID { get; set; }
    public string PalletSeq { get; set; }
    public string PONo { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime QCDate { get; set; }

    // Each defect status needs to be saved as a (DB)subQCItems item
    public List<QCItem> Defects { get; set; } = new List<QCItem>();
    public List<string> ImagePaths { get; set; } = new List<string>();
}

OleDb requires that you pass the parameters in the same exact order in which you present them in the command text. OleDb要求您以与在命令文本中显示它们的相同顺序传递参数。 If you don't do it, then wrong values could be used for the WHERE statement and, if you are unlucky you could end to change a wrong record, or simply you don't update anything. 如果您不这样做,则可能在WHERE语句中使用错误的值,并且,如果您不走运,则可能会更改错误的记录,或者根本就不更新任何内容。

Unfortunately Dapper doesn't take in consideration this in its algorithms used to prepare the command. 不幸的是,Dapper在用于准备命令的算法中没有考虑到这一点。 Sql Server and other providers with named parameters and no constraints about order don't have this problem. 带有命名参数且对顺序没有约束的Sql Server和其他提供程序没有此问题。 Instead adding the code to create and order correctly the parameters based on your query text could be very costly for performances. 取而代之的是添加代码以根据查询文本正确创建和排序参数,这对于性能而言可能是非常昂贵的。
I believe they thought this didn't worth the effort and that you can simply resolve the problem defining yourself the parameters. 我相信他们认为这样做不值得,您可以简单地解决定义参数的问题。

So you are on your own on this point and you should use the version of Execute that takes a DynamicParameters parameter 因此,您现在就该靠自己了,应该使用带有DynamicParameters参数的Execute版本。

using (var db = new OleDbConnection(ConnectionString))
{
    var query = Constants.UpdateSample;

    DynamicParameters pars = new DynamicParameters();
    pars.Add("@Clerk", sample.Clerk, DbType.String);
    // ... and so on for all parameters following the order of the placeholders

    // but end with ....
    pars.Add("@GRV", sample.GRV, DbType.String);
    pars.Add("@PalletSeq", sample.PalletSeq, DbType.String);
    pars.Add("@SampleNo", sample.SampleNo, DbType.String);

    return db.Execute(query, pars);
}

which database are you using ? 您正在使用哪个数据库? if SQL Server, use SQL Profiler to find out the exact query being executed. 如果是SQL Server,则使用SQL事件探查器找出正在执行的确切查询。

if you are not using SQL server, try to execute the query yourself in the database without oledb. 如果不使用SQL Server,请尝试在没有oledb的数据库中自己执行查询。 It probably is a parameter issue in which the where-clause does not lead to any results. 这可能是参数问题,其中where子句不会导致任何结果。 Which value do @GRV , @PalletSeq and @SampleNo have ? @GRV,@PalletSeq和@SampleNo具有哪个值? if no records match that combination, obviously nothing happens 如果没有记录匹配该组合,则显然不会发生任何事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM