The technique and realization of vbnet installation Engineering

Source: Internet
Author: User
The technique has just listened to the talk of smart client deployment, which details what to do if you're in. NET installation project to run the custom DLL or EXE, in the future in the custom project how to get the installation project passed over the parameters. Here is a general idea of your own experience:
First, it is to make a kind of project. Then add an installation class file and add a SQL.TXT file as an embedded resource, the code for the class is as follows, followed by a description:
Dim Cn as SqlConnection
' Get SQL Script string
Private Function GetSQL (ByVal Name As String) as String
Try
' Fetching the assembly by reflection
Dim ASM as [Assembly] = [Assembly]. GetExecutingAssembly ()
' Read resource file data to set the resource file to an embedded resource at compile time
Dim Sm as Stream = Asm.getmanifestresourcestream (Asm.getname (). Name + "." + name)
Dim Rr as New StreamReader (Sm)
Return Rr.readtoend
Catch ex as Exception
MsgBox (Err.Description, msgboxstyle.okonly, "error Tip")
Throw EX
End Try
End Function
' Get the database backup file path
Private Function Getbakfilepath (ByVal datfilename As String) as String
Try
' Fetching the assembly by reflection
Dim ASM as [Assembly] = [Assembly]. GetExecutingAssembly ()
' Read resource file data to set the resource file to an embedded resource at compile time
Dim Sm as Stream = Asm.getmanifestresourcestream (Asm.getname (). Name + "." + Datfilename)
If file.exists ("C:\" & Datfilename) Then
File.delete ("C:\" & Datfilename)
End If
Dim Fs as New FileStream ("C:\" & Datfilename, FileMode.Create)
Dim Sw as New BinaryWriter (Fs)
Dim RecByte (sm.length-1) as Byte
Dim Strread as New BinaryReader (Sm)
Strread. Read (recbyte, 0, RecByte. Length)
Sw.write (recbyte, 0, RecByte. Length)
Sw.close ()
Fs.close ()
Return "C:\" & Datfilename
Catch ex as Exception
MsgBox (Err.Description, msgboxstyle.okonly, "error Tip")
Throw EX
End Try
End Function
Private Sub ExecuteSQL (ByVal dbname As String, ByVal SQLText as String, ByVal uid As String, ByVal pwd As String, ByVal se Rvername as String)
Dim cmd as New SqlCommand (SQLText, Cn)
If cn.state = connectionstate.closed Then cn.open ()
Try
Cn.changedatabase (dbname)
Cmd. ExecuteNonQuery ()
Catch ex as Exception
MsgBox ("Run SQL script error!", msgboxstyle.okonly, "hint")
Throw EX
End Try
Cn.close ()
End Sub
Protected Sub adddbtable (ByVal dbname As String, ByVal uid As String, ByVal pwd As String, ByVal ServerName as String)
' Determine if the database exists
Try
Cn = New SqlConnection ("Persist Security info=false; User id= "& uid &"; Initial catalog=master;data source= "& servername &";p assword= "& pwd &"; Max Pool size=75000 ")
Dim Dt as New DataTable ()
Cn.open ()
Dim cmd as New SqlDataAdapter ()
Cmd. SelectCommand = New SqlCommand ("exec sp_helpdb", Cn)
Cmd. Fill (Dt)
Dim I as Int16
For i = 0 to Dt.rows.count-1
If dbname.tolower = CType (Dt.rows (i). Item (0), String). ToLower Then
' Database exists
cmd = Nothing
Cn.close ()
Exit Sub
End If
Next
Catch ex as Exception
MsgBox (Err.Description & "Connection database error, please check the parameters entered correctly!", msgboxstyle.okonly, "Error Prompt")
Exit Sub ' If it already exists, jump out
End Try

Try
' ======== method: Run SQL script to create a database
' ExecuteSQL ("Master", "Create DataBase" & dbname, uid, PWD, servername)
' ExecuteSQL (dbname, GetSQL ("MySql.txt"), uid, PWD, servername)
' ======== Method II: Performing a Restore backup file
Dim cm as New adodb.command ()
Dim cn as New ADODB. Connection ()
Dim DBRs as New ADODB. Recordset ()
cn. Open ("Provider=SQLOLEDB.1; Persist Security Info=false; User id= "& uid &" Initial catalog=master;data source= "& servername &";p assword= "& pwd)
Cm. ActiveConnection = cn
' Determine if the data exists, delete it
Cm.commandtext = "Exec sp_helpdb"
DBRs = cm. Execute () ' Fetch all database names
If not DBRs. BOF and not DBRs. EOF Then
Do as not DBRs. Eof
If Trim (DBRs. Fields (0). Value) = Trim (dbname) Then ' If the data inventory
Cm.commandtext = "Drop Database" & Trim (dbname)
Cm. Execute ()
Exit do
End If
DBRs. MoveNext ()
Loop
End If
' DBRs. Close ()
Dim sfile as String = Getbakfilepath ("Installdb.dat")
Cm.commandtext = "RESTORE Database" & Trim (dbname) & "from disk=" "& Trim (sfile) &" "
Cm. Execute ()
cn. Close ()
If file.exists (sfile) Then
File.delete (sfile)
End If
Catch ex as Exception
MsgBox (Err.Description & "A", Msgboxstyle.okonly, "error Tip")
Throw EX
End Try
End Sub

' Overwrite installation method
Public Overrides Sub Install (ByVal statesaver as System.Collections.IDictionary)
Mybase.install (statesaver)
' Take the value of the parameter in the text box that the installer adds back to the user interface
Dim connstr as String = Me.Context.Parameters.Item ("dbname")
Dim Cs as String () = Split (ConnStr, "|")
Adddbtable (CS (0), CS (1), CS (2), CS (3))
End Sub

Then add an installation project that adds the engineering of the class above to the project master output. Add a text box to the user interface

Then enter the parameter names in the four properties of the text box ...

There are four parameters set here, and when the installation is running, there will be an interface for you to enter four parameters, in the custom operation of the installation project, the loaded incoming project master input selected.

and set the property customactiondata to/Parameter name =[][][][] ... To write the parameters set in the File box in this way, let's take a look at the code in the above class:
Dim connstr as String = Me.Context.Parameters.Item ("dbname")
The value of the parameter dbname is taken, and is the value that the user enters when the interface is installed.
The Install method is also overridden in the class. Automatically runs and executes user-defined program code. The code is the same as the vbnet.
The above code does two things, one is to get the SQL statement from the embedded text file and run it to create the database. The other is to use
SQL data backup DAT files as embedded resources, in the installation process, the user input parameters as a condition, connect the SQL Server and restore the database ...




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.