2015-09-21 207 views
2

我最近开始学习C#WPF(使用MS VS 2013和快递)和从来就试图连接到我的Access数据库没有成功,我的问题是,每当我试图做一个连接我得到这个异常“不是有效的文件名”连接到MS Access数据库

我后来才意识到不是我应该有(后一吨使用Google对此事的),它必须有一些与我的连接字符串如下:

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,唯一的区别是后面的两个选项只要我运行应用程序之前就立即返回以下异常,即使手动触发尝试连接到我的事件数据库。

型“System.IO.DirectoryNotFoundException”的异常出现在mscorlib.dll,但在用户代码中没有处理

其他信息:找不到路径“d的一部分:\谷歌驱动器\ Programmering \ C#\ WpfApplication3 \ WpfApplication3 \ BIN \ 64 \调试\传感器。

我认为这个异常的变化与我调试它为任何CPU时非常相似。

这里是我的C#代码MainWindow.xaml.cs,我的XAML代码isn't相关,因为我有一个关于我的代码数据库背后的一切。 (据我所知) 它可能看起来像一个笑话给你,但正如我刚才所说,我刚刚开始使用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访问数据库中,因为您会发现这将导致与完整路径相同的字符串,并且如预期的那样,它会返回完全相同的异常。

而且我有Office 2013 64位的Windows 10,和Visual Studio 2013 Express的台式机,所有驱动程序更新。

我已经试过各种我能想象,现在搜索小时的网络,这是我最后的手段,如果你能只发送一个链接到一个可能帮助网页我将不胜感激帮偶。 提前致谢,并对我的英语感到抱歉,因为它不是我的第一语言。

编辑我没有打开MS Access并尝试连接它,因为据我所知,它在打开时使用某种保护措施?

+1

删除_ @ _从第一连接字符串或将其添加到第二 – Steve

+0

我已经尝试了所有这些组合的不幸, @用// @ 使用/, // @不, /不带@ –

+0

然后尝试检查文件是否可从您的程序与File.Exists(.... fullfilename ...) – Steve

回答

0

我已经解决了! 我有点尴尬,因为我曾经经历过这个,但是这次从来没有过我的想法,因为我从来没有换过电脑。

问题是,当Google Drive同步我的文件时,它有时会“fk ck”,并将文件夹重命名为“sensors(1)”,而不是原来的名称“sensors”,这导致了我之间某段时间获取文件路径并尝试运行应用程序Google Drive更改了文件夹的名称因此,该路径确实无效,感谢所有试图帮助编辑我的问题的人,特别是那些编辑我的问题的人,因为我在这里还是很新的,甚至学习如何格式化我的问题。

相关问题