20

We have an application where the data in Excel file (present in shared path) moves to Database. In case of any error, the files moves to error folder by writing the error in a log file.It uses a windows service for the operation.

Sometimes the file doesn't have any error still moves to error folder by writing log External table is not in the expected format. But the same file uploading again for once or multiple times, its moving to Database without any errors.

The windows service, DB and shared path are present in XP Server. Application was running fine all these years. But in the recent days, above mentioned problem is occurring for almost every file.

We have installed Microsoft 2003, 2007,2012 office components and access engines too. But still the issue still persists.

I am mentioning the Windows service code below. Pls help. Thanks in advance.

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()
        {
        }
    }
}
Stefan Drissen
  • 3,266
  • 1
  • 13
  • 21
METALHEAD
  • 2,734
  • 3
  • 22
  • 37
  • 2
    Assuming it fails for 'a lot of files', is there anything 'different' about those that DO pass? (e.g. number of sheets, number of records in [Reports] sheet, ...) . Worst case, have a closer look at a failed and an OK file: unzip them and compare all (relevant) files. PS: In my (limited) experience, loading data from excel via might fail due to some regional settings thing (on the server AND/OR on the client machines). PS: I don't think it matters since it has worked in the past; but shouldn't the connectionstring say `Excel 12.0 Xml` ? https://www.connectionstrings.com/ace-oledb-12-0/ – deroby Aug 08 '16 at 09:02
  • @deroby Thanks for your comment sir. Can you please throw some light on the regional settings thats causing this issue ? – METALHEAD Aug 08 '16 at 09:45
  • 1
    Also my connection string states :: `string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";` There is a \ before and after `Excel 12.0;HDR=Yes;IMEX=1`. Is this causing the trouble..?? – METALHEAD Aug 08 '16 at 09:48
  • Well, in theory excel stores its information in 'native format', thus a date is stored internally as a double. However, sometimes users copy-paste things from one place to another and end up pasting e.g. date-information into excel as a string. Excel accepts this even though the cell is formatted as date. It looks OK to have `2000-04-18` there but to excel it means nothing because the regional settings expect `2000/04/18` (example). When you try to read this using OLEDB you might be expecting datetimes for a given column but are presented with strings (containing unrecognizable date-info). – deroby Aug 09 '16 at 10:36
  • As for the connectionstring: not a clue. I simply pointed you to the connectionstrings website because I noticed a missing `Xml` part. Best way to figure out if things are important is by simply trying out what the effects may be IMHO. – deroby Aug 09 '16 at 10:38
  • External table is not in the expected format. I guess it is most probably talking about the SQL table where the data is heading. You probably have not told it the format in advance since it is a dumb machine. It could also be saying this about the Excel table if it does not know what format this table is to be in or it is guessing it should be of a certain format since you did not specify for it what format it should be in. But in the recent days, above mentioned problem is occurring for almost every file. – Soliman Soliman Dec 08 '16 at 11:32
  • But in the recent days, above mentioned problem is occurring for almost every file. So you need to find out what happened in recent days. Did you get an MS SQL update that changed the format of the tables for example? Tough one huh! – Soliman Soliman Dec 08 '16 at 11:33
  • Sometimes it can happen (by using Using() wrong as example) that the file is still used from a process, so it can't be moved. – Cataklysim Feb 10 '17 at 17:12

3 Answers3

1

I did some searching regarding the OLEDB coms and newer versions of Excel. It seems as though a great many people are having compatibility issues with them.

Unfortunately, it doesn't look like Microsoft is giving this any attention. Microsoft announced the depreciation of OLEDB functionality many years ago, and they have stopped adding any kind of internal support in their Office products and SQL servers. In fact, The official shutdown date for MSAccess Web Apps and Web Databases was April 2018. That being the case, an update to the server, the client version of windows, or the client version of Excel may have triggered this, and it doesn't look like there is going to be a fix. I myself have started using 3rd party packages (free ones are available) to handle the interop with office products because I was tired of banging my head against the wall to create workarounds. Honestly, I don't know why Access still exists if they are taking away the ability to programmatically connect to an Access database.

I know this doesn't fix your problem, but it's better to face the truth and move on than to try to fix something that isn't going to fix.

Tim Jones
  • 185
  • 2
  • 11
0

Looking at this question, this appears to be an issue with reading the excel file, not the SQL table. Try changing the Excel connection string.

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

to

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

Also, looking at another answer, the root cause could be uploading newer versions of excel.

Community
  • 1
  • 1
Andrew O'Brien
  • 1,793
  • 1
  • 12
  • 24
-1

Are you using an Excel 2007 file with a connection string that uses: Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0?

You can change the string to some other as below:

public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx";
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";

OR

Change the connection string from the link below: http://www.connectionstrings.com/excel-2007

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Pallavi
  • 21
  • 2