Your question is quite vague as you do not explain what kind of “app”, “setup file” or “db” you are using, nor how you “attach it to sql”. In the future, please include these details. However, I can give a general answer.
- Create a seed database, that contains the starting data for your application, in your source project.
- Add the seed database file to your project/solution file and set its Build Action to “Content”.
- Ensure your installer includes project content in the deployment folder (the application folder for WinForms apps).
- To open the seed database from your app, use a connection string like
Data Source=|DataDirectory|seed.sdf
. Do not try to search for your seed file or to set DataDirectory
yourself; the installer will set DataDirectory
to the directory your content was installed to.
- Do not try to write to
DataDirectory
; it may not be writable by the user who installed it. Repairing the app will overwrite DataDirectory
, destroying anything you saved there, as well.
- If you need to save data in the database, copy
|DataDirectory|seed.sdf
to Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
, then read and write all data to the copy.
For more information, read my answer to a poster who wrote to |DataDirectory| and therefore kept destroying his user's data.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…