最近我遇到了我正在創建的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時,我嘗試從數據庫中插入/選擇時收到該錯誤消息。
如果這些信息不足,我很抱歉,我不知道真正提供什麼來幫助解釋我的情況。
謝謝!
什麼是DataDirectory你的情況?它是哪條路? – tym32167
它說在qbcdb.mdb屬性下的完整路徑是'C:\ Users \ jimmy \ OneDrive \ documents \ visual studio 2017 \ Projects \ QBC \ QBC \ qbcdb.mdb' – user2101411
您在ConnectionString中使用過'DataDirectory'意味着數據庫更新位於項目的子文件夾BIN \ DEBUG文件夾中。指出位於項目本身中的那個,而不是Bin/Debug。 https://stackoverflow.com/a/31605045/2946329 –