问题
SqlCeConnection cs = new SqlCeConnection(@"Data Source=C:\Users\Shahid\Documents\Visual Studio 2010\Projects\DarUlIfta\DarUlIfta\DB.sdf");
This works fine with complete path
But only problem I'm facing when I use relative path my database stop saving data into it. While at run time it works fine when I close the app there is noting in the database. What should I do?
回答1:
Your connection string is to a database inside your project source folder. That is not right; when you deploy software you do not deploy your source code. You have to determine the path to your deployed data. There are several ways to do this, depending on information which you have not shown us.
As a first guess, try this:
SqlCeConnection cs = new SqlCeConnection(@"Data Source=|DataDirectory|\DB.sdf");
If that works, you have to learn about deployment properties. If that doesn't work, we will need a lot more information about what you are trying to do.
Also, consult Using a database in .NET.
来源:https://stackoverflow.com/questions/12093962/sqlceconnection-relative-path-for-connection-string-fails