如何将 sqldatareader 转换为 dto 列表?

发布于 2024-12-18 03:27:40 字数 185 浏览 3 评论 0原文

我刚刚开始将所有 ado.net 代码从 asp.net 页面移动到存储库,并为每个表创建 dto(手动),但现在我不知道将 sqldatareader 转换为我的列表的有效方法是什么dto 对象?

例如,我的 dto 是 Customer。

我正在使用 Webforms,并且没有使用 ORM。我想慢慢开始,然后努力向上。

I just started moving all my ado.net code from the asp.net pages to repo's and created dto's for each table (manually), but now I don't know what is a good efficient way to convert a sqldatareader to a list of my dto objects?

For example sake, my dto is Customer.

I am using webforms and I am NOT using an ORM. I would like to start slow and work my way up there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜中书 2024-12-25 03:27:40

通常该模式看起来像这样:

List<Customer> list = new List<Customer>();

using(SqlDataReader rdr = GetReaderFromSomewhere()) {
  while(rdr.Read()) {
     Customer cust = new Customer();
     cust.Id = (int)rdr["Id"];
     list.Add(cust)
  }
}

Usually the pattern looks something like:

List<Customer> list = new List<Customer>();

using(SqlDataReader rdr = GetReaderFromSomewhere()) {
  while(rdr.Read()) {
     Customer cust = new Customer();
     cust.Id = (int)rdr["Id"];
     list.Add(cust)
  }
}
∞觅青森が 2024-12-25 03:27:40

下面是一个关于如何使用数据读取器检索数据的简短示例:

var customers = new List<Customer>();
string sql = "SELECT * FROM customers";
using (var cnn = new SqlConnection("Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;")) {
    cnn.Open();
    using (var cmd = new SqlCommand(sql, cnn)) {
        using (SqlDataReader reader = cmd.ExecuteReader()) {
            // Get ordinals (column indexes) from the customers table
            int custIdOrdinal = reader.GetOrdinal("CustomerID");
            int nameOrdinal = reader.GetOrdinal("Name");
            int imageOrdinal = reader.GetOrdinal("Image");
            while (reader.Read()) {
                var customer = new Customer();
                customer.CustomerID = reader.GetInt32(custIdOrdinal);
                customer.Name = reader.IsDBNull(nameOrdinal) ? null : reader.GetString(nameOrdinal);
                if (!reader.IsDBNull(imageOrdinal)) {
                    var bytes = reader.GetSqlBytes(imageOrdinal);
                    customer.Image = bytes.Buffer;
                }
                customers.Add(customer);
            }
        }
    }
}

如果表列可为空,则在检索数据之前检查 reader.IsDBNull

Here a short example on how you can retrieve your data using a data reader:

var customers = new List<Customer>();
string sql = "SELECT * FROM customers";
using (var cnn = new SqlConnection("Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;")) {
    cnn.Open();
    using (var cmd = new SqlCommand(sql, cnn)) {
        using (SqlDataReader reader = cmd.ExecuteReader()) {
            // Get ordinals (column indexes) from the customers table
            int custIdOrdinal = reader.GetOrdinal("CustomerID");
            int nameOrdinal = reader.GetOrdinal("Name");
            int imageOrdinal = reader.GetOrdinal("Image");
            while (reader.Read()) {
                var customer = new Customer();
                customer.CustomerID = reader.GetInt32(custIdOrdinal);
                customer.Name = reader.IsDBNull(nameOrdinal) ? null : reader.GetString(nameOrdinal);
                if (!reader.IsDBNull(imageOrdinal)) {
                    var bytes = reader.GetSqlBytes(imageOrdinal);
                    customer.Image = bytes.Buffer;
                }
                customers.Add(customer);
            }
        }
    }
}

If a table column is nullable then check reader.IsDBNull before retrieving the data.

ゞ花落谁相伴 2024-12-25 03:27:40

你确实应该看看 Massive - 这个 ADO.NET 上的简单包装器

var table = new Customer();
//grab all
var customers = table.All();
//just grab from customer 4. This uses named parameters
var customerFour = table.All(columns: "CustomerName as Name", where: "WHERE customerID=@0",args: 4);

You defenitly should look at Massive - this simple wrapper over ADO.NET

var table = new Customer();
//grab all
var customers = table.All();
//just grab from customer 4. This uses named parameters
var customerFour = table.All(columns: "CustomerName as Name", where: "WHERE customerID=@0",args: 4);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文