Error "An attempt to attach an auto-named database for file"

Hi,

I've just created my first projects using Visual Studio 2005 and SQL 2005 Express. This is the basic;

  • One windows service is created that updates a database. The windows service running under "Local system" account
  • One windows application where a user can see data (and change) from the database
  • A common business and dataaccess layer/project is used by both applications
  • Connection string to the database:"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data\Database.mdf;Integrated Security=True;Connect Timeout=60;User Instance=True"

Both application is located in the same directory. When the computer starts the windows service starts and works perfectly. But when a user starts the windows applications the following error is reported"An attempt to attach an auto-named database for file..A database with the same name exists, or specified file cannot be opened, or it is located on UNC share".

It look likes that only one of my application can connect to the database at a time!

Is it possible to use separate applications to connect to the same database file?

/Robert

[1307 byte] By [RobertWarnestam] at [2007-12-22]
# 1

hi,

have a look at http://msdn.microsoft.com/sql/default.aspx?pull=/library/en-us/dnsse/html/sqlexpuserinst.asp for further info about User Instances..

regards

AndreaMontanari at 2007-8-30 > top of Msdn Tech,SQL Server,SQL Server Express...
# 2
Andrea Montanari wrote:

hi,

have a look at http://msdn.microsoft.com/sql/default.aspx?pull=/library/en-us/dnsse/html/sqlexpuserinst.asp for further info about User Instances..

regards

Thanks for the answer, I read the article but it didn't really answered my question.

I've worked with SQL Server before but not with the express edition. I'm however working with a smaller project now where it be a overkill to use a full scale database engine. Still wondering if it can be done, any sample would be nice.

/Robert

RobertWarnestam at 2007-8-30 > top of Msdn Tech,SQL Server,SQL Server Express...
# 3

hi,

you are actually working with SQL Server, as SQLExpress is just an edition of it... but only SQLExpress can expose local "User Instances".. and User Instance allow further idiosincrasies, but AttachDbFileName is just a supported extended property of SQL Server provider.. I do not actually know if it works differently on SQLExpress User Instance then in "traditional" SQL Server scenario, but I'd stay away from this feature in your case and just use SQLExpress as usual...

regards

AndreaMontanari at 2007-8-30 > top of Msdn Tech,SQL Server,SQL Server Express...
# 4

Another thought after some testing...

I created a couple of win apps that connected to the same database - that worked!

I thinkt the problem could be in that my service is on "local system" which dont use the logged on users account. One workaround could be to let the service expose some methods using remoting - but honestly, isn't there an easier way?

RobertWarnestam at 2007-8-30 > top of Msdn Tech,SQL Server,SQL Server Express...
# 5
Andrea Montanari wrote:

hi,

you are actually working with SQL Server, as SQLExpress is just an edition of it... but only SQLExpress can expose local "User Instances".. and User Instance allow further idiosincrasies, but AttachDbFileName is just a supported extended property of SQL Server provider.. I do not actually know if it works differently on SQLExpress User Instance then in "traditional" SQL Server scenario, but I'd stay away from this feature in your case and just use SQLExpress as usual...

regards

Thanks for the reply,

are you meaing that I should connect directly to a SQL server and not using the AttachDbFileName? The main reason for using filebased db was to simplify the deployment. In that case, how do I I deploy the app together with the database?

regards

RobertWarnestam at 2007-8-30 > top of Msdn Tech,SQL Server,SQL Server Express...
# 6

hi,

as usual... you can provide the DDL + DML sql scripts, like http://msdn.microsoft.com/msdnmag/issues/04/09/customdatabaseinstaller/ (or your own custom tool), or http://www.red-gate.com/products/SQL_Packager/index.htm (this is a commercial AKA $$ solution ) ...

you can even provide the .Mdf + .Ldf files to be attached at very first application run (or soon after installation) or a backup file to be restored...

I do prefer scripts scenario as this provide you a simple path for database schema changes (and, of course, you'll have to deal with thme soon or later ), it let your your whole package be under source code control (where it always should be), it does not infer your own settings, as all database settings are inherited from user's model database settings...

regards

AndreaMontanari at 2007-8-30 > top of Msdn Tech,SQL Server,SQL Server Express...

SQL Server

Site Classified