使用数据行列表来存储数据

发布于 2024-09-15 18:55:50 字数 2038 浏览 8 评论 0 原文

我在两个类之间有主从关系。大师班将包含许多细节的列表。目前我正在

public class Master: cloneable<T>
{
     //other properties here...
    private List<detailClass> details
    public List<detailClass> Details
    {
        get { return details; }
    }
}

大师班中使用。在保存此类时,我需要在将详细信息列表传递到 sp 之前使用数据表。 (因为我们在 sql2008 中使用表值参数)。使用 tvp 的原因是 1 个 master 可以包含多达 10k 个详细信息,并且 tvp 是一种非常有效的方法,可以非常快速地将所有信息转储到数据库中。

Qs:当我将列表转换为数据表以进行数据库插入时,相同数据的内存使用量增加了一倍。除了直接使用数据表之外,还有更好的方法将详细信息保存在 master 中吗?

问:我的下一个选择是尝试使用列表详细信息,并执行 datatable.ImportRow(row)。但我不知道如何在不定义任何列的情况下将数据添加到行中。我也不知道任何外部对象如何访问此类列表中的各个详细信息字段。


按照casperOne的回答我使用了IEnumerable 方法,能够通过流式传输插入数据,而无需在内存中创建额外的数据表。为了对任何其他寻找类似解决方案的人有所帮助,我发布了下面的代码。

public class DetailCollection: List<Detail>, IEnumerable<SqlDataRecord>
{
    IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
    {
        // mapping the properties of the Detail object to the 
        // user defined table type in sql
        SqlMetaData[] metaDataArray = new SqlMetaData[4];

        //-1 indicates varchar(max) sql data type
        metaDataArray[0] = new SqlMetaData("Col1", SqlDbType.VarChar, -1); 
        metaDataArray[1] = new SqlMetaData("Col2", SqlDbType.TinyInt);
        metaDataArray[2] = new SqlMetaData("Col3", SqlDbType.VarChar,100);
        metaDataArray[3] = new SqlMetaData("Col4", SqlDbType.Int);

        SqlDataRecord sdr = new SqlDataRecord(metaDataArray);


        foreach (Detail detailRecord in this)
        {
                    sdr.SetValue(0, detailRecord.Property1);
                    sdr.SetValue(1, Convert.ToByte(detailRecord.Property2));
                    sdr.SetValue(2, detailRecord.Property3);
                    sdr.SetValue(3, detailRecord.Property4);
                    yield return sdr;
        }
    }
}

I have a master-detail relationship between 2 classes. The master class will contain a list of many details. Currently I was using

public class Master: cloneable<T>
{
     //other properties here...
    private List<detailClass> details
    public List<detailClass> Details
    {
        get { return details; }
    }
}

inside the master class. While saving this class, I need to use a datatable for the details list before passing it into a sp. (since we are using table value params in sql2008). reason for using tvp, is that 1 master can contain upwards of 10k details, and tvp is a very efficient way of dumping all that info into the db very fast.

Qs:When I convert the list to a datatable for db insertion, there is a double memory usage for the same data. Is there a better way of saving the details in master, other than directly using a datatable?

Qs:I next option was to try using a List details, and do datatable.ImportRow(row). But I do not know, how I can add data into a row, without having any columns defined. I also dont know how any external object can acess the individual detail fields in such a list.


Following casperOne's answer I used the IEnumerable<SqldataRecord> method and was able to insert data by streaming and without having to create an additional datatable in memory.To be helpful for any one else looking for similar solution, I am posting the code below.

public class DetailCollection: List<Detail>, IEnumerable<SqlDataRecord>
{
    IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
    {
        // mapping the properties of the Detail object to the 
        // user defined table type in sql
        SqlMetaData[] metaDataArray = new SqlMetaData[4];

        //-1 indicates varchar(max) sql data type
        metaDataArray[0] = new SqlMetaData("Col1", SqlDbType.VarChar, -1); 
        metaDataArray[1] = new SqlMetaData("Col2", SqlDbType.TinyInt);
        metaDataArray[2] = new SqlMetaData("Col3", SqlDbType.VarChar,100);
        metaDataArray[3] = new SqlMetaData("Col4", SqlDbType.Int);

        SqlDataRecord sdr = new SqlDataRecord(metaDataArray);


        foreach (Detail detailRecord in this)
        {
                    sdr.SetValue(0, detailRecord.Property1);
                    sdr.SetValue(1, Convert.ToByte(detailRecord.Property2));
                    sdr.SetValue(2, detailRecord.Property3);
                    sdr.SetValue(3, detailRecord.Property4);
                    yield return sdr;
        }
    }
}

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

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

发布评论

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

评论(1

山川志 2024-09-22 18:55:50

您可以使用以下两种方法之一将结果流式传输到表值参数方法:

  • IEnumerable -您可以在 IEnumerable 实现(或 LINQ,更简单)中使用 yield return 来创建流解决方案。
  • DbDataReader实现 - 您可以创建一个实现,它将引用您的列表并在读者枚举时提供适当的转换。

使用其中任何一个,您基本上都可以创建实现,从列表中获取项目,然后将结果转换为表值参数进行流式传输。这样,您不必在应用程序中重新实现第二个列表,只需根据需要对第一个列表执行转换即可。

有关详细信息,请参阅 MSDN 中标题为“SQL Server 中的表值参数的部分2008”,特别是“配置 SqlParameter 示例”和“使用 DataReader 流式传输行”部分。

You can stream results to a table-valued parameter using one of two methods:

  • IEnumerable<SqlDataRecord> - You can use yield return in an IEnumerable<SqlDataRecord> implementation (or LINQ, which is even easier) to create a streaming solution.
  • DbDataReader implementation - You can create an implementation which will take a reference to your list and provide the appropriate transformations as the reader is enumerated through.

With either of these, you can basically create implementations which will take the item from the list and then transform the result to be streamed as the table-valued-parameter. This way, you don't have to rematerialize a second list in your application, you can just perform the transformation on the first as-needed.

For more information, see the section of MSDN titled "Table-Valued Parameters in SQL Server 2008", specifically, the "Configuring a SqlParameter Example" and the "Streaming Rows with a DataReader" sections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文