SQL Server Express in APP_DATA and running with IIS
I haven't really been paying much attention to SQLEXPRESS and the APP_DATA directory in the past but over the last couple of days of finalizing my slides for the ASP.NET Connections conference I realized that it's one heck of a lot easier to ship a database in APP_DATA then to have scripts or even something like Red Gate's Sql Packager generated database. Sql Packager BTW, is an excellent tool if you need to shuttle around databases frequently between machines as it packages databases into an EXE that you can move and simply install on another machine. It's much faster than anything native including attaching/detaching.
Anyway, using APP_DATA and shipping a detached database is also very easy and it works great if you have SQL Express installed and you're running with the built-in ASP.NET 2.0 Web server.
However, I've not been able to get APP_DATA to work with IIS. I get security errors which basically point at the ASPNET account not having rights to the database. I'm not sure exactly how SQLExpress deals with permissions in this scenario, but it's not recognizing the ASPNET account as a valid user even though on this demo machine it is set up as an Admin account (yeah I know bad idea, but for demos this is lets me use integrated security so I don't have to expose passwords to the world). I also made sure I added ASPNET to APP_DATA ACL list and gave it full permissions.
I figured that SQLEXPRESS must be triggering off the impersonated account in some way, but in that case ASPNET should work the same as my login account both of which are Admin at this point. And it's not…
The workaround I suppose is to attach the database and then administer the security policy properly through SQL Security, but given that the expectation is SQLEXPRESS this is not so transparent since SQL Express doesn't come with Administration tools (although you can download a beta version of the Express Management Studio).
Is there an easier way?
The Voices of Reason
# re: SQL Server Express in APP_DATA and running with IIS
connectionString="Data Source=.\SQLExpress;AttachDBFilename=|DataDirectory|MyDataBase.mdf;Integrated Security=True;User Instance=True;
# re: SQL Server Express in APP_DATA and running with IIS
# re: SQL Server Express in APP_DATA and running with IIS
Jacques - I agree and that's usually what I do with my databases in APP_DATA: I attach them to my full local SQL Server. Not only is it still easier to manage data and queries etc. in Management Studio but it also reduces the application load time which is horrible with the local database needing to attach.
I'm more thinking of the ease of use of people who download the demo and only have SQL Express and nothing else.
# re: SQL Server Express in APP_DATA and running with IIS
# re: SQL Server Express in APP_DATA and running with IIS
connect to the db via express manager, rename the database from the default [C:\bar\foo\aspnetdb.mdf]
to aspnetdb
then edit the config to just database=aspnetdb
and forget the attach and user instance.
works now.
Oh and I did add NT AUTHORITY\Network service as a valid user/login
# re: SQL Server Express in APP_DATA and running with IIS
Unfortunately if you attach the database now and you have many of those you can run into problems with the name.
# re: SQL Server Express in APP_DATA and running with IIS
# re: SQL Server Express in APP_DATA and running with IIS
the simple answer is to let the SQL Express services run under the System account instead of the Network Services Account ...
Hope this helps..
# re: SQL Server Express in APP_DATA and running with IIS
Many thanks. You ended several DAYS of beating my head against the wall with your simple fix. YES. Changing SQL Express services to run under Local System instead of Network Services fixed ALL of my problems at once.
The root of my issue is I have group of setup scripts that live on the C: drive. The C: drive is NOT shared, so Network Services does not trust it. Therefore, I got "Access is denied" every time I tried to run a setup script via OSQL.
# re: SQL Server Express in APP_DATA and running with IIS
Are the Silverlight security policies the problem? Thanks in advance.
# re: SQL Server Express in APP_DATA and running with IIS
# re: SQL Server Express in APP_DATA and running with IIS
how do you change which account Sql Express runs under. Do you have to reinstall?
# re: SQL Server Express in APP_DATA and running with IIS
# Could not attach database '' to file 'c:\inetpub\wwwroot\SqlServer\App_Data\imsys_Data.MDF'.
i have delpoyed my website to ftp and iis server,, the site is working well,, but the problem is, when i am debuging the website using visual web developer 2008 then the database is working well, but after the deployment it does not wrok... everytime i got the error :
Could not attach database '' to file 'c:\inetpub\wwwroot\SqlServer\App_Data\imsys_Data.MDF'.
this may be due to change of path.. from sql/data/<database Name> to c:\inetpub\wwwroot\<website>\<database>.mdf
please help me what to do with this
thanx
regards
# re: SQL Server Express in APP_DATA and running with IIS
FWIW...