直接来代码吧,这里只对一类数据进行存储实现。
//数据实体类
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
namespace DAL.Models
{
[DataContract]
public class MD_StationMeteoData
{
[Key]
[DataMember]
public int Id { get; set; }
[DataMember]
public System.DateTime ModelDate { get; set; }
[DataMember]
public int LevelHeight { get; set; }
[DataMember]
public int StationID { get; set; }
[DataMember]
public System.DateTime TimePoint { get; set; }
[DataMember]
public string PollutantCode { get; set; }
[DataMember]
public double Value { get; set; }
[DataMember]
public string Mark { get; set; }
}
}
//业务逻辑类
using DAL.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace DAL.BLL
{
public class MD_StationMeteoDataBLL
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//static MD_StationMeteoDataBLL _defaule = new MD_StationMeteoDataBLL();
//public static MD_StationMeteoDataBLL Default
//{
// get { return _defaule; }
//}
public MD_StationMeteoDataBLL()
{
}
private void SqlBulkCopyByDatatable(string connectionString, string TableName, DataTable dt)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction))
{
try
{
sqlbulkcopy.DestinationTableName = TableName;
for (int i = 0; i < dt.Columns.Count; i++)
{
sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
}
sqlbulkcopy.WriteToServer(dt);
}
catch (System.Exception ex)
{
throw ex;
}
}
}
}
private DataTable CreateDataTable(List
{
var valuesList = values;
DataTable table = new DataTable();
table.Columns.AddRange(new DataColumn[] {
new DataColumn("ModelDate", typeof(DateTime)),
new DataColumn("LevelHeight",typeof(int)),
new DataColumn("StationID",typeof(int)),
new DataColumn("TimePoint", typeof(DateTime)),
new DataColumn("PollutantCode", typeof(string)),
new DataColumn("Value", typeof(double)),
new DataColumn("Mark", typeof(string)),
});
foreach (var item in values)
{
DataRow row = table.NewRow();
int num = 0;
DateTime timePoint = valuesList[0].TimePoint;
row[num++] = item.ModelDate;
row[num++] = item.LevelHeight;
row[num++] = item.StationID;
row[num++] = item.TimePoint;
row[num++] = item.PollutantCode;
row[num++] = item.Value;
row[num++] = "";
table.Rows.Add(row);
}
return table;
}
public bool AddSqlBulkCopyByData(List
{
try
{
DataTable dt = CreateDataTable(modellist);
string constr = db.Database.Connection.ConnectionString;
SqlBulkCopyByDatatable(constr, "MD_StationMeteoData", dt);
return true;
}
catch (Exception ex)
{
log.Error(ex);
return false;
}
}
}
}
//数据存储处理类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using DAL.Models;
using DAL.BLL;
namespace MeteoDataProcessDemo
{
public class SaveProcess
{
///
///
///
private readonly AutoResetEvent _event;
private bool _flag;
private readonly List
///
/// 工作线程
///
private Thread _worker;
///
/// 静态字段
///
public static SaveProcess Default = null;
///
/// 静态构造
///
static SaveProcess()
{
Default = new SaveProcess();
}
private SaveProcess()
{
_flag = true;
this._MeteoDataList = new List
this._event = new AutoResetEvent(true);
}
///
/// 向列表添加数据项
///
///
public void Append(List
{
if (values != null)
{
lock (this._MeteoDataList)
{
this._MeteoDataList.AddRange(values);
this._event.Set(); //通知工作线程保存
}
}
}
///
///
///
public void Stop()
{
if ((this._worker != null) && this._worker.IsAlive)
{
this._flag = false;
this._event.Set();
this._worker.Join();
}
}
///
/// 线程启动事件
///
public void Start()
{
_worker = new Thread(Work);
_worker.Start();
}
///
/// 工作线程
///
public void Work()
{
//this._flag = true;
while (this._flag)
{
int num = -1;
try
{
if (this._MeteoDataList.Count == 0)
{
this._event.Reset();
this._event.WaitOne();
}
Thread.Sleep(1000);
num = this.Save();
}
catch (Exception ex)
{
}
finally
{
}
}
}
///
/// 快速复制List
///
///
private int Save()
{
if (this._MeteoDataList.Count > 0)
{
MD_StationMeteoData[] values = null;
lock (this._MeteoDataList) //
{
values = this._MeteoDataList.ToArray(); //复制list
this._MeteoDataList.Clear(); //清空列表
}
if (values != null)
{
return this.Save(values);
}
return -1;
}
return 0;
}
///
/// 保存数据-SqlBulkCopyByData
///
///
///
private int Save(MD_StationMeteoData[] values)
{
int num = 0;
try
{
MD_StationMeteoDataBLL meteodatabll = new MD_StationMeteoDataBLL();
meteodatabll.AddSqlBulkCopyByData(values.ToList
}
finally
{
}
return num;
}
}
}