Attaching SQL Express database via code (.NET) or Installshield
Posted on 2011-03-01
I have a SQL Express database that I need to include with an application. I have created an Installshield package with SQL Express 2005 as a prerequisite.
Now I want to be able to attach a SQL Express database prior to the application running.
Rather than doing this via an InstallShield script of which I have very little knowledge of, I thought I could create a connection to the database when the application runs and run the SQL command:
create database MyDB ON (FILENAME = 'C:\MyData\MyDB.mdf') FOR ATTACH;
(I have verified that running that SQL in Management Studio successfully attaches the database).
However I am getting an error:
"Unable to open the physical file "C:\MyData\MyDB.mdf". Operating system error 32: "32(error not found)"."
My VB.NET code is:
Dim sql As New CommonTools.SQL.SQLObjects(strConn)
Dim strCommand As String = "create database EPMNetDB ON (FILENAME = 'C:\MyData\MyDB.mdf') FOR ATTACH;"
sql.Cmd.CommandType = CommandType.Text
Catch ex As Exception
The connection to the database is fine, the error only occurs when trying to execute the SQL
Any suggestions regarding this error, or a better way of attaching a SQL Express database at runtime, or even a way to attach the database as part of the InstallShield process.