#region数据库对应表
/// <summary>
/// 数据库对应表
/// </summary>
/// <returns></returns>
private static DataTable GetTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("User_ID", typeof(string));
dt.Columns.Add("User_Name", typeof(string));
dt.Columns.Add("User_Birth", typeof(DateTime));
dt.Columns.Add("User_Sex", typeof(int));
return dt;
}
#endregion
/// <summary>
/// 批量插入数据
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public void AddModels()
{ List<UserEntity> users = (List< UserEntity>)HttpContext.Current.Cache["UserList "];
DataTable dt_users = GetTable ();
foreach (UserEntity model in users)
{
DataRow dr_user = dt_ users.NewRow();
dr_ user ["User_ID"] = Guid.NewGuid().Tostring();
dr_ user ["User_Name "] = model.UserName;
dr_ user ["User_Birth "] = model.UserBirth;
dr_ user ["UserSex"] = model.Sex;
dt_ users.Rows.Add(dr_ user);
}
}
if (dt_ users != null && dt_ user.Rows.Count > 0)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
try
{
using (SqlBulkCopy bulk = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, trans))
{
bulk.BatchSize = 1000000;
bulk.DestinationTableName = "db_User";
bulk.WriteToServer(dt_users);
trans.Commit();
IsRead = true;
}
}
catch
{
IsRead = false;
trans.Rollback();
}
}
}
}
}