public void AddLetters(IList<int> customerIds, string title, string content, LetterEnum.LetterType type)
{
// 插入内容,获取内容ID
var msgContentId = this.AddLetterContent(title, content, 2); // 站内信接收者表字段初始化
var letterDataTable = this.InitDataTable();
foreach (var customerId in customerIds)
{
var letterDataRow = letterDataTable.NewRow(); letterDataRow["CustomerId"] = customerId; letterDataRow["MsgContentId"] = msgContentId; letterDataTable.Rows.Add(letterDataRow);
} using (var conn = new SqlConnection(_sqlMapper.DataSource.ConnectionString))
{
conn.Open(); using (var sqlBulkCopy = new SqlBulkCopy(conn))
{
sqlBulkCopy.DestinationTableName = "Letter";
sqlBulkCopy.BatchSize = letterDataTable.Rows.Count; if (letterDataTable.Rows.Count > 0)
{
sqlBulkCopy.WriteToServer(letterDataTable);
}
}
}
} /// <summary>
/// 初始化Letter表结构
/// </summary>
private DataTable InitDataTable()
{
var dataTable = new DataTable(); using (var conn = new SqlConnection(_sqlMapper.DataSource.ConnectionString))
{
var queryString = string.Format(@"select top 0 * from {0}", "Letter");
var adapter = new SqlDataAdapter(queryString, conn);
adapter.Fill(dataTable); } return dataTable;
}