How can I update a cell value in a dB table, using SQL Server CE and C# (Visual Studio 2010)

前端 未结 2 909
情书的邮戳
情书的邮戳 2020-11-29 14:27

I\'m writing a small application for college, a video rental application. I have no problems reading from the database, but I cannot update the data in the tables.

相关标签:
2条回答
  • 2020-11-29 14:35

    You have to tell us exactly what connection string you are using, saying things like "I updated the connection to point to the database in the bin folder" is not helpful. Are you using a connection string like c:\Users\Me\Visual Studio 2010\Projects\MyProject\bin\debug\HKRMoviesDB.sdf? That is not right, that is hard-coded to your VS2010 debug test folder on your machine, and will not work for anyone else, will only work when you run from the debugger, and will not work when you deploy your app.

    ADO.NET looks for vertical bars in connection strings and expands the enclosed string using the AppDomain property. For example, if you use Data Source=|DataDirectory|\HKRMoviesDB.sdf from the VS debugger, DataDirectory expands to your project's bin folder, where VS copies the debug DLLs. If you deploy your app using an MSI installer, DataDirectory expands to your app's install folder chosen by the user. Further explanation is at this answer.

    Be aware that DataDirectory may not be a directory writable by users; it is a good directory for read-only data, but if you want users to write to the file you should copy it to Environment.SpecialFolder.ApplicationData or somewhere in your app, as explained in this answer.

    There is one more thing to keep in mind. When you are debugging you typically want to copy your original source data to the debug folder, test it, then discard your test data because you typically do not want your test data included in the final deployment. This is what the "Copy to Output Directory" property in your project does. If it is set to "copy if newer", which it probably should be, then any changes you make to your test data is temporary; the next time you update your original source data in your project, your test data will be discarded. Remember, your application's deployment directory is typically not writable by users so you should not be writing user data in DataDirectory anyway.

    0 讨论(0)
  • 2020-11-29 14:41

    A fellow student pointed this out to me:

    "From what I understand, the database that your code updates, unless you hard coded the path to the database is the file in your /bin/debug/ folder. The file you view in Visual studio is the one in your project root."

    I updated the connection to point to the database in the bin folder.

    0 讨论(0)
提交回复
热议问题