0

This exception was originally thrown at this call stack:

System.Data.OleDb.OleDbConnectionInternal.OleDbConnectionInternal(System.Data.OleDb.OleDbConnectionString, System.Data.OleDb.OleDbConnection)
    System.Data.OleDb.OleDbConnectionFactory.CreateConnection(System.Data.Common.DbConnectionOptions, System.Data.Common.DbConnectionPoolKey, object, System.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection)
    System.Data.ProviderBase.DbConnectionFactory.CreateConnection(System.Data.Common.DbConnectionOptions, System.Data.Common.DbConnectionPoolKey, object, System.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection, System.Data.Common.DbConnectionOptions)
    System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionPoolGroup, System.Data.Common.DbConnectionOptions)
    System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, out System.Data.ProviderBase.DbConnectionInternal)
    System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions)
    System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions)
    System.Data.ProviderBase.DbConnectionInternal.OpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory)
    System.Data.OleDb.OleDbConnection.Open()
    WindowsFormsApp1.DB_Connect.DB_Fields() in DB_Connect.cs
    ...
    [Call Stack Truncated]

It was working Till yesterday now it is not working, can someone please guide me what is wrong in the following code:

public static string[] DB_Fields()
{

String[] s = new string[21];
int i = 0, res1;
string s1 = "-";

            string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mu\Desktop\9-50070_000018.csv;" + @"Extended Properties='Excel 8.0;HDR=No;'";
            using (OleDbConnection connection = new OleDbConnection(con))
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand("select * from [9-50070_000018.csv$]", connection);
                using (OleDbDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        var data = dr[0];
                        //Console.WriteLine(data);
                        res1 = Strings.InStr(data.ToString(), s1, CompareMethod.Text);
                        //MessageBox.Show(res1.ToString());
                        //MessageBox.Show(data.ToString());
                        if (res1 == 0)
                        {
                            s[i] = data.ToString();
                            i++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            return s;
        }
qamar
  • 1
  • 5
  • Use the `Microsoft.ACE.OLEDB.12.0` or `Microsoft.ACE.OLEDB.16.0` instead of `Microsoft.Jet.OLEDB.4.0`. When you open a CSV file, the connection string contains the **Directory Path**, not the **.csv file name** and the Extended Properties do not specify `Excel 8.0`, but `text`. The query, `SELECT * FROM`, specifies the file name, not an Excel-like sheet like this `[9-50070_000018.csv$]`. + (more problems) -- See the different examples [here](https://stackoverflow.com/a/54352568/7444103) – Jimi Jan 13 '20 at 02:50
  • Can you please write a proper statement ? – qamar Jan 13 '20 at 02:54
  • I have followed your given link and still I am getting error for connection.Open – qamar Jan 13 '20 at 03:03
  • Can you open the file with Notepad? Did the data change in the file recently? Did you do any windows updates recently? – jdweng Jan 13 '20 at 03:25
  • I have changed the connection string as Jimi guidance now I am getting invalid path error, where as my path is right. – qamar Jan 13 '20 at 03:49

0 Answers0