文章分类

相关文章

C#将DataTable写入MongoDB

分类:C#     发布:2018-06-06     来源:本站     浏览:730 次
思路:遍历DataTable的Rows,将Row转换为BsonDocument对象,然后批量写入数据库。
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("rc114");
var collection = database.GetCollection<BsonDocument>("IISLog");
var dt = data.DataSource as DataTable;
var count = 0;
var batch = new List<BsonDocument>();
if (dt != null)
{
    foreach (DataRow dr in dt.Rows)
    {
        count++;
        var dictionary = dr.Table.Columns.Cast<DataColumn>().ToDictionary(col => col.ColumnName, col => dr[col.ColumnName]);
        batch.Add(new BsonDocument(dictionary));

        //分批次写入,防止内存溢出
        if (batch.Count == 5000)
        {
            UpdateUI(() => { lblStatus.Text = "已导入 " + count; });
            collection.InsertManyAsync(batch.AsEnumerable());
            batch.Clear();
            ClearMemory();
        }
    }
    if (batch.Count > 0)
    {
        UpdateUI(() => { lblStatus.Text = "已导入 " + count; });
        collection.InsertManyAsync(batch.AsEnumerable());
        batch.Clear();
        ClearMemory();
    }
}
清理程序占用内存方法:
        [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);

        public static void ClearMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
            }
        }
参考:https://stackoverflow.com/questions/37940065/insert-datatable-in-mongo-db-using-c-sharp

上一篇:Windows bat批处理删除指定N天前的文件

下一篇:清山代码生成工具(适用于.NET平台开发)