C#遍历某文件夹下的所有文件,读取文件并插入到数据库中

    

   1 先讲个小问题,前面文章http://blog.csdn.net/joqwer/article/details/38388653中提到已经有File.ReadAllLines(path);来读取文件了,为什么还需要FileStream之类的东东?File.ReadAllLines(path);不能读取中文,好吧,这里FileStream还是不需要,网上查找到的有使用StreamReader,FileStream没解决,反而有多出来一个StreamReader:

            StreamReader sr= new StreamReader(filePath, UnicodeEncoding.GetEncoding("GB2312"));
            string line = "";
            while (line != null)
            {
                line = sr.ReadLine();
                if (line != null && !line.Equals(""))
                   MessageBox.Show(line);
            }
            sr.Close();

    当是了解一下吧,因为这里StreamReader仍用不上,而通过File.ReadAllLines(path, UnicodeEncoding.GetEncoding("GB2312"));就可以解决。

    

    2 进入正题:

            if (folderLogsDialog.ShowDialog() == DialogResult.OK)
            {
                string[] logFilePaths = Directory.GetFiles(folderLogsDialog.SelectedPath);
                string[] allLines = null; //allLines各元素存放一行数据
                string strStartE = "export";
                string strEnd = ".log";
                string logFileName = null;

                string strFilePath = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + Application.StartupPath + "\\testInfo.accdb";
                System.Data.OleDb.OleDbConnection con = new OleDbConnection(strFilePath);
                con.Open();

                foreach (string logFilePath in logFilePaths)
                {
                    int position = logFilePath.LastIndexOf('\\'); //若找不到?
                    logFileName = logFilePath.Substring(position+1); //取得文件名

                    if (!(logFileName.StartsWith(strStartE) && logFileName.EndsWith(strEnd))) //判断是否是符合条件的文件
                        continue;

                    allLines = File.ReadAllLines(logFilePath, UnicodeEncoding.GetEncoding("GB2312")); //读文件

                    testInfo testInfo = new testInfo();
                    OleDbCommand cmd;

                    foreach (string line in allLines)
                    {
                        string[] split = line.Split(new Char[] { ' ', ',', ':', '\t', '\r', '\n' });

                        if (split.Length != (int)(enumExportFile.end)) //采集的数据不符合Export结构体
                            continue;

                        string sql = String.Format(@"insert into testInfo(double1, double2, double3, str1, str2) Values 
                                                    ('{0}', '{1}', '{2}', '{3}', '{4}')", testInfo.double1, testInfo.double2,
                                                        testInfo.double3, testInfo.str1, testInfo.str2);
                        cmd = new OleDbCommand(sql, con);
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                    }
                }
                con.Close();
                con.Dispose();
            }

            MessageBox.Show("OK");

1)简单解释:

A :首先需要一个FolderBrowserDialog

B:代码全部放到一个button_click里

C:取得当前文件下的所有文件,对于每个文件,解析文件名看是否是需要的,否则跳过;

D:以GB2312的方式循环读取每个文件,解析各行,将所需要的数据插入到数据库中。至于数据库操作请看之前的文章


2)学习到的类:

A:FolderBrowserDialog:打开文件夹对话框,那当然包含该文件夹的路径了folderLogsDialog.SelectedPath

B:Directory类:GetFiles方法,很好用,string[] logFilePaths = Directory.GetFiles(folderLogsDialog.SelectedPath);只要传入路径就返回给你所有该目录下的文件,子文件夹下的文件??自己找找帮助文档看有没有吧。

C:String类:LastIndexOf方法、Substring方法,截取子字符串一般可以结合IndexOf等相关函数如LastIndexOf使用;此外还有判断开头结尾的函数StartsWith、EndsWith。至于split、Format,都是老朋友了,之前的文章都有提到,解析字串和类似sprintf。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值