I have created a simple C# application that stores some info in a SQL Server CE database, searches for a specific row etc... I can read from the database without any problem, but inserting data into it isn't going as smoothly.
I checked many sources online, and what I got is that I can't use this connection string:
SqlCeConnection connection = new SqlCeConnection(@"Data Source=|DataDirectory|\Database1.sdf");
because |DataDirectory|
in this case works with reading from the database, but not with inserting. What I should do according to online sources is use a hard-coded path for the database, ie :
SqlCeConnection connection1 = new SqlCeConnection(@"Data Source=C:\Users\user\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.sdf");
However, if I do this, it will cause major deployment program because the hard-coded path won't be the same on all the machines. Please any suggestions would be greatly appreciated.
You can use the hard-coded path in debugging and |DataDirectory|
in deployment as below.
SqlCeConnection connection;
if (System.Diagnostics.Debugger.IsAttached)
{
SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\Users\user\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.sdf");
}
else
{
connection = new SqlCeConnection(@"Data Source=|DataDirectory|\Database1.sdf");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.