1

我试图让我connection string连接到我的 Access 数据库,但我遇到了这个问题。

当我将其写为连接字符串时:

string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\prjThesis\STUD_RECORD.accdb";

反斜杠被标记为红色,并且一条错误消息指出Unrecognized escape sequence指向斜杠。

我该如何解决这个问题?

4

3 回答 3

4

Use @ before the string or double slashes.

string cs = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\prjThesis\STUD_RECORD.accdb";

or

string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\prjThesis\\STUD_RECORD.accdb";
于 2013-01-18T14:07:21.853 回答
2

Try with verbtaim string literals like this;

string cs = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\prjThesis\STUD_RECORD.accdb";

Or you can use double slash (\\) in your code without verbtaim literal;

string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\prjThesis\\STUD_RECORD.accdb";
于 2013-01-18T14:08:00.730 回答
0

Use \\ (they mean \ in an ordinary string literal):

string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\prjThesis\\STUD_RECORD.accdb"

...or verbatim string literals:

string cs = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\prjThesis\STUD_RECORD.accdb"
于 2013-01-18T14:08:39.197 回答