使用DataContractSerializer序列化DataSet时保留DataRowState [英] Preserving DataRowState when serializing DataSet using DataContractSerializer

查看:53
本文介绍了使用DataContractSerializer序列化DataSet时保留DataRowState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于各种原因,我不得不将类型化的数据集发送到WCF服务端点.除在反序列化时,将每个DataTable中的每一行的RowState设置为"Added"(不管它们在客户端上是什么)外,其他方法都可以正常工作.如果我将序列化的流写到文件中,则会看到RowState不是序列化数据的一部分.如何添加它,以便可以跨服务边界保留RowState?我认为这并不重要,但是客户端进程正在运行.net 3.5,而服务进程正在运行.net 4.0

For various reasons I am having to send a typed dataset to a WCF service endpoint. This works fine except that upon Deserializing, the RowState of each row in each DataTable is set to 'Added', regardless of what they were on the client. If I write the serialized stream out to a file, I see that the RowState is not part of the Serialized data. How can I add this so that I can preserve the RowState across service boundaries? Not that I think it matters, but the client process is running .net 3.5 while the service process is running .net 4.0

推荐答案

以下是 RowState 属性的代码:

public DataRowState RowState
{
    get
    {
        if (this.oldRecord == this.newRecord)
        {
            if (this.oldRecord == -1)
            {
                return DataRowState.Detached;
            }
            if (0 < this._columns.ColumnsImplementingIChangeTrackingCount)
            {
                foreach (DataColumn column in this._columns.ColumnsImplementingIChangeTracking)
                {
                    object obj2 = this[column];
                    if ((DBNull.Value != obj2) && ((IChangeTracking)obj2).IsChanged)
                    {
                        return DataRowState.Modified;
                    }
                }
            }
            return DataRowState.Unchanged;
        }
        if (this.oldRecord == -1)
        {
            return DataRowState.Added;
        }
        if (this.newRecord == -1)
        {
            return DataRowState.Deleted;
        }
        return DataRowState.Modified;
    }
}

如您所见,

它的值可能无能为力,因为它是计算得出的,而不仅仅是存储的.最简单的解决方案可能是将另一列添加到包含行状态的数据集.

as you can see, there may be nothing you can do about its value, because it is calculated rather than just stored. The easiest solution may be to just add another column to the DataSet which contains the state of the row.

(为什么它总是计算得出值 Added ??最有可能的原因是,当序列化的数据集在服务器上重新水化后,会创建新的行并将其添加到数据集中-因此该值是如果您按照上述建议在数据集中添加另一列,则需要更改服务器代码才能检查和处理它-如果您要进行此类更改,则可能是值得整个事情并重新编码该服务以使用适当的可序列化DTO?).

(Why does it always calculate out to the value Added? Most likely because when your serialized dataset is rehydrated back on the server, new rows are created and added to the dataset - so the value is quite literally true. If you follow the above suggestion to add another column to the dataset, that will require a change to the server code to be able to examine and process it - if you are going to make that sort of change then maybe it is worth doing the whole thing and recode that service to use proper serializable DTOs instead?).

这篇关于使用DataContractSerializer序列化DataSet时保留DataRowState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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