string path = @"D:\1.sql";
StreamReader sr = File.OpenText(path);
DataTable dt = new DataTable();
dt.Columns.Add("UserName",typeof(string));
dt.Columns.Add("UserPwd", typeof(string));
dt.Columns.Add("Email", typeof(string));
int i = 0;
int j = 0;
while (!sr.EndOfStream)
{
string str = sr.ReadLine().Trim();
if(str == "")
{
continue;
}
string[] arr = StringHelper.SplitString(str, " # ");
try
{
if (arr.Length <= 1)
continue;
DataRow dr = dt.NewRow();
dr[0] = arr[0].Trim();
dr[1] = arr[1].Trim();
dr[2] = arr[2].Trim();
dt.Rows.Add(dr);
}
catch
{
continue;
}
i++;
j++;
if (dt.Rows.Count == 4000)
{
SqlBulkCopy bulkCopy1 = new SqlBulkCopy(ConfigurationSettings.AppSettings["SqlConnString"].ToString(), SqlBulkCopyOptions.Default);
//每一批次中的行数
bulkCopy1.BatchSize = 1000;
bulkCopy1.DestinationTableName = "Users";
SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("UserName", "UserName");
SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("UserPwd", "UserPwd");
SqlBulkCopyColumnMapping mapping3 = new SqlBulkCopyColumnMapping("Email", "Email");
bulkCopy1.ColumnMappings.Add(mapping1);
bulkCopy1.ColumnMappings.Add(mapping2);
bulkCopy1.ColumnMappings.Add(mapping3);
bulkCopy1.WriteToServer(dt);
bulkCopy1.Close();
dt.Rows.Clear();
i = 0;
}
}
if (dt.Rows.Count > 0)
{
SqlBulkCopy bulkCopy = new SqlBulkCopy(ConfigurationSettings.AppSettings["SqlConnString"].ToString(), SqlBulkCopyOptions.Default);
//每一批次中的行数
bulkCopy.BatchSize = 1000;
bulkCopy.DestinationTableName = "Users";
SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("UserName", "UserName");
SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("UserPwd", "UserPwd");
SqlBulkCopyColumnMapping mapping3 = new SqlBulkCopyColumnMapping("Email", "Email");
bulkCopy.ColumnMappings.Add(mapping1);
bulkCopy.ColumnMappings.Add(mapping2);
bulkCopy.ColumnMappings.Add(mapping3);
bulkCopy.WriteToServer(dt);
bulkCopy.Close();
dt.Rows.Clear();
}