Bulk copy of SQL data from table on disk to an in-memory table

c# database sql sqlbulkcopy sql-server

Question

I want to periodically copy large records from a SQL server into a in-memory table. I need to put these fields I read into my own objects, not into another SQL table or flat file storage. Is a SQLBulkCopy the way forward to do this?

Popular Answer

create a class..

public class MemoryObject
{
    public Int64 Id {get; set;}  
    public string OtherProperty {get; set;}     
}

then, use a SQLDataReader...

public List<MemoryObject> GetMemoryObjects()
{
    using (SqlConnection connection = new SqlConnection("..."))
    {
        string sql = "query";

        SqlCommand command = new SqlCommand(sql.ToString(), connection);
        command.CommandTimeout = 3600;

        connection.Open();
        var memoryObjects = new List<MemoryObject>();

        using (var rdr = command.ExecuteReader())
            {
                    while (rdr.Read())
                    {
                        var memoryObject = new MemoryObject {
                            Id = rdr.GetInt64(0),
                            OtherProperty = rdr.GetString(1)
                        };
                        memoryObjects.Add(memoryObject);
                    }                   
            }
    }

    return memoryObjects;
}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why