title: How do you share a MS Access Database File in C#?
body: <p>I have a MS Access file for data logging in my C# project but when the system is open and another remote workstation wants to access this MS Access file, it notifies that you cannot access that file because it is already in use? Can this be programmed in C# that this MS Access file can be shared to another workstation?
  Here is my code.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  using System.IO;
  using System.Data.OleDb;
  
  using System.Security.AccessControl;
  
  
  using ADOX;
  
  namespace TestDataLog
  {
      public class TestDataLog
      {
          OleDbConnection dbConnection = null;
  
          OleDbConnectionStringBuilder oleDbConnectionStringBuilder = null;
  
          OleDbCommand command;
  
          public string FileName { get; set; }
  
          public string TableName { get; set; }
  
  
          public TestDataLog(string tableName, string path, string fileName = "TestDataLog.Mdb")
          {
              try
              {
                  TableName = tableName;
  
                  FileName = fileName;
  
                  dbConnection = new OleDbConnection();
  
                  oleDbConnectionStringBuilder =
  
                  new OleDbConnectionStringBuilder();
  
                  oleDbConnectionStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
  
                  path = Path.Combine(path, FileName);
  
                  oleDbConnectionStringBuilder.DataSource = path;
  
                  oleDbConnectionStringBuilder.ConnectionString += ";Jet OLEDB:Engine Type=5";
  
                  if (!File.Exists(path))
                  {
                      Catalog catalog = new Catalog();
  
                     catalog.Create(oleDbConnectionStringBuilder.ConnectionString);
                  }
  
                  this.Open();
  
                  this.CreateTable();
              }
  
              catch (Exception ex)
              {
                  this.Close();
  
                  throw ex;
              }                        
          }
  
  
          public void Log(string serial, bool testResult)
          {
              try
              {
                  command = dbConnection.CreateCommand();
  
                  command.CommandText = "INSERT INTO " + TableName +
                      " (DateTimeLog," +
                      " SN," +
                      " TestResult)" +
                      " VALUES(#" +
                      DateTime.Now + "#, '" +
                      serial + "', " +
                      testResult + ")";
  
                  command.ExecuteNonQuery();
  
                  this.Close();
              }
  
              catch (Exception ex)
              {
                  this.Close();
  
                  throw ex;               
              }
          }
  
  
          public void Open()
          {
              if (!(dbConnection.State == System.Data.ConnectionState.Open))
              {
                  dbConnection.ConnectionString =
                      oleDbConnectionStringBuilder.ConnectionString;
  
                  dbConnection.Open();
              }
          }
  
  
  
          public void Close()
          {
              if (dbConnection != null)
                  if (!(dbConnection.State == System.Data.ConnectionState.Closed))
                  {
                      dbConnection.Close();
  
                      dbConnection = null;
                  }
          }
  
  
          public void CreateTable()
          {
              try
              {
                  command = dbConnection.CreateCommand();
  
                  command.CommandText =
                      "CREATE TABLE " + TableName + " (" +
                      "[Count] IDENTITY NOT NULL PRIMARY KEY, " +
                      "[DateTimeLog] TIMESTAMP NOT NULL, " +
                      "[SN] VARCHAR(50) NOT NULL, " +
                      "[TestResult] BIT NOT NULL)";
  
                  command.ExecuteNonQuery();
              }
  
              catch (OleDbException ex)
              {
                  if (!(ex.Message.StartsWith("Table") &amp;&amp; ex.Message.EndsWith("already exists.")))
                  {
                      this.Close();
  
                      throw ex;
                  }
              }
          }
      }
  }
  </code></pre>