Oledb, crash if DB path have spaces… C#

拥有回忆 提交于 2019-12-12 05:16:44

问题


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();

Thanks!

/Nick


回答1:


Wrap the path in single quotes

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


来源:https://stackoverflow.com/questions/9925369/oledb-crash-if-db-path-have-spaces-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!