Oledb,如果数据库路径中有空格,则崩溃... C#

Oledb,如果数据库路径中有空格,则崩溃... C#

问题描述:

我有一个问题,我认为有人可以为我提供帮助,我有一个使用Access数据库的C#应用​​程序.如果我的路径中没有空格,例如"C:/Test/db.accdb",那么它就像一个超级按钮,但是,如果路径中有空格,例如"C:/Test folder/db.accdb",那么...就没有多少了.知道为什么会这样吗?我的代码看起来像这样:(查询只是一个例子,您明白了这一点:)

I have a problem that I thought someone may be able to help me with, I have a C# application that uses a Access-database. If my path is without spaces like "C:/Test/db.accdb" it works like a charm, but if the path got spaces like "C:/Test folder/db.accdb", not so much... does anyone know why this is? my code looks like this: (The query is just an example, you get the point :)

String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath;
        OleDbConnection connection = new OleDbConnection(connectionString);
        OleDbCommand command;
        connection.Open();

        command = new OleDbCommand("UPDATE Table SET Tablevalue = 1 WHERE Tablevalue2 = 3") 
        command.ExecuteNonQuery();
        connection.Close();

谢谢!

/尼克

将路径用单引号引起来

    String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + dbPath +"'"; //could use String.Format here as well.