Automatically install SQL Server database

xiaoxiao2021-03-06  60

During this time, the project of the hand is close to the end. It has been unless otherwise not paying attention to the problem, but it refers to the unique engineering installation package, and there is a database instance on lack of the operating environment. Installation and database data installation recovery. Here first, the experience of packaging the MSDE into the installer is shared, then let's talk nonsense.

When your project is basically completed, when you are ready to publish, making a user using a simple automatic installation package to become another problem after the encoding is completed, whether the engineering development is full, from the installation, the user should have a The initial experience.

Let's briefly introduce how to add Microsoft SQL Server 2000 DeskTop Engine (MSDE) to add Microsoft SQL Server 2000 DeskTop Engine (MSDE). MSM:

1. First you need to download the MSDE2000 SP3 installation package. You can download from the link below, url: http://www.microsoft.com/sql/downloads/2000/sp3.asp

2. Download, you can install or use zip to decompress the local hard drive, you will see: MSI, MSM, Setup These 3 directory and SETUP, where Dongdong in the MSM folder is the MSDE2000 you want to introduce this article. Merger modules.

3. Add a installation project in your project (assuming to MySetup1, the path is C: / MySetup1), add the project output, select the output file (Content File ", select the output file (CONTENT FILES) Two.

4. Add a merge module, select Browse, specify to your MSDE MSM folder, select all files under MSM and MSM / 1033 (Repl.MSM, REPL_RES.MSM, DMO_RES.MSM, and DMO. MSM can be not selected), open.

5. Set SearchPath in the properties of the installation project, specify the path where your MSM is located (you need to add two: Path / MSM and PATH / MSM / 1033).

6. Set other properties about the installer, we don't mention it, this is to compile your installation project. When compiling, you can see your packaging project MySetup.msi .

7. This is the project to make a decline. Here we need to modify the laying installation package so that it can automatically install an instance of MSDE after installed the program (assuming instance name: MyServer). Now we need to use a tool ORCA for MS, download address: http://support.microsoft.com/default.aspx? Scid = kb; EN-US; 255905 After installing ORCA, you can use this tool to our MySetup1 .msi makes it modified.

8. Use ORCA to open the installation package file MySetup.msi, find the Property Table, add row to fill in the protropETanceName, and fill in the instance name MyServer in the Value. See: http://support.microsoft.com/?i = 810826 and http://support.microsoft.com/default.aspx?scid=kb;n-us; 281983

9. All the properties of the password, I have no success, it is SQLSAPWD, this parameter is used with SqlSecurityMode, but I have not set success, the password is always empty, I don't know what is! 10. Select Installexecutesequence this table, find the setPropsqlmsdeInstalled this action, modify 206 is 102; find RemoveexistingProducts, modify 1525 to 6601. Save, exit!

At this time, run your installation package on the machine without the SQL environment. After the program is installed, the MSDE's Myserver instance is automatically installed, and the instance of SQL Server is automatically launched after the machine is restarted. ============================================================================================================================================================================================================= ===================== The previous one introduces how to pack the installer along with the desktop version of SQL Server, I still want it to: When publishing a program to a computer that already has a SQL environment, automatically use SQL's OSQL to restore the specified database to your SQL Server's Date.

First, in C: / Create a temporary directory, such as C: / Tempbd, copy osql.exe to the directory, copy your database backup (TRUCKDB) to the directory; create a restore.bat and restore.txt files in the directory. The content is as follows: 1. restore.bat file content: OSQL -e -s -i c: /tempdb/Restore.txt 2. Restore.txt file content: use master if exists (Select * from sysdevices where name = 'truckdb " ) EXEC SP_DROPDEVICE 'TRUCKDB' ELSE EXEC SP_ADDUMPDEVICE 'DISK', 'TRUCKDB', 'C: / Program Files / Microsoft SQL Server / MSSQL / DATA / TRUCKDB.MDF'

Restore Database Truckdb from Disk = 'C: / Tempdb / TruckDB' with Replace

Second, add an Installer Class in your project: Select Project Main Project, add Installer Class, the name is assumed to be installer1. Select the code page of Instollr1, add the following code:

Public Overrides Sub Install (Byval Stateaver As System.collections.idictionary) 'Holding Install Method

Dim file as system.IO.FILE

IF file.exists ("c: / program files / microsoft sql server / mssql / data / TRUCKDB_DATA.MDF") = true kilion

Mybase.install (StateSaver)

Dim CheckedDir as system.io.directory

IF CHECKEDDIR.EXISTS ("C: / Program Files / Microsoft SQL Server / MSSQL / DATA") = false kil

CheckedDir.createdIRectory ("C: / Program Files / Microsoft SQL Server / MSSQL / DATA")

END IF

DIM FULLPATH AS STRING

DIM asm as system.reflection.assembly = system.reflection.Assembly.GetexecutingAssembly ()

DIM STRCONFIGLOC AS STRING

Strconfigloc = asm.location

DIM STRTEMP AS STRING

Strtemp = Strconfigloc

'Extraction installation path

Strtemp = startemp.remove ("/"), Len (strTemp) - strTemp.lastIndexof ("/"))

'Copy Datebase to Computer.

IF creATDIR (strTemp) = false then

'Failure, reverse installation

Me.unInstall (StateSaver)

EXIT SUB

Else

END IF

IF installdb (strtemp) = false kil

'Failure, reverse installation

Me.unInstall (StateSaver)

EXIT SUB

Else

END IF

'Delete database temporary files

DeleteDir ("c: / tempdb")

Deletedir (strTemp "/ tempdb)

End Sub

Public overrides subnstall (Byval Stateaver as system.collections.idictionary)

'Execution reverse installation

'Using reflection extraction installation path

MyBase.unInstall (StateSaver)

DIM asm as system.reflection.assembly = system.reflection.Assembly.GetexecutingAssembly ()

DIM STRCONFIGLOC AS STRING

Strconfigloc = asm.location

DIM STRTEMP AS STRING

Strtemp = Strconfigloc

Strtemp = startemp.remove ("/"), Len (strTemp) - strTemp.lastIndexof ("/"))

'Delete database files and temporary files

Deletedir (strTemp "/ tempdb)

DeleteDir ("c: / tempdb")

End Sub

Private function deletedir (byval path as string) AS Boolean 'Delete the specified folder

DIM DIR AS System.io.directory

If Dir.exists (PATH) = True Ten Dir.Delete (Path, True)

END FUNCTION

Private function creatdir (byval path as string) as boolean

'Creating a specified folder

Dim files as system.IO.FILE

DIM DIRS AS System.io.directory

Try

If DIRS.EXISTS ("C: / Tempdb") = false the Dirs.createdIRectory ("C: / Tempdb")

'COPY CREAT DB FILES

CopyFile (Path "/ Tempdb", "C: / Tempdb")

Return True

Catch

Return False

END TRY

END FUNCTION

Private sub copyfile (byval sourceDirname as string, byval destDirname as string)

All files of 'Copy specified folders to the target folder (single layer).

DIM DIR AS System.io.directory

Dim file as system.IO.FILE

DIM Spath, Opath As String

DIM I as integer

For i = 0 to dir.getfiles (SOURCEDIRNAME) .LENGTH - 1

Spath = dir.getfiles (SOURCEDIRNAME) .GetValue (i) .tostring

Opath = microsoft.visualbasic.right (Spath, Len (Spath) - Len (SOURCEDIRNAME))

File.copy (Spath, DestDirname Opath, True)

NEXT

End Sub

Private function installdb (byval path as string) as boolean

'Install the database, call the automatic batch.

'Dim Checkeddir as System.io.directory

'If CheckedDir.exists ("C: / Program Files / Microsoft SQL Server / MSSQL / DATA") = false Then

'CheckedDir.createdIRectory ("C: / Program Files / Microsoft SQL Server / MSSQL / DATA")

'End if

Try

Shell ("C: /TEMPDB/Restore.bat", AppWinStyle.hide, True)

Catch

END TRY

END FUNCTION

Then, add a installation project in your project, name MySetup1, add the project output, select the output file (PRIMARY OUTPUT), the content file, and add a folder. To the Application Folder, the folder's name is Tempdb, add a file to the folder Tempdb: osql.exe, restore.bat, restore.txt, truckdb (Database File). Set your folder's Properties's AlwayScreate for True. Compile your setup project. At this time, the generated mounting package will automatically call the installer class to restore your TRUCKDB database after the program is installed.

Note that TRUCKDB should be backed up to "C: / Program Files / Microsoft SQL Server / MSSQL / DATA /" for easy recovery.

转载请注明原文地址:https://www.9cbs.com/read-118857.html

New Post(0)