批量自动导入 任意数据源的数据 至oracle
时间:2010-12-09 来源:耐得住寂寞
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
public static string sSqldrContent = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//得到数据源所有文件的“文件路径+名称”
string[] files = Directory.GetFiles(@"E:\coding\ImportBatchTxt\ErrCs1_201008\");
//循环生成ctl文件
for (int i = 0; i < files.Length; i++)
{
string file = files.GetValue(i).ToString();
//截取文件名称
string filename = file.Substring(39, 22);
Response.Write(file.ToString() + "<br/>");
Response.Write(filename.ToString() + "<br/>");
//创建ctl
this.CreateCtl(filename.ToString(), i);
}
//生成bat文件
//bat文件中命令的拼接用&,"一段命令+空格+&+空格"
string sSqlldrFileName = @"E:\coding\ImportBatchTxt\ErrCs1_201008\ctl\SqlldrFileName.bat";
FileStream fs = new FileStream(sSqlldrFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
StreamWriter swSqlldr = new StreamWriter(fs);
for (int i = 0; i < files.Length; i++)
{
string file = files.GetValue(i).ToString();
string filename = file.Substring(39, 22);
if (i == files.Length)
{
sSqldrContent = " sqlldr hn/1234@ZhengSL control='E:\\coding\\ImportBatchTxt\\ErrCs1_201008\\ctl\\" + filename + ".ctl'";
}
else
{
sSqldrContent = "sqlldr hn/1234@ZhengSL control='E:\\coding\\ImportBatchTxt\\ErrCs1_201008\\ctl\\" + filename + ".ctl' & ";
}
swSqlldr.WriteLine(sSqldrContent);
swSqlldr.Flush();
}
swSqlldr.Close();
//自动调用bat
Process.Start(@"E:\coding\ImportBatchTxt\ErrCs1_201008\ctl\SqlldrFileName.bat");
}
/// <summary>
/// 生成ctl文件
/// </summary>
/// <param name="sFileName">文件名</param>
/// <param name="i">循环的行</param>
protected void CreateCtl(string sFileName, int i)
{
string sData = sFileName;
sFileName = @"E:\coding\ImportBatchTxt\ErrCs1_201008\ctl\" + sFileName + ".ctl";
FileStream fs = new FileStream(sFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs);
string sContent = "load data ";
sContent += "infile 'E:\\coding\\ImportBatchTxt\\ErrCs1_201008\\" + sData + ".err' ";
sContent += "append into table cs_zsl ";
sContent += "fields terminated by ';' ";
sContent += "(TRADEMARK,SERVICE_ID,DR_TYPE,BILL_MONTH,VALID_RATE_PROD_ID,RATE_PROD_ID char(2048),";
sContent += "FREERES_RESERVES,ALL_PROMOTION_PRODS,LATE_LINK,INPUT_TIME,ROW_ID)";
sw.WriteLine(sContent);
sw.Flush();
sw.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
相关阅读 更多 +