views:

84

answers:

1

hi, I'm not very good at deployment and I'm facing a problem:

First Off, I'm using Vista.

i have created a windows application in vb.net which has a database file named Customerdb.mdf in it.

I have selected the database and in the property, I have selected Embedded Resource option.

I have used the following connection:

Dim constring As String
constring = Application.StartupPath.ToString() + "\Customerdb.mdf"
Dim con = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" + constring + ";Integrated Security=True;User Instance=True")

My problem is that I cannot access the database after installing the setup. the following error is thrown each time i try to access the database:

"An attempt to attach an auto-named database for file C:\Program File\App\Customerdb.mdf failed. A database with the same name exits, or specified file cannot be opened, or it is located on UNC share."

It seems like once installed, the application cannot have access to the database.

Could you please tell me what is wrong with the connection?

Any help will be very very appreciated. Thanks in advance

+1  A: 

I have selected Embedded Resource option.

That means the database file is embedded inside the created *.exe file as a resource. It's not a separate file on the file system. The file name you're trying to create with this code: Application.StartupPath.ToString() + "\Customerdb.mdf" doesn't exist.

To fix this, you have a few options:

  • Change from Embedded Resource to Content and tell it to "Always Copy"
  • Check if the file exists and write the byte array out to disk before opening the database if it doesn't
  • Change from Embedded Resource to Content and set your deployment project to put it in the Application Data folder.

Of those, you really should to do the last one, and don't forget to deploy sql server express with your app.

Finally, Sql Server Express is a really poor choice for use as a single-user desktop database. You really should go for a desktop (or "in-process") engine like SQL Server Compact Edition, sqlite, or even Access.

Joel Coehoorn
thanks for replying, i got the first point, but can u provide a sample of how to do this, because i tried and i got the same error message. with the "content" option, the database file still not showing in the application folder after setup. i suppose the path im using is correct.thanks for replying!
You still have to add it to the deployment project manually. Setting it to 'Content' just makes it available to the project so you can add it.
Joel Coehoorn
thanks, let me try this
i put selected content option and put the database file in the App folder (which is displayed in the program files) during the deployment. afer the setup, the database file shows in the App folder but i got the following error code when truing to access the database:could not open new database 'C:\PROGRAM FILES\APP\CUSTOMERDB.MDF'. CREATE DATABASE is aborted.An attempt to attach an auto-named database for file C:\Program File\App\Customerdb.mdf failed.
thanks for keep on helping!
That's either because you're not deploying sql server express edition or because the app doesn't have write access to that folder. Even if these things are okay for you, they will be a problem for your users. As I said: you should really convert this project to an _in-process_ database engine.
Joel Coehoorn
An-process database engine, how? thanks once again.
**In**-process engine. Like Sql Server Compact or Access. There will be some slight differences in sql syntax and you'll need the System.Data.OleDb or System.Data.SqlCe namespace instead of System.Data.SqlClient, but otherwise everything works exactly the same, and deployment and user experience are _much nicer.
Joel Coehoorn
thanks for replying sir. i have been trying for the whole week end. can you please provide me with a very detailled sample with a step by step procedure. I will appreciate it too much. Iam submitting my project soon. Please I need your help!
Get Sql Server Compact Edition here: http://www.microsoft.com/sqlserver/2008/en/us/compact.aspx A complete tutorial on how to use it is beyond the scope of a simple comment, especially since _it works pretty much the same_ as sql server express.
Joel Coehoorn