在SqlDataReader中修改数据的最佳方法是什么? [英] Best way to modify data in SqlDataReader?

查看:134
本文介绍了在SqlDataReader中修改数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在C#中修改(My)SqlDataReader对象的最佳方法是什么?

原因是我想将所有空字符串更改为null - 在此读取器对象中稍后使用SqlBulkCopy的WriteToServer(读取器)复制该读取器。

I'm wondering what's the best way to modify an (My)SqlDataReader object in C#?
The reason is that I want to change all empty strings into null - within this reader object to copy that reader later with SqlBulkCopy's WriteToServer(reader).

所以我的第一个想法是遍历每个字段并替换所有空字符串为null:

So my first thought was to go through each field and replace all empty strings with null:

while (reader.Read())
{
  foreach (var field in reader)
  {
    //field.Replace('', null);
  }
}

SQL select语句或者更确切地说选择的列是完全不同的 - 列是动态选择。

所以我认为我必须操纵读者对象,但我也认为有更好的方法来做到这一点,不是吗?

The SQL select statement or rather the columns that are selected is quite different - the columns are dynamically choosen.
So I think I have to manipulate the reader object, but I also think there is a much better way to do this, isn't it?

提前致谢!

Flo

Thanks in advance!
Flo

推荐答案

我怀疑你可以改变读者的内容并把它放在读者中,因为毕竟它是一个读者和一切都是只读的,不能写回给我所知的读者。

I doubt that you can change the content of the reader and hold it in the reader, because after all, it's a reader and everything is read only and not writable back to the reader that I know about.

你能做的最好的就是使用自定义类型,如DTO, 从datareader提供的数据填充DTO,然后操纵DTO属性中的数据。然后将DTO加载到List< T>中。并使用DTO(s)的集合。

The best you can do is use a custom type like a DTO,  populate the DTO from data presented by the datareader and then manipulate the data in the DTO property. Then you load the DTO into a List<T> and use the collection of DTO(s).

https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

while (reader.Read()) { var dto = new DtoCustomer(); dto.FirstName = rdr["FirstName"];

dto.FirstName = dto.FirstName +" Help" ;;

//填充其余属性。
//如果需要,则操纵DTO中的数据

dtos.Add(dto);
}

dto.FirstName = dto.FirstName + "Help"; // populate the rest of the properties. // manipulate the data in the DTO if need be dtos.Add(dto); }

虽然示例没有使用datareader并且它正在使用Entity Framework,但使用DTO的概念是相同的。

Although the example is not using a datareader and it is using Entity Framework, the concepts of using the DTO are the same.

public List<DtoProject> GetProjectsByUserId(string userid)
        {
            var dtos = new List<DtoProject>();

            using (var context = new ProjectManagementContext(_options))
            {
                
                dtos = (from a in context.Projects.Where(a => a.UserId.Contains(userid))
                    select new DtoProject
                    {
                        ProjectId = a.ProjectId,
                        ClientName = a.ClientName,
                        ProjectName = a.ProjectName,
                        Technology = a.Technology,
                        ProjectType = a.ProjectType,
                        UserId = a.UserId,
                        StartDate = a.StartDate,
                        EndDate = a.EndDate,
                        Cost = a.Cost
                    }).ToList();
            }

            return dtos;
        }


这篇关于在SqlDataReader中修改数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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