110,532
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
int i = 0;
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = "D:\\文献"; //设置监控的文件目录
fsw.IncludeSubdirectories = true; //设置监控C盘目录下的所有子目录
fsw.Filter = "*.txt"; //设置监控文件的类型
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size; //设置文件的文件名、目录名及文件的大小改动会触发Changed事件
fsw.Created += new FileSystemEventHandler(fsw_Created); //绑定事件触发后处理数据的方法。
fsw.EnableRaisingEvents = true; //启动监控
while (true)
{
Thread.Sleep(5000);// 创建和控制线程,设置其优先级并获取其状态
}
void fsw_Created(object sender, FileSystemEventArgs e)
{
Console.SetCursorPosition(0, 1);
List<string> s = new List<string>();
s.Add(e.Name);
string newname = e.Name;
string str = "D:\\文献\\" + newname;
//string filename1 = newname;
int judgenumber = 1;
//try
//{
//定义一个开始时间
DateTime startTime = DateTime.Now;
//创建内存临时数据表来存储从文本文件中读取出来的数据
using (DataTable table = new DataTable())
{
//为数据表创建相对应的数据列
table.Columns.Add("进站车站编码", typeof(System.String));
table.Columns.Add("进站日期", typeof(System.String));
table.Columns.Add("进站时间", typeof(System.String));
table.Columns.Add("出站车站编码", typeof(System.String));
table.Columns.Add("出站日期", typeof(System.String));
table.Columns.Add("出站时间", typeof(System.String));
table.Columns.Add("清分日期", typeof(System.String));
table.Columns.Add("卡号", typeof(System.String));
table.Columns.Add("卡类型", typeof(System.String));
table.Columns.Add("交易代码", typeof(System.String));
table.Columns.Add("闸机号", typeof(System.String));
table.Columns.Add("数据更新时间", typeof(System.String));
//因为文件比较大,所有使用StreamReader的效率要比使用File.ReadLines高
while (true)
{
try
{
using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
if (stream != null)
break;
}
System.Threading.Thread.Sleep(50000);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Output file {0} not yet ready ({1})", e.Name, ex.Message));
}
using (StreamReader sr = new StreamReader(new FileStream(e.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.Default))
{
while (!sr.EndOfStream)//获取一个值,该值指示当前的流位置是否在流结尾
{
DataRow dr = table.NewRow();//创建数据行
string readStr = sr.ReadLine();//读取一行数据
if (judgenumber > 1)//去掉标题行
{
string[] strs = readStr.Split(new char[] { '\t', '"' }, StringSplitOptions.RemoveEmptyEntries);//将读取的字符串按"制表符/t“和””“分割成数组
string a = strs[0];
string b = strs[1];
string c = strs[2];
string d = strs[3];
string m = strs[4];
string f = strs[5];
string g = strs[6];
string h = strs[7];
string n = strs[8];
string j = strs[9];
string k = strs[10];
string l = strs[11];
//往对应的 行中添加数据
dr["进站车站编码"] = a;
dr["进站日期"] = b;
dr["进站时间"] = c;
dr["出站车站编码"] = d;
dr["出站日期"] = m;
dr["出站时间"] = f;
dr["清分日期"] = g;
dr["卡号"] = h;
dr["卡类型"] = n;
dr["交易代码"] = j;
dr["闸机号"] = k;
dr["数据更新时间"] = l;
table.Rows.Add(dr);//将创建的数据行添加到table中
}
judgenumber = judgenumber + 1; //if(judgenumber==16)
}
sr.Close();
sr.Dispose();
}
string connStr = "Data Source = orcl; Persist Security Info = True; User ID = noistest; Password = noistest";
OracleConnection conn = new OracleConnection(connStr);
OracleBulkCopy bulkCopy = new OracleBulkCopy(connStr, OracleBulkCopyOptions.UseInternalTransaction); //用其它源的数据有效批量加载Oracle表中
//conn.BeginTransaction();
//OracleBulkCopy bulkCopy = new OracleBulkCopy(connOrcleString, OracleBulkCopyOptions.Default);
bulkCopy.BatchSize = 100000;
bulkCopy.BulkCopyTimeout = 260;
bulkCopy.DestinationTableName = "AFC"; //服务器上目标表的名称
bulkCopy.BatchSize = table.Rows.Count; //每一批次中的行数
try
{
conn.Open();
if (table != null && table.Rows.Count != 0)
bulkCopy.WriteToServer(table); //将提供的数据源中的所有行复制到目标表中
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
//
if (bulkCopy != null)
bulkCopy.Close();
}
}
}
Console.WriteLine(e.Name + "正在进行");
}