约700万条数据,耗时约5分钟,
protected void Page_Load(object sender, EventArgs e)
{
string path="D://1.TXT";
string[] arr={"code","UserId","IsEncash","EncashDateTime","CreateTime","Mark"};
DataSet ds = TextFileLoader(path, "tb", arr);
int count = ds.Tables[0].Rows.Count;
BulkInsert(ds.Tables[0]);
}
/**/
/**/
/**/
/// <summary>
/// 文件加载
/// </summary>
/// <param name="FilePath">带文件名的路径</param>
/// <param name="TableName">自定义的表名</param>
/// <param name="FieldsInArray">自定义的表字段</param>
/// <returns>DataSet</returns>
public static DataSet TextFileLoader(string FilePath, string TableName, string[] FieldsInArray)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable(TableName);
FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
for (int i = 0; i < FieldsInArray.Length; i++)
{
dt.Columns.Add(new DataColumn(FieldsInArray[i], typeof(string)));
}
string strRead;
bool flag = true;
while (flag)
{
strRead = sr.ReadLine();
if (!string.IsNullOrEmpty(strRead))
{
string[] aryVale = strRead.Split('\t');
DataRow dr = dt.NewRow();
for (int k = 0; k < aryVale.Length; k++)
{
dr[FieldsInArray[k]] = aryVale[k];
}
dt.Rows.Add(dr);
}
else
{
flag = false;
}
}
ds.Tables.Add(dt);
return ds;
}
private void BulkInsert(DataTable dt) {
string time = String.Empty;
DateTime beginTime = DateTime.Now;
using (SqlBulkCopy bulk = new SqlBulkCopy(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString()))
{
//{"code","UserId","IsEncash","EncashDateTime","CreateTime","Mark"}
bulk.BatchSize = 1000;
bulk.DestinationTableName = "OfficeEncashOne";
bulk.ColumnMappings.Add("code", "code");
bulk.ColumnMappings.Add("UserId", "UserId");
bulk.ColumnMappings.Add("IsEncash", "IsEncash");
bulk.ColumnMappings.Add("EncashDateTime", "EncashDateTime");
bulk.ColumnMappings.Add("CreateTime", "CreateTime");
bulk.ColumnMappings.Add("Mark", "Mark");
bulk.WriteToServer(dt);
}
DateTime endTime = DateTime.Now;
TimeSpan useTime = endTime - beginTime;
dt.Dispose();
time = "使用时间" + useTime.TotalSeconds.ToString() + "秒";
}
{
string path="D://1.TXT";
string[] arr={"code","UserId","IsEncash","EncashDateTime","CreateTime","Mark"};
DataSet ds = TextFileLoader(path, "tb", arr);
int count = ds.Tables[0].Rows.Count;
BulkInsert(ds.Tables[0]);
}
/**/
/**/
/**/
/// <summary>
/// 文件加载
/// </summary>
/// <param name="FilePath">带文件名的路径</param>
/// <param name="TableName">自定义的表名</param>
/// <param name="FieldsInArray">自定义的表字段</param>
/// <returns>DataSet</returns>
public static DataSet TextFileLoader(string FilePath, string TableName, string[] FieldsInArray)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable(TableName);
FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
for (int i = 0; i < FieldsInArray.Length; i++)
{
dt.Columns.Add(new DataColumn(FieldsInArray[i], typeof(string)));
}
string strRead;
bool flag = true;
while (flag)
{
strRead = sr.ReadLine();
if (!string.IsNullOrEmpty(strRead))
{
string[] aryVale = strRead.Split('\t');
DataRow dr = dt.NewRow();
for (int k = 0; k < aryVale.Length; k++)
{
dr[FieldsInArray[k]] = aryVale[k];
}
dt.Rows.Add(dr);
}
else
{
flag = false;
}
}
ds.Tables.Add(dt);
return ds;
}
private void BulkInsert(DataTable dt) {
string time = String.Empty;
DateTime beginTime = DateTime.Now;
using (SqlBulkCopy bulk = new SqlBulkCopy(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString()))
{
//{"code","UserId","IsEncash","EncashDateTime","CreateTime","Mark"}
bulk.BatchSize = 1000;
bulk.DestinationTableName = "OfficeEncashOne";
bulk.ColumnMappings.Add("code", "code");
bulk.ColumnMappings.Add("UserId", "UserId");
bulk.ColumnMappings.Add("IsEncash", "IsEncash");
bulk.ColumnMappings.Add("EncashDateTime", "EncashDateTime");
bulk.ColumnMappings.Add("CreateTime", "CreateTime");
bulk.ColumnMappings.Add("Mark", "Mark");
bulk.WriteToServer(dt);
}
DateTime endTime = DateTime.Now;
TimeSpan useTime = endTime - beginTime;
dt.Dispose();
time = "使用时间" + useTime.TotalSeconds.ToString() + "秒";
}