2017-08-28 82 views
12

最近我遇到了我正在創建的C#程序拋出異常Could not find file C:\windows\system32\qbcdb.mdb的問題。這很奇怪,因爲在通過高級安裝程序部署我的程序之前,我從未遇到過這個問題。我沒有改變任何東西,但由於某種原因,這個錯誤不斷髮生(例外框的屏幕截圖 - http://imgur.com/1GLhwmg)。Windows窗體程序找不到mdb文件(C: windows system32 qbcdb.mdb)

我不知道要在這個問題中包含什麼來幫助解釋我的問題更多,所以這裏是我的App.config文件(在一個網站上閱讀它可能與此有關,但我再也沒有有迄今爲止的任何問題):

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
     <add name="QBC.Properties.Settings.qbcdbConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\qbcdb.mdb" 
      providerName="System.Data.OleDb" /> 
    </connectionStrings> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
</configuration> 

插入到數據庫的方法:

從DB方法
#region inserts the data into the database 
private void InsertData() 
{ 
    using (dbConn) 
    { 
     dbConn.Open(); 

      using (dbCmd = new OleDbCommand("INSERT INTO members (household_head, birthday, phone, email, address, status, spouse, spouse_birthday, spouse_phone, spouse_email, " + 
       "anniversary, spouse_status, child1, child1_birthday, child1_email, " + 
     "child2, child2_birthday, child2_email, child3, child3_birthday, child3_email, child4, child4_birthday, child4_email, child5, child5_birthday, child5_email," + 
     "child6, child6_birthday, child6_email, child7, child7_birthday, child7_email) " + 
     "VALUES (@txtBox_householdHead, @txtBox_householdHeadBirthday, @txtBox_householdHeadPhone, @txtBox_householdHeadEmail, @txtBox_householdHeadAddress, @txtBox_householdHeadStatus, " + 
     "@txtBox_spouse, @txtBox_spouseBirthday, @txtBox_spousePhone, @txtBox_spouseEmail, @txtBox_Anniversary, @txtBox_spouseStatus, " + 
     "@txtBox_child1, @txtBox_child1Birthday, @txtBox_child1Email, " + 
     "@txtBox_child2, @txtBox_child2Birthday, @txtBox_child2Email, @txtBox_child3, @txtBox_child3Birthday, @txtBox_child3Email, @txtBox_child4, @txtBox_child4Birthday, @txtBox_child4Email, " + 
     "@txtBox_child5, @txtBox_child5Birthday, @txtBox_child5Email, @txtBox_child6, @txtBox_child6Birthday, @txtBox_child6Email, @txtBox_child7, @txtBox_child7Birthday, @txtBox_child7Email)", dbConn)) 
      { 
       try 
       { 
        InsertDBParameters(ref dbCmd); 

        dbCmd.ExecuteNonQuery(); 
       } 
       catch (OleDbException ex) 
       { 
        MessageBox.Show(ex.ToString()); 
        return; 
       } 
       finally 
       { 
        dbConn.Close(); 
       } 
      } 


      MessageBox.Show("Record inserted."); 

      ClearAll(this); 
     } 


} 
#endregion 


#region creates the db parameters 
private void InsertDBParameters(ref OleDbCommand cmd) 
{ 

    foreach (Control c in Controls) 
    { 
     if (c is TextBox) 
     { 
      listOfTextboxes.Add(new KeyValuePair<string, string>(((TextBox)c).Name, ((TextBox)c).Text)); 
      } 
     } 

     for (int i = 0; i < listOfTextboxes.Count; i++) 
     { 
      cmd.Parameters.AddWithValue(String.Format("@{0}", listOfTextboxes[i].Key.ToString()), listOfTextboxes[i].Value); 
     } 



} 
#endregion 

選擇 -

#region displays all members in the database 
private void MenuViewMembers_Click(object sender, EventArgs e) 
{ 
     // hide any controls left that may be left over from another option 
     HideAllControls(this); 



     qbcDataGridView.Font = new Font(qbcDataGridView.Font.FontFamily, 10); 

     qbcDataGridView.Location = new Point(30, 100); 

     qbcDataGridView.Size = new Size(1500, 500); 


     dbConn.Open(); 

     DataTable dt = new DataTable(); 


     DbAdapter = new OleDbDataAdapter("select ID, household_head AS head, birthday, phone, email, address, status, spouse, spouse_birthday AS sbirthday, spouse_email AS semail, anniversary," + 
      " spouse_status AS sstatus," + 
      "child1, child1_birthday AS birthday1, child1_email AS email1, child2, child2_birthday AS birthday2, child3, child3_birthday AS birthday3, child3_email AS email3, " + 
      "child4, child4_birthday AS birthday4, child4_email AS email4, child5, child5_birthday AS birthday5, child5_email AS email5," + 
      "child6, child6_birthday AS birthday6, child6_email AS email6, child7, child7_birthday AS birthday7, child7_email AS email7 from members", dbConn); 
     DbAdapter.Fill(dt); 

     for (int i = dt.Columns.Count - 1; i >= 0; i--) 
     { 
      if (dt.AsEnumerable().All(row => row[i].ToString() == "")) 
      { 
       dt.Columns.RemoveAt(i); 
      } 
     } 

     qbcDataGridView.DataSource = dt; 


     qbcDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; 

     qbcDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; 

     qbcDataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True; 


     dbConn.Close(); 

     Controls.Add(qbcDataGridView); 
} 
#endregion 

裏面的visual studio,它運行良好,但是當我使用高級安裝程序爲它構建msi時,我嘗試從數據庫中插入/選擇時收到該錯誤消息。

如果這些信息不足,我很抱歉,我不知道真正提供什麼來幫助解釋我的情況。

謝謝!

+0

什麼是DataDirectory你的情況?它是哪條路? – tym32167

+0

它說在qbcdb.mdb屬性下的完整路徑是'C:\ Users \ jimmy \ OneDrive \ documents \ visual studio 2017 \ Projects \ QBC \ QBC \ qbcdb.mdb' – user2101411

+0

您在ConnectionString中使用過'DataDirectory'意味着數據庫更新位於項目的子文件夾BIN \ DEBUG文件夾中。指出位於項目本身中的那個,而不是Bin/Debug。 https://stackoverflow.com/a/31605045/2946329 –

回答

0

首先,C:\ windows \ system32 \是而不是是存儲運行時數據的正確位置。這種方法是無效的,因爲Windows 95失去了時尚,甚至在那裏它只工作,因爲FAT無法防止這種情況。特別是該文件夾通過權利和過程如Virtualsiation保護。而且這種情況在將來只會變得更糟。所以最好不要從它開始。

存儲此類數據的「正確」位置在Special Folders之一。無論是用戶特定的還是Windows寬帶的。 ApplicationData和CommonApplicationData應該是最好的地方。

請注意,如果多於一個用戶運行程序(runas,切換用戶)並且句柄不能快速釋放,則跨用戶位置會引發問題。一般來說,如果目標是一個跨用戶文件夾,則該目標應該被假定爲不可寫/只能在短的突發中寫入。

0

當我使用高級安裝程序爲它構建msi時,當我嘗試從數據庫插入/選擇時收到該錯誤消息。

要解決此問題,您需要一個名爲Process Monitor的工具。 Proces sMonitor跟蹤硬盤,註冊表,事件&實時處理。

快速啓動跟蹤,重現錯誤並在失敗時快速停止跟蹤。然後調查ProcMon(Filemon)日誌以查看應用程序在哪找到它無法找到的AccessDB。它將是一個ACCESS DENIED或一個Path Not Found

從這裏您可以確定文件的位置。我懷疑它會像用戶對C:\ Windows \ system32的權限被拒絕。或者用戶有一個D:\驅動器操作系統而不是C:\或類似的東西,你可以通過指定數據來修復目錄正確。

作爲一個經驗法則,不要使用System32作爲文件的傾倒地。 Access數據庫應位於程序文件或用戶數據目錄中,或位於FileShare上(如果由> 1人使用)。

相關問題