我最近开始学习 C# WPF(使用 MS VS 2013 Express)并且我尝试连接到我的 Access 数据库但没有成功,我遇到的问题是每当我尝试建立连接时我都会得到这个异常“不是有效的文件名”。
我意识到它一定与我的连接字符串有关:
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=D:\\Google Drive\\Programmering\\C#\\WpfApplication3\\WpfApplication3\\bin\\Debug\\sensors\\MPU6050.accdb;
Persist Security Info=False;";
并且此路径是从文件的属性/安全选项卡复制的,因此应该是正确的。 我也试过
connect.ConnectionString ="Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=D:\Google Drive\Programmering\C#\WpfApplication3\WpfApplication3\bin\Debug\sensors\MPU6050.accdb;
Persist Security Info=False;";
相同但不包括开头的 @。
我曾尝试将其调试为任何 CPU、x64 和 x86,唯一的区别是后两个选项在我运行应用程序后立即返回以下异常,甚至在手动触发尝试连接到我的数据库的事件之前.
An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code
Additional information: Could not find a part of the path 'D:\Google Drive\Programmering\C#\WpfApplication3\WpfApplication3\bin\x64\Debug\sensors'.
我假设这个异常 var 与我将其调试为 Any CPU 时几乎相同。
这是我的 C# 代码 MainWindow.xaml.cs
和我的 XAML 代码不相关,因为我的代码后面有关于数据库的所有内容。 (据我所知)
这对您来说可能像是个玩笑,但正如我之前所说,我刚刚开始使用 WPF(和 C#),而且我自己已经知道我可以在某些方面更有效地处理事情。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.IO;
using System.Data.OleDb;
using System.Data;
using System.Windows.Threading;
namespace WpfApplication3
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
avbComPort.Text = "COM Port";
addSensors();
foreach (string s in SerialPort.GetPortNames())
{
ComboBoxItem cbi = new ComboBoxItem();
cbi.Content = s;
avbComPort.Items.Add(cbi);
}
}
public void addSensors()
{
string dynamicPath = System.IO.Directory.GetCurrentDirectory();
string fullPath = dynamicPath + "\\sensors";
string[] sensors = Directory.GetFiles(fullPath);
int fileQuantity = sensors.Length -1;
for (int i = 0; i <= fileQuantity ; i++)
{
string path = sensors[i];
string[] pathArr = path.Split('\\');
string[] fileArr = pathArr.Last().Split('.');
string fileName = fileArr.First().ToString();
MenuItem sensor = new MenuItem {Header = fileName};
sensor.Click += new RoutedEventHandler(sensor_Click);
confSensors.Items.Add(sensor);
}
}
public void sensor_Click(Object sender, RoutedEventArgs e)
{
MenuItem sensor = sender as MenuItem;
TabItem tab = new TabItem { Header = sensor.Header, Width = sensorTab.Width, Height = sensorTab.Height };
DataGrid dataLog = new DataGrid() { Name = "dataLog", IsReadOnly = true, Width = 300, Height = 500, HorizontalAlignment = 0, VerticalAlignment = 0, AutoGenerateColumns = true, ItemsSource = "Binding"};
string filePath = System.IO.Directory.GetCurrentDirectory() + "\\sensors" + sensor.Header + ".accdb";
try //code regarding the database connection
{
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Google Drive\\Programmering\\C#\\WpfApplication3\\WpfApplication3\\bin\\Debug\\sensors\\MPU6050.accdb; Persist Security Info=False;";
connect.Open();
dbStatusLbl.Content = "Connection to database established successfully";
connect.Close();
}
catch(Exception ex)
{
MessageBox.Show("A problem occured while trying to establish a stable connection to the database: " + ex.Message, "A wild error has appeared", MessageBoxButton.OK, MessageBoxImage.Error);
} // end of that code
Grid grid = new Grid() { Height = tab.Height, Width = tab.Width};
grid.Children.Add(dataLog);
tab.Content = grid;
sensorTab.Items.Add(tab);
}
private void sensorTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
我还尝试使用这些片段以编程方式获取文件名
string filePath = System.IO.Directory.GetCurrentDirectory() + "\\sensors" + sensor.Header + ".accdb";
connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|filePath|; Persist Security Info=False;";
\\sensors
是我的 bin\debug 文件夹中的文件夹,sensor.Header
来自 public void addSensors()
方法并且在以前的上下文中明确工作,我将信息存储在 .txt 文件而不是 MS Access 数据库中,如您所见,这将导致与完整路径相同的字符串,并且如预期的那样,它返回完全相同的异常。
此外,我还有 Office 2013 64 位、Windows 10 和 Visual Studio 2013 express for desktop,所有驱动程序都已更新。
我已经尝试了我能想到的一切并在网上搜索了几个小时,这是我最后的手段,如果你能提供帮助,即使只是发送一个指向可能有帮助的网页的链接,我将不胜感激。 在此先感谢并为我的英语感到抱歉,因为它不是我的母语。
编辑 我在尝试连接时没有打开 MS Access,因为我知道它在打开时使用了某种保护措施?
最佳答案
我已经解决了! 我有点尴尬,因为我以前经历过这种情况,但这次我从来没有想过,因为我从来没有换过电脑。
问题是,当 Google Drive 同步我的文件时,它有时会“f*ck a bit”并将文件夹重命名为“sensors(1)”而不是原始名称“sensors”,这导致我得出结论文件路径并尝试运行应用程序 Google Drive 更改了文件夹的名称因此路径确实无效,感谢所有试图提供帮助的人,尤其是编辑我的问题的 2,因为我在这里很新并且还在学习甚至如何格式化我的问题。
关于c# - 连接到 MS Access 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703669/