Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created simple database connectivity using Visual C++ 2010 professional and SQL server 2008. I want to deploy the project to other machine. I am keeping database backup file in setup.But how should I restore it programmatically ?

At Present I am deploying in Computer where VC++2010 and SQL server 2008 is already installed.
Will anyone guide me on how should I programmatically restore SQL Database from VC++ project?
Posted
Updated 12-Jun-18 23:30pm
v4

If you can execute SQL commands, there's one named 'RESTORE DATABASE'.

Hope this helps,

Pablo.
 
Share this answer
 
Comments
adityarao31 30-Aug-12 8:05am    
How to execute that command from VC++ MFC is the main question ?
I found one command SQLExecute ,but unabble to use it
Pablo Aliskevicius 30-Aug-12 8:10am    
There are three options:
1. Use ADO.
2. Use OLE DB.
3. Use ODBC.

In each case, you create and open a connection object: to restore a database, you should connect to MASTER.
Then you build a string with the desired command.
At last, you execute the command.

Search MSDN for examples, there should be a few still lying around.

Hope this helps,
Pablo.
adityarao31 31-Aug-12 0:19am    
I want to use ODBC method .I have written following code in InitInstance Of my
Dialog based application ,but it does not work. Will you please tell me What Code should I write ?
BOOL CTestDBApp::InitInstance()
{
-----------~~~----------------
SQLHSTMT hs;
SQLPrepare(hs,(SQLWCHAR*)("RESTORE DATABASE Trial FROM DISK = 'D:\\Trial.bak' WITH FILE = 1"), SQL_NTS);
SQLExecute(hs);
----------~~~~---------
}
Pablo Aliskevicius 31-Aug-12 11:41am    
Read this (top to bottom):
http://www.easysoft.com/developer/languages/c/odbc_tutorial.html
Particularly pay attention to the connection stuff.
Best wishes,
Pablo.
adityarao31 1-Sep-12 2:05am    
Yah It seems to be very useful
have a look at this article

http://technico.qnownow.com/backup-and-restore-sql-server-database-programmatically-in-c/[^]

in Restore a Backup section.

Hope this helps :)

Jas
 
Share this answer
 
Comments
adityarao31 30-Aug-12 8:08am    
But it is C# friend ,VC++ MFC is unmanaged application in VC++ 2010 Professional.
Will you please give example for it.
I used the following as a query on master.mdf to restore the database which works well and tested in VC++.
qryRestore.Format("RESTORE DATABASE %s FROM DISK = '%%s.bak' WITH  MOVE '%s' TO '%s%s.mdf', MOVE '%s_log' TO '%s%s_log.ldf'", sDest, backupDir, sSrc, sSrc, dataDir, sDest, sSrc, dataDir, sDest);
						pConnection->Execute(_bstr_t(qryRestore), &vRecordsAffected, adExecuteNoRecords|adCmdText)

Here sDest= New DB Name
backupDir = Path for Backup directory
sSrc = Name of .bak file
dataDir = path for DATA folder of SQL.
 
Share this answer
 
v2
Comments
CHill60 13-Jun-18 7:15am    
Solution 1 posted over 5 years ago discussed the use of RESTORE DATABASE. Stick to answering new questions where the OP still needs help - and make sure you are not just repeating what someone else has already said

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900