2011-02-11 79 views
1

我有一個MS Access文件用於在我的C#項目中進行數據記錄,但是當系統打開並且另一個遠程工作站想要訪問此MS Access文件時,它會通知您無法訪問該文件,因爲它已被使用?這可以編程在C#中,這個MS Access文件可以共享到另一個工作站? 這是我的代碼。如何在C#中共享MS Access數據庫文件?

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") && ex.Message.EndsWith("already exists."))) 
       { 
        this.Close(); 

        throw ex; 
       } 
      } 
     } 
    } 
} 
+0

是否有您所使用的訪問,而不是像SQL Server Express的什麼原因? – 2011-02-11 23:11:23

回答

3

Mode=Share Deny None添加到您的連接字符串。這將以完全共享模式打開您的數據庫文件。不過,現在由您來管理併發。 :-)

0

當然是的。 ..只要確保您的數據庫放置在其他工作站也可以通過網絡查看的共享文件夾中。然後使用該文件所在的路徑您的數據庫就像是一座坐落:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\MySharedFolder\\TestDataLog.Mdb 

MS ACESS Connection

問候..