Can't connect to my SQL Server database?

泪湿孤枕 提交于 2019-12-24 11:20:03

问题


I'm creating a very basic CRUD desktop winforms application in C#/.NET 4.0.

Letting Visual Studio auto-generate the fields for the table I'd like to perform my CRUD operations on works just fine, but I'm running into problems when I try and interact with it manually with my own SQL queries.

The auto-generated fields are using the connection string:

Data Source=|DataDirectory|\Data Analysis.sdf

If I try and do:

SqlConnection conn = new SqlConnection(@"Data Source=|DataDirectory|\Data Analysis.sdf");
conn.Open();

It just hangs. What am I missing?


回答1:


That's a connection string for a SQL Server Compact Edition (CE) database (everything stored inside a single .sdf file) - is that what you're using?

If so : in that case, you'd have to use SqlCeConnection (not a SqlConnection - that's for "grown-up" SQL Server version - not CE)




回答2:


Maybe try adding some more options to the connection string:

Persist Security Info=False;
File Mode=shared read;



回答3:


Believe you've specified a relative path to the .sdf file, where you might need to get the executable's runtime folder from System.Environment.CurrentDirectory and prepend it to the filename.



来源:https://stackoverflow.com/questions/12109696/cant-connect-to-my-sql-server-database

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