|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Trying to restore a .bak in C#RESTORE DATABASE NewDB FROM DISK = 'ApplicationStartupPath\SQLSE2005\DevDB.bak' WITH RECOVERY, MOVE 'DevDB_Data' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\NewEB_Data.mdf', MOVE 'DevDB_Log' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Log.ldf' while connected to the master db installed by default in SQLSE 2005? If not how the heck do I run this command to restore this db for the client when they install the app? I'm trying to do this through VS 2003 so that would be another question--2003 isn't going to have any problems doing commands for SQL 2005 is it? Or am I just screwed here? TIA Jayyde
Show quote
"Jayyde" <a***@hotmail.com> wrote in message That should work fine, provided the account has permissions and the backup news:%23e2w7euwGHA.4300@TK2MSFTNGP05.phx.gbl... > Can I run a SQL command like: > > RESTORE DATABASE NewDB FROM DISK = > 'ApplicationStartupPath\SQLSE2005\DevDB.bak' WITH RECOVERY, MOVE > 'DevDB_Data' TO 'C:\\Program Files\\Microsoft SQL > Server\\MSSQL.1\\MSSQL\\Data\\NewEB_Data.mdf', MOVE 'DevDB_Log' TO > 'C:\\Program Files\\Microsoft SQL > Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Log.ldf' > > while connected to the master db installed by default in SQLSE 2005? If > not > how the heck do I run this command to restore this db for the client when > they install the app? > > I'm trying to do this through VS 2003 so that would be another > question--2003 isn't going to have any problems doing commands for SQL > 2005 is it? Or am I just screwed here? > and data files are all visible and accessable to the SQL Server. What problem are you having? David As soon as the code below gets to RestoreCommand.ExecuteNonQuery() it throws
an exception. ****CODE********************************************* strConnectionString = "Server=(local)\\SQLEXPRESS;"; strConnectionString += "Database=master;"; strConnectionString += "uid=sa;"; strConnectionString += "pwd=********;"; strRestoreSQL = "RESTORE DATABASE NewDB "; strRestoreSQL += "FROM DISK = '" + Application.StartupPath + "\\SQLSE2005\\OldDB.bak' "; strRestoreSQL += "WITH RECOVERY, "; strRestoreSQL += "MOVE 'OldDB_Data' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Data.mdf', "; strRestoreSQL += "MOVE 'OldDB_Log' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Log.ldf'"; RestoreConnection = new SqlConnection(strConnectionString); RestoreConnection.Open(); RestoreCommand = new SqlCommand(); RestoreCommand.Connection = RestoreConnection; RestoreCommand.CommandText = strRestoreSQL; RestoreCommand.CommandTimeout = 0; RestoreCommand.ExecuteNonQuery(); RestoreConnection.Close(); ******************************************************* - Jayyde Show quote "David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in message news:eZx3X3uwGHA.4460@TK2MSFTNGP04.phx.gbl... > > "Jayyde" <a***@hotmail.com> wrote in message > news:%23e2w7euwGHA.4300@TK2MSFTNGP05.phx.gbl... >> Can I run a SQL command like: >> >> RESTORE DATABASE NewDB FROM DISK = >> 'ApplicationStartupPath\SQLSE2005\DevDB.bak' WITH RECOVERY, MOVE >> 'DevDB_Data' TO 'C:\\Program Files\\Microsoft SQL >> Server\\MSSQL.1\\MSSQL\\Data\\NewEB_Data.mdf', MOVE 'DevDB_Log' TO >> 'C:\\Program Files\\Microsoft SQL >> Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Log.ldf' >> >> while connected to the master db installed by default in SQLSE 2005? If >> not >> how the heck do I run this command to restore this db for the client when >> they install the app? >> >> I'm trying to do this through VS 2003 so that would be another >> question--2003 isn't going to have any problems doing commands for SQL >> 2005 is it? Or am I just screwed here? >> > > That should work fine, provided the account has permissions and the backup > and data files are all visible and accessable to the SQL Server. What > problem are you having? > > David >
Show quote
"Jayyde" <a***@hotmail.com> wrote in message Apart from having no local variables and too much string concatenation news:eCktb7uwGHA.2208@TK2MSFTNGP05.phx.gbl... > As soon as the code below gets to RestoreCommand.ExecuteNonQuery() it > throws an exception. > > ****CODE********************************************* > > strConnectionString = "Server=(local)\\SQLEXPRESS;"; > > strConnectionString += "Database=master;"; > > strConnectionString += "uid=sa;"; > > strConnectionString += "pwd=********;"; > > strRestoreSQL = "RESTORE DATABASE NewDB "; > > strRestoreSQL += "FROM DISK = '" + Application.StartupPath + > "\\SQLSE2005\\OldDB.bak' "; > > strRestoreSQL += "WITH RECOVERY, "; > > strRestoreSQL += "MOVE 'OldDB_Data' TO 'C:\\Program Files\\Microsoft SQL > Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Data.mdf', "; > > strRestoreSQL += "MOVE 'OldDB_Log' TO 'C:\\Program Files\\Microsoft SQL > Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Log.ldf'"; > > RestoreConnection = new SqlConnection(strConnectionString); > > RestoreConnection.Open(); > > RestoreCommand = new SqlCommand(); > > RestoreCommand.Connection = RestoreConnection; > > RestoreCommand.CommandText = strRestoreSQL; > > RestoreCommand.CommandTimeout = 0; > > RestoreCommand.ExecuteNonQuery(); > > RestoreConnection.Close(); > > ******************************************************* > > there's nothing wrong with your code. When ExecuteNonQuery throws an exception, there will be useful information in there indicating the cause of the problem. What is it? David Jayyde wrote:
Show quote > As soon as the code below gets to RestoreCommand.ExecuteNonQuery() it throws What is Application.StartupPath pointing to?> an exception. > > ****CODE********************************************* > > strConnectionString = "Server=(local)\\SQLEXPRESS;"; > > strConnectionString += "Database=master;"; > > strConnectionString += "uid=sa;"; > > strConnectionString += "pwd=********;"; > > strRestoreSQL = "RESTORE DATABASE NewDB "; > > strRestoreSQL += "FROM DISK = '" + Application.StartupPath + > "\\SQLSE2005\\OldDB.bak' "; > Hi Jayyde
Given that this script will be run during installation, I assume you want it to actually CREATE the database, right? If so, you should include the REPLACE option in the command, eg: .... WITH REPLACE, RECOVERY, MOVE ..... You should also qualify the path names to the .bak files properly. Your installer should be able to give you the actual paths. Regards, Greg Linwood SQL Server MVP Show quote "Jayyde" <a***@hotmail.com> wrote in message news:%23e2w7euwGHA.4300@TK2MSFTNGP05.phx.gbl... > Can I run a SQL command like: > > RESTORE DATABASE NewDB FROM DISK = > 'ApplicationStartupPath\SQLSE2005\DevDB.bak' WITH RECOVERY, MOVE > 'DevDB_Data' TO 'C:\\Program Files\\Microsoft SQL > Server\\MSSQL.1\\MSSQL\\Data\\NewEB_Data.mdf', MOVE 'DevDB_Log' TO > 'C:\\Program Files\\Microsoft SQL > Server\\MSSQL.1\\MSSQL\\Data\\NewDB_Log.ldf' > > while connected to the master db installed by default in SQLSE 2005? If > not > how the heck do I run this command to restore this db for the client when > they install the app? > > I'm trying to do this through VS 2003 so that would be another > question--2003 isn't going to have any problems doing commands for SQL > 2005 is it? Or am I just screwed here? > > TIA > Jayyde > |
|||||||||||||||||||||||