问题
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