最近一直在用,也一直在学oralc,项目上也用到了批量的添加(读取上传CSV文件信息,把符合条件的信息写入到数据库中),在写的时候想到了可能是数据量大就想该怎么快,(由于本人在.NET开发期间没有做过深度的优化)
private string GUID;//序号GUID唯一标识
private string filename;//文件名称
private string lmportl_ID;//导入ID
private Int32? lmportl_date;//导入时间
private string diflag;// 国际/国内标识 DIFlag
上传CSV文件有专门的方法,只要复制进去就好了
#region 将CSV文件的数据读取到DataTable中
/// <summary>
/// 将CSV文件的数据读取到DataTable中
/// </summary>
/// <param name="fileName">CSV文件路径</param>
/// <returns>返回读取了CSV数据的DataTable</returns>
public static DataTable OpenCSV(string filePath)
{
DataTable dt = new DataTable();
FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.UTF8);
//StreamReader sr = new StreamReader(fs, encoding);
//string fileContent = sr.ReadToEnd();
//记录每次读取的一行记录
string strLine = "";
//记录每行记录中的各字段内容
string[] aryLine = null;
string[] tableHead = null;
//标示列数
int columnCount = ;
//标示是否是读取的第一行
bool IsFirst = true;
//逐行读取CSV中的数据
while ((strLine = sr.ReadLine()) != null)
{ if (IsFirst == true)
{
tableHead = strLine.Split(','); if (tableHead.Length > )
//截取列数
tableHead = tableHead.Skip().Take().ToArray();
IsFirst = false;
columnCount = tableHead.Length; //创建列
for (int i = ; i < columnCount; i++)
{
tableHead[i] = tableHead[i].Replace("\"", "");
DataColumn dc = new DataColumn(tableHead[i]);
dt.Columns.Add(dc);
}
}
else
{
aryLine = strLine.Split(',');
if (aryLine.Length > )
aryLine = aryLine.Skip().Take().ToArray();
DataRow dr = dt.NewRow();
//是否超过1000行
if (dt.Rows.Count <= )
{
//Prefix和FormType字符串长度是否超过3
if (aryLine[].Length <= && aryLine[].Length <= )
{
dr[] = aryLine[].Replace("\"", "");
dr[] = aryLine[].Replace("\"", "");
}
else
{
dr[] = aryLine[].Substring(, );
dr[] = aryLine[].Substring(, );
}
//TicketNo 字符串长度是否超过8
if (aryLine[].Length <= )
{
dr[] = aryLine[].Replace("\"", "");
}
else
{
dr[] = aryLine[].Substring(, );
}
}
else
{
return dt;//跳出程序,行数大于1000,不做添加,
}
dt.Rows.Add(dr);
}
}
if (aryLine != null && aryLine.Length > )
{
dt.DefaultView.Sort = tableHead[];
}
sr.Close();
fs.Close();
return dt;
}
#endregion
上传完成的CSV文件返回的DataTable 我需要把DataTable 转货成List,我们用的EF所以可以直接转换
DataEnt = DataConvertor.GetEntityList<t>(dtb) as List<T>;
把List传入方法中
#region 插入票号转换数据到表
/// <summary>
/// 插入票号转换数据到表
/// </summary>
/// <param name="">表的实体</param>
/// <returns></returns>
public BaseEntityJsonObject<int> Insert_TRANSFER(List<T> T)
{ BaseEntityJsonObject<int> result = new BaseEntityJsonObject<int>();
int count = ;
string strSql = "";
try
{
StringBuilder strBuilder = new StringBuilder(); strBuilder.Append("INSERT INTO 表名( 字段明,字段明,字段明) ");
strBuilder.Append("select sequence_name.nextval ,t.c1,t.c2,t.c3,t.c4,t.c5 FROM ( ");
foreach (var item in T)
{
strBuilder.Append(" SELECT ");
strBuilder.Append("'" + item.字段名+ " 'C1,'" + item.字段名+ " 'C2,'" + item.字段名+ "' C3,");
strBuilder.Append("'字段名' C4 ,'" + 字段名+ "' C5 FROM DUAL union all ");
}
strBuilder.Append(" ) T");
strSql = string.Format(strBuilder.ToString());
int i = ;
//sql写完后这里嘚坐sql的截取,要不然会报错
strSql = strSql.Remove(strSql.Length - i, ); count = DbContext.Database.ExecuteSqlCommand(strSql); }
catch (Exception ex)
{
ExceptionHandler.HandleException(ex); }
return result;
}
#endregion
这就是添加!
下面写批量修改
写修改前先要把要修稿的数据查询出来,转货成LIst,我就不说怎么查询,怎么转货List了
我这里写的流程是先把List写进数据库的视图中,然后数据插进伪表中 然后根据伪表中的数据做进一步的条件,不说了,直接上代码
#region 修改转换数据到表
/// <summary>
/// 修改转换数据到表
/// </summary>
/// <param name="">表的实体</param>
/// <returns></returns>
public BaseEntityJsonObject<int> Update_TRANSFER(List<T> T)
{
BaseEntityJsonObject<int> result = new BaseEntityJsonObject<int>();
int count = ;
string strSql = "";
try
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append(" MERGE INTO TRT_TICKETNUMBER_TRANSFER T1 USING ( ");
//从这里循环要修改的数据,把数据先临时放到伪表中
foreach (var item in t)
{
strBuilder.Append(" SELECT '" + item.要修改的字段名+ "' a, '" + item.要修改的字段名+ "' b FROM DUAL t ");
strBuilder.Append(" union all ");
}
string str2 = strBuilder.ToString();
if (str2 != "")
{
str2 = str2.Remove(str2.Length - );
}
//把sql进行截取,要不然会报错,然后在拼接SQL语句 StringBuilder strBuilder2 = new StringBuilder();
strBuilder2.Append(" ) T2 ON ( T1.表中的主键字段 = T2.a 临时表的字段 ) ");
strBuilder2.Append(" WHEN MATCHED THEN ");
strBuilder2.Append(" UPDATE SET T1.需要修改的字段名 = t2.b"); strSql = str2 + strBuilder2.ToString();
count = DbContext.Database.ExecuteSqlCommand(strSql);
result.payload = count;
result.errorCode = ;
}
catch (Exception ex)
{
ExceptionHandler.HandleException(ex); LogHelper.Error(string.Format("添加表数据失败"), ex);
}
return result;
}
#endregion
这样就完成了数据的修改,
由于临时想起来写博客,没有准备充分,
有什么不妥的欢迎大家指出,欢迎大家吧问题指出来,然后大家共同进步