2016-06-30 9 views
9

Excel dosyasındaki verilerin (paylaşılan yolda bulunan) Veritabanına taşındığı bir uygulama var. Herhangi bir hata durumunda, dosyalar bir günlük dosyasına hata yazarak hata klasörüne gider. İşlem için bir windows servisi kullanır.Bazı Excel Dosyaları Paylaşılan Yoldan SQL Server'a Taşınmadı

Bazen dosyada herhangi bir hata yok, hata kaydı klasörüne yazılıyor External table is not in the expected format. Günlüğe yazılıyor ancak aynı dosya bir kez veya daha fazla kez tekrar yükleniyor, herhangi bir hata olmadan Veritabanına aktarılıyor.

Windows hizmeti, DB ve paylaşılan yol XP Server'da bulunur. Uygulama tüm bu yıllarda iyi çalışıyordu. Ancak son günlerde, yukarıda sözü edilen problem neredeyse her dosya için ortaya çıkmaktadır.

Microsoft 2003, 2007,2012 ofis bileşenlerini ve erişim motorlarını da yükledik. Ama yine de sorun hala devam ediyor.

Aşağıdaki Windows hizmet kodunu belirtiyorum. Pls yardımcı olur. Şimdiden teşekkürler.

using System.IO; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Data.SqlClient; 
using System.Data.OleDb; 
using System.Data.Common; 

namespace Impexp_Service 
{ 
    public partial class Service1 : ServiceBase 
    { 
     System.Timers.Timer T1 = new System.Timers.Timer(); 
     public Service1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
      { 
      ///start 
      /// 

      { 
       SqlConnection strconnection = new SqlConnection(); 
       strconnection.ConnectionString = @"Data Source=XXXXXX;Initial Catalog=XXXX;User ID=XX;Password=XXXXXX;"; 
       strconnection.Open(); 
       // To get the all files placed at the shared path 
       DirectoryInfo directory = new DirectoryInfo(@"D:\Impexp\Data\"); 
       FileInfo[] files = directory.GetFiles("*.xlsx"); 



       foreach (var f in files) 
       { 
        string path = f.FullName; 

        // TO establish connection to the excel sheet 
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";"; 
        //Create Connection to Excel work book 
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); 



        excelConnection.Open(); 
        //Create OleDbCommand to fetch data from Excel 
        OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection); 

        DbDataReader dr = cmd.ExecuteReader(); 
        // OleDbDataReader dReader; 
        // dReader = cmd.ExecuteReader(); 
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection); 
        //Give your Destination table name 
        sqlBulk.DestinationTableName = "imp_master_test"; 
        sqlBulk.WriteToServer(dr); 

        excelConnection.Close(); 

        File.Delete(path); 




        // To move error files to the error folder 



        /// end 


        T1.Interval = 20000; 
        T1.Enabled = true; 
        T1.Start(); 

        T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed); 
       } 
      } 
    } 


     void T1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
     { 
      T1.Enabled = false; 
      try 
      { 
       SqlConnection strconnection = new SqlConnection(); 
       strconnection.ConnectionString = @"Data Source=10.91.XXXXXX;Initial Catalog=XXXXX;User ID=XXXXX;Password=XXXXX;"; 
       strconnection.Open(); 
       // To get the all files placed at the shared path 
       DirectoryInfo directory = new DirectoryInfo(@"D:\Impexp\Data\"); 
       FileInfo[] files = directory.GetFiles("*.xlsx"); 



       foreach (var f in files) 
       { 
        string path = f.FullName; 

        // TO establish connection to the excel sheet 
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";"; 
        //Create Connection to Excel work book 
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); 

       try 
       { 
        excelConnection.Open(); 
        //Create OleDbCommand to fetch data from Excel 
        OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection); 

        DbDataReader dr = cmd.ExecuteReader(); 
        // OleDbDataReader dReader; 
        // dReader = cmd.ExecuteReader(); 
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection); 
        //Give your Destination table name 
        sqlBulk.DestinationTableName = "imp_master_prod"; 
        sqlBulk.WriteToServer(dr); 

        excelConnection.Close(); 

        File.Delete(path); 



       } 
       // To move error files to the error folder 
       catch (Exception exp) 
       { 

        excelConnection.Close(); 
        File.Move(path, Path.Combine(@"D:\Impexp\error\", f.Name)); 
        string path1 = @"D:\Impexp\error\error.txt"; 
        if (File.Exists(path1)) 
        { 
         // Create a file to write to. 
         using (StreamWriter sw = File.AppendText(path1)) 
         { 
          sw.WriteLine("File : " + path + " : " + exp.Message); 
          sw.Flush(); 

         } 
        } 


         T1.Enabled = true; 
         T1.Start(); 

        } 
       } 
       strconnection.Close(); 

      // End of TRY 1 

      } 
      catch (UnauthorizedAccessException UAEx) 
      { 
       string path1 = @"D:\Impexp\error\error.txt"; 
       if (File.Exists(path1)) 
       { 
        // Create a file to write to. 
        using (StreamWriter sw = File.AppendText(path1)) 
        { 
         sw.WriteLine(UAEx.Message); 
         sw.Flush(); 

        } 
       } 
       T1.Enabled = true; 
       T1.Start(); 
      } 
      catch (PathTooLongException PathEx) 
      { 
       string path1 = @"D:\Impexp\error\error.txt"; 
       if (File.Exists(path1)) 
       { 
        // Create a file to write to. 
        using (StreamWriter sw = File.AppendText(path1)) 
        { 
         sw.WriteLine(PathEx.Message); 
         sw.Flush(); 

        } 
       } 
       T1.Enabled = true; 
       T1.Start(); 
      } 
      T1.Enabled = true; 
      T1.Start(); 


     } 

     protected override void OnStop() 
     { 
     } 
    } 
} 
+2

'Çok sayıda dosya' için başarısız olduğunu varsayarsak, DO 'a geçenler hakkında' farklı 'bir şey var mı? (ör., sayfaların sayısı, [Raporlar] sayfasındaki kayıtların sayısı, ...). En kötüsü, başarısız ve tamam bir dosyaya daha yakından bakın: bunları açın ve tüm (ilgili) dosyaları karşılaştırın. Not: (sınırlı) deneyimimde, excel'den veri yükleme bazı bölgesel ayarlardan dolayı başarısız olabilir (sunucudaki VE/VEYA istemci makinelerinde). Not: Geçmişte çalıştığı için önemli olduğunu düşünmüyorum; ancak connectiontring 'Excel 12.0 Xml' dememeli mi? https://www.connectionstrings.com/ace-oledb-12-0/ – deroby

+0

@deroby Yorumunuz için teşekkürler efendim. Bu soruna neden olan bölgesel ayarlara biraz ışık tutabilir misiniz? – METALHEAD

+1

Ayrıca, bağlantı dizgisi durumları :: 'string excelConnectionString = @" Sağlayıcı = Microsoft.ACE.OLEDB.12.0; Veri Kaynağı = "+ yol +"; Genişletilmiş Özellikler = \ "Excel 12.0; HDR = Evet; IMEX = 1 \" ;; '' Excel 12.0 öncesi ve sonrası var, HDR = Evet, IMEX = 1'. Bu soruna neden oluyor .. ?? – METALHEAD

cevap

0

this question baktığımızda, bu excel dosyası değil SQL tablosu okuma ile bir sorunu olarak görünmektedir. Excel bağlantı dizesini değiştirmeyi deneyin.

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";"; 

Ayrıca

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;HDR=Yes;IMEX=1\"; 

için, another answer bakarak, kök neden excel yeni sürümlerini yükleyerek mümkündür.

İlgili konular