using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
namespace shengjiDate
{
public class fuyiHangyeZhishu
{
public void getAllHangyeZhishu()
{
string path = @"E:\FuyiCSapp\shengjiDate\Fuyi行业指数";
DirectoryInfo dinfo = new DirectoryInfo(path);
foreach (FileInfo file in dinfo.GetFiles())
{
addOneHangyeZhishu(file.FullName);
}
}
private void addOneHangyeZhishu(string filePath)
{
try
{
List<string> HyZs = getTextContent(filePath);
string hy = "";
int zsIndex = 0;
for (int i = 0; i < HyZs.Count; i++)
{
string[] zs;
if (i == 0)
{
zs = HyZs[i].Split(',');
hy = zs[1].ToString();
}
else if (i != 1)
{
zs = HyZs[i].Split(',');
string ZSdate = zs[0].ToString();
string shoupanjia = zs[4].ToString();
if (DateTime.Parse(ZSdate) < DateTime.Parse("2008-1-1"))
{
continue;
}
string TableName = "IndustryZS";
try
{
if (SqlHelper.ExecuteDataTable("select ZSdate from " + TableName + " where ZSdate ='" + ZSdate + "'").Rows.Count > 0)
{
string sql = "update " + TableName + " set [" + hy + "]=" + shoupanjia + " where ZSdate ='" + ZSdate + "'";
SqlHelper.ExecuteNonQuery(sql);
}
else
{
string sql = "insert into " + TableName + "(ZSdate,[" + hy + "]) values ('" + ZSdate + "'," + shoupanjia + ")";
SqlHelper.ExecuteNonQuery(sql);
}
}
catch (Exception ex)
{
if (ex.ToString().IndexOf("列名") >= 0 && ex.ToString().IndexOf("无效") >= 0)
{
string sqlAdd = "alter table [dbo].[" + TableName + "] add [" + hy + "] decimal(18,2)";
SqlHelper.ExecuteNonQuery(sqlAdd);
if (SqlHelper.ExecuteDataTable("select ZSdate from " + TableName + " where ZSdate ='" + ZSdate + "'").Rows.Count > 0)
{
string sql = "update " + TableName + " set [" + hy + "]=" + shoupanjia + " where ZSdate ='" + ZSdate + "'";
SqlHelper.ExecuteNonQuery(sql);
}
else
{
string sql = "insert into " + TableName + "(ZSdate,[" + hy + "]) values ('" + ZSdate + "'," + shoupanjia + ")";
SqlHelper.ExecuteNonQuery(sql);
}
}
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private List<string> getTextContent(string fileName)
{
List<string> str = new List<string>();
if (File.Exists(fileName))
{
StreamReader sr = new StreamReader(fileName, System.Text.Encoding.Default);
while (!sr.EndOfStream) //直到文件结束
{
str.Add(sr.ReadLine().Replace("\t",","));
}
sr.Close();
sr.Dispose();
}
return str;
}
}
}