SqlConnection conn = new SqlConnection(ScheduleTaskMgr.CONN_STRING_NON_DTC);
SqlCommand cmd = new SqlCommand();
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
//创建存放Raw Data的文档的文件夹
string timeFlag = DateTime.Now.ToFileTime().ToString();
string dirStr = path + timeFlag;
Directory.CreateDirectory(dirStr);
//分批次捞取Raw Data
Obj = nextBatch(dr);
if (Obj.Count != 0)
{
while (Obj.Count > 0)
{
//将每一批次的Raw Data写入Excel
// 导出excel方法自己去找很多
//重新抓一批Raw Data
Obj = null;
Obj = nextBatch(dr);
}
conn.Close();
//压缩文件
ZipDAO dao = new ZipDAO(dirStr);
dao.ZipFold();
DeleteFile(dirStr);
Console.WriteLine("处理完毕");
Thread.Sleep(1000);
dirStr = null;
return downloadPath + timeFlag + ".rar";
}
else
{
conn.Close();
DeleteFile(dirStr);
Console.WriteLine("处理完毕");
return "false";
}
}
//分批次获取SqlDataReader,一次取oneBatchSize==40000笔
public IList<ProblemCodeRawdataInfo> nextBatch(SqlDataReader dr)
{
IList<ProblemCodeRawdataInfo> Obj = new List<ProblemCodeRawdataInfo>();
int i = 0;
while (dr.Read())
{
Obj.Add(PopulateProblemCodeRawdataFromDr(dr));
Console.WriteLine(i.ToString());
i++;
if (i >= oneBatchSize)
break;
}
return Obj;
}
//删除文件夹
private static void DeleteFile(string folderPath)
{
if (Directory.Exists(folderPath))
{
Directory.Delete(folderPath, true);
}
}