Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接到MS access数据库_C#_.net_Wpf_Xaml_Ms Access - Fatal编程技术网

C# 连接到MS access数据库

C# 连接到MS access数据库,c#,.net,wpf,xaml,ms-access,C#,.net,Wpf,Xaml,Ms Access,我最近开始学习C#WPF(使用MS VS 2013 express),并且我尝试连接到access数据库,但没有成功。我遇到的问题是,每当我尝试建立连接时,我都会遇到此异常“不是有效的文件名” 后来我意识到这一定与我的连接字符串有关,如下所示: connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=‪D:\\Google Drive\\Programmering\\C#\\WpfAppli

我最近开始学习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进行调试,唯一的区别是,在手动触发试图连接到数据库的事件之前,后两个选项在我运行应用程序时立即返回以下异常

mscorlib.dll中发生“System.IO.DirectoryNotFoundException”类型的异常,但未在用户代码中处理

其他信息:找不到路径“D:\Google Drive\Programmering\C#\WpfApplication3\WpfApplication3\bin\x64\Debug\sensors”的一部分

我假设这个异常变量与我作为任何CPU调试它时的情况几乎相同

这是我的C#code
main window.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)
        {
        }
    }
}
其中,
\\sensors
是我的bin\debug文件夹中的文件夹,
sensor.Header
来自
public void addSensors()
方法,该方法在以前的上下文中显式有效,我将信息存储在.txt文件中,而不是MS access db中,正如您所看到的,这将导致与完整路径相同的字符串,并且正如预期的那样,它返回完全相同的异常

此外,我还有Office 2013 64位、Windows 10和Visual Studio 2013 express桌面版,所有驱动程序都已更新

我已经尝试了我能想象的一切,在网上搜索了几个小时,这是我最后的手段,如果你能帮我,哪怕只是发送一个链接到一个可能有帮助的网页,我都会非常感激。 提前谢谢,对不起,我的英语不是我的第一语言

编辑我在尝试连接MS Access时没有打开它,因为我知道它在打开时使用了某种保护?

我已经解决了它! 我有点尴尬,因为我以前也经历过这种情况,但这次我从没想过,因为我从来没有换过电脑


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

从第一个连接字符串中删除@或将其添加到第二个连接字符串我已尝试了所有这些组合,很遗憾,@with//、//with//、//without@、/without@然后尝试检查该文件是否可以从程序中通过file.Exists(..fullfilename…)访问好的,我明天会试试,谢谢你的快速回复:)你的第一个示例连接字符串不应该使用@符号。连接字符串的第二个示例应使用@符号。此外,您应该检查您的代码是否可以访问access db。换句话说,按照史蒂夫的建议去做。他正在引导你朝着好的方向前进。
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;";