Search code examples
c#asp.netado.netoledbdbase

Opening the connection to OLEDB terminates the code


I am trying to create a dbf file in C#. Somehow when I am trying to open the connection to OLEDB, the program terminates without throwing an exception. Below is the code:

private void CreateDBFFile()
    {
        try
        {
            using (var dBaseConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; " + @" Data Source=k:\Temp; " + @"Extended Properties=dBase IV"))
            {
                dBaseConnection.Open();

                string createTableSyntax =
                    "Create Table Person " +
                    "(Name char(50), City char(50), Phone char(20), Zip decimal(5))";
                var cmd = new OleDbCommand(createTableSyntax, dBaseConnection);
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            string x = ex.Message;

        }

    }

The error is thrown right here:

dBaseConnection.Open();

I checked the event logs and that does not help either. Below is the error log from event log:'

Faulting application name: iisexpress.exe, version: 10.0.14358.1000, time 
stamp: 0x574fc56b
Faulting module name: clr.dll, version: 4.6.1649.1, time stamp: 0x58f97fe6
 Exception code: 0xc0000005
Fault offset: 0x0045068d
 Faulting process id: 0x3354
 Faulting application start time: 0x01d2eeacd40d1e91
Faulting application path: C:\Program Files (x86)\IIS Express\iisexpress.exe
 Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
 Report Id: 18960866-5aa0-11e7-b3f6-005056c00008

Any help will be greatly appreciated.


Solution

  • The way you have your connection is not ideal.

    Do this instead then step through the code and you should be able to get an exception or it would work: Put your connection string inside web.config.

      <connectionStrings>
        <add name="Connection" connectionString="Data Source=k:\Temp; xtended Properties=dBase IV Provider=Microsoft.Jet.OLEDB.4.0" />    
      </connectionStrings>
    
    
    string connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
               using (OleDbConnection dBaseConnection = new OleDbConnection(connection))
               {
                   if (dBaseConnection.State == ConnectionState.Closed)
                   {
                       dBaseConnection.Open();
                   }
               }