我是C#的新手,我目前正在学习Windows窗体。连接到本地数据库时,我收到有关连接字符串的错误。我得到的错误是Sql在以下行 -
2017-01-08 19:19:25 INFO PersonService:26 -
Method com.baeldung.performancemonitor.PersonService.getAge
execution started at:Sun Jan 08 19:19:25 EET 2017
2017-01-08 19:19:25 INFO PersonService:33 -
Method com.baeldung.performancemonitor.PersonService.getAge execution lasted:50 ms
2017-01-08 19:19:25 INFO PersonService:34 -
Method com.baeldung.performancemonitor.PersonService.getAge
execution ended at:Sun Jan 08 19:19:25 EET 2017
2017-01-08 19:19:25 WARN PersonService:37 -
Method execution longer than 10 ms!
这里,我在文件名中收到错误。我附上了一个截图,以便于检测。
答案 0 :(得分:3)
您有几个选择:
请改为尝试:
con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;" +
"AttachDbFilename=\"c:\\users\\devesh lashkari\\documents\\visual studio 2015\\Projects\\DemoApp1.0\DemoApp1.0\\DemoAppDataBase.mdf\";" +
"Integrated Security=True");
...或者如果您想使用@
,则需要使用GalacticCowboy(下文)中提到的双引号:
con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""c:\users\devesh lashkari\documents\visual studio 2015\Projects\DemoApp1.0\DemoApp1.0\DemoAppDataBase.mdf"";Integrated Security=True");
请注意,这不是SQL错误,而是c#转义错误。您需要路径中的引用,因此您无法使用@
。我把它分成多行,以便于阅读。
你在c:
感谢GalacticCowboy提示和发现我的愚蠢。 ;)