且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用带有SQL Server 2008数据库的clickonce部署Windows应用程序

更新时间:2022-11-25 14:28:50

这不是SQL Server的工作方式。您不会将 .mdf 文件复制到客户端计算机;您将它附加到现有的SQL Server实例,并让客户端连接到该实例。



如果您使用 AttachDBFileName 选项,用户需要在其计算机上安装SQL Server Express的副本。您还会发现数据库将在应用程序的不同实例之间或不同用户或不同计算机之间共享。



坏习惯:使用AttachDBFileName - SQL SentrySQL Sentry团队博客 [ ^ ]



另请注意此功能将在以后的版本中删除:



此功能将在Microsoft SQL Server的未来版本中删除。避免在新的开发工作中使用此功能,并计划修改当前使用此功能的应用程序。





如果你想要一个简单的基于文件的数据库,你可以使用 LocalDB [ ^ ],但仍需要安装。或者,您可以使用SQL CE,但功能要少得多。



SQL Express v LocalDB v SQL Compact Edition - Jerry Nixon [ ^ ]


I have a windows application and I want to create a click once app.After creating clickonce app from visual studio when I deploy the file on client machine it throws error when trying to access database.I have included database file within the clickonce app.The database file is stored on client pc on location :
C:\Users\Admin\AppData\Local\Apps\2.0\Data\8Z14E1HB.329\P54O9PWP.VCY\ nain..tion_b5deb07cfd237918_0001.0000_ce0d12c892dd7f73\Data\Database1.mdf

What connection string should I mentioned in my project that when code is published it should access the location the database is stored on client machine.
The current connectionstring is :

This connectionstring works fine when I run the code from visual studio but wont work on client side.

What I have tried:

Tried to change connection string but to no avail.If keep same connectionstring throws error 'A network related or instance specific error while establishing connection to Sql Server error-40 Could not open connection to Sql Server '

That's not how SQL Server works. You don't copy the .mdf file to the client computer; you attach it to an existing instance of SQL Server, and have the client connect to that instance.

If you're using the AttachDBFileName option, the user will need to have a copy of SQL Server Express installed on their computer. You will also find that the database will not be shared between different instances of your application, or between different users or different computers.

Bad habits : Using AttachDBFileName - SQL SentrySQL Sentry Team Blog[^]

Also note that this feature will be removed in a future version:


This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.



If you want a simple file-based database, you could use LocalDB[^], but that still needs to be installed. Alternatively, you could use SQL CE, but that has far fewer features.

SQL Express v LocalDB v SQL Compact Edition – Jerry Nixon[^]