读取记事本

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;
        }
    }
}

转载于:https://www.cnblogs.com/server/archive/2009/09/10/1564223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值