Advertisement

C#读取CSV文件并保存进数据库

阅读量:
复制代码
    /// <summary>  

    
         /// 读文件  
    
         /// </summary>  
    
         /// <param name="path">文件路径</param>  
    
         /// <returns></returns>  
    
     internal void ReadFile(string Path)
    
     {
    
         try
    
         {
    
             FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.None);
    
             StreamReader sr = new StreamReader(fs, System.Text.UTF8Encoding.UTF8);
    
             InventorySql sql = new InventorySql();
    
  
    
             string str = "";
    
             string s = Console.ReadLine();
    
             int i = 0;
    
             while (str != null)
    
             {
    
                 str = sr.ReadLine();
    
                 if (str == null)
    
                     break;
    
                 string[] xu = new String[5];
    
                 xu = str.Split(',');
    
                 string F_Date = xu[0];
    
                 string F_Fax = xu[1].Replace("\"", "");//过滤双引号
    
                 string F_ScanId = xu[2].Replace("\"", "");
    
                 string F_StoreNumber = xu[3];
    
                 string F_Num2 = xu[4];
    
                 DateTime time = DateTime.Now;
    
                 if (F_Fax == s)
    
                 {
    
                     Log.info(F_Fax); break;
    
                 }                   
    
                 if (i != 0)//过滤表头
    
                 {
    
                     sql.Put("yearmonth", F_Date);
    
                     sql.Put("shop_no", F_Fax);
    
                     sql.Put("JAN", F_ScanId);
    
                     sql.Put("pro_count", F_StoreNumber);
    
                     sql.Put("real_count", F_Num2);
    
                     sql.Put("insert_time", time);
    
                     db.Execute(sql.GetSQL(InventorySql.InsertInventory)).ToString();
    
                 }
    
                 i++;
    
             }                
    
             sr.Close();
    
         }
    
         catch(Exception e)
    
         {
    
             throw new Exception(e.Message, e);
    
         }
    
     }

全部评论 (0)

还没有任何评论哟~