Original: Inno Setup database connection and creation
Description: The first half of the program in the Inno Setup instance inside, and back if the database backup and restore in the instance is not, in the Internet is not easy to find, I have to pay a good big strength a sentence to try out, please refer to the note when you turn to show respect for personal labor. Thank you!
[Code]
{---SQLDMO---} //About SQLDMO You can search the Internet.
Const
SQL Server name, this name in the installation of SQL will require a server name input, all open SQL Server on each machine to see the name is different, such as I, but here we can use a generic name, is (local), that the server is local. That's all you can do.
SQLServerName = ' (LOCAL) ';
SQLDMOGROWTH_MB = 0;
//This is an event that creates a button press, which is a "metabase" button established in the page in the Setup Wizard
Procedure Sqldmobuttononclick (Sender:tobject);
Var
///Use some of the variables
SQL Server, Database, DBFile, logfile:variant;
Idcolumn, NameColumn, table,obackup,orestore:variant;
db_path:string;
//This variable is used to hold key values that read the registry
resultstr:string;
Begin
{Create The main SQLDMO COM Automation object}
//Check if SQL is installed
Try
SQL Server: = Createoleobject (' SQLDMO. SQL Server ');
Except
RaiseException (' You have not installed SQL database yet. ') #13 #13 ' (Error ' + getexceptionmessage + ' occurred) ');
End
{Connect to the Microsoft SQL Server}
//Connect SQL
Sqlserver.loginsecure: = True;
Sqlserver.connect (SQLServerName);
MsgBox (' Connect to SQL Server ' + sqlservername + ' '. ', mbinformation, MB_OK);
{Setup a database}
//Create SQL database file
Database: = Createoleobject (' SQLDMO. Database ');
Database.name: = ' dbase ';//The name of the database to be created
DBFile: = Createoleobject (' SQLDMO. DBFile ');
Dbfile.name: = ' ISData1 ';
//Database records
Dbfile.physicalname: = ' c:/program files/microsoft sql Server/mssql/data/dbase_data.mdf ';
Dbfile.primaryfile: = True;
Dbfile.filegrowthtype: = SQLDMOGROWTH_MB;
Dbfile.filegrowth: = 1;
Database.FileGroups.Item (' PRIMARY '). Dbfiles.add (DBFile);
LogFile: = Createoleobject (' SQLDMO. LogFile ');
Logfile.name: = ' ISLog1 ';
//Database journaling
Logfile.physicalname: = ' c:/program files/microsoft sql Server/mssql/data/dbase_log.ldf ';
DATABASE.TRANSACTIONLOG.LOGFILES.ADD (LogFile);
{ADD the database}
//Add a database
SQLSERVER.DATABASES.ADD (Database);
MsgBox (' Added database ' + database.name + ' '. ', mbinformation, MB_OK);
//Data backup
Obackup: = Createoleobject (' SQLDMO. Backup ');//Create a Backup object
Obackup.database: = ' Louyu ';//database name to be backed up
Obackup.files: = ' F:/louyu3.bak ';//Backup to where
Obackup.backupsetname: = ' Louyu3.bak ';//Name of the backup
Obackup.backupsetdescription: = ' bakdescription ';
Obackup.initialize: = true;
Obackup.sqlbackup (SQL Server);//Perform SQL backup operations
//Data restore
Key notes:
The backup file of the database in a location in the package, to restore the database first to find the backup files, to find the backup file, before you know where the software installed, that is, the installation path. During the installation of the program, we established the relevant information in the registry, including the software installation path records, please refer to the previous written "Inno Setup registry Add and read".
Why do I use this method to restore a database, as if it were around a circle, in fact, I think this may not be a good way, but in this I do not know what other ways to get the data in the installation package (file) or the software to install the path. {app}, {PF} cannot be used directly in this way, so this stupid method is used.
If a friend has a good way, please let me know.
Obtain the path to the program installation from the registry, and locate the backup file for the restored database based on the path.
If Regquerystringvalue (HKLM, ' software/******* ', ' Server ', resultstr) then
Begin
ResultStr: = Removequotes (RESULTSTR);
MsgBox (RESULTSTR,MBINFORMATION,MB_OK);
Try
Orestore: = Createoleobject (' SQLDMO. Restore ');//Create a Restore object
Orestore.database: = ' dbase ';//Restored database name
DB_PATH:=RESULTSTR + '/database file/dbase.bak ';//path to backup files
MsgBox (DB_PATH,MBINFORMATION,MB_OK);//test to see if the path is right
Orestore.files: =db_path;//Specify Backup Files
Orestore.replacedatabase: = true;
Orestore.sqlrestore (SQL Server);//Perform a restore operation
MsgBox (' Database restore succeeded! ', MBINFORMATION,MB_OK);
Except
MsgBox (' Database restore failed!, please try again or consult the Help documentation for manual configuration! ', MBINFORMATION,MB_OK);
End
End
End
//Database creation and restore events have been written, but it will not be smart to know when to execute, so we have to tell it when to execute. The following is the installation of the program, on the end of the page set up a button, when the button is pressed, the database will execute the event response.
{---}
//Create a button
Procedure Createbutton (Aleft, Atop:integer; acaption:string; Anotifyevent:tnotifyevent);
Begin
With Tbutton.create (wizardform) DO begincreation of//button objects
Left: = Aleft;//button position and width height
Top: = ATop;
Width: = WizardForm.CancelButton.Width;
Height: = WizardForm.CancelButton.Height;
Caption: = acaption;name of the//button
OnClick: = anotifyevent;
//This is the creation of a button to specify a parent window, that is, which page to show it on the top
Parent: = Wizardform.finishedpage;//The end page of the form is specified here
End
End
//Initialization wizard window
Procedure Initializewizard ();
Var
Left, Top, Topinc:integer;
Begin
Left: = WizardForm.WelcomeLabel2.Left;
Topinc: = WizardForm.CancelButton.Height + 8;
Top: = WizardForm.WelcomeLabel2.Top + wizardform.welcomelabel2.height-4*topinc;
//Let the button created above show up and give it a response event
Createbutton (left, Top, ' & Config SQL database ', @SQLDMOButtonOnClick);
Top: = top + topinc;
End
Connection and creation of the Inno Setup database