table of Contents
l Summary L Multiple installations LEAR DISTER Database in the installer 1. Execute the database scripted file by calling OSQL 2. Load the script file into the resource file 3. Create a database L summary
Summary After a project is completed, the production of the installer is a necessary process. If you need to deploy a database, make the production of the installation are more troublesome. Use Visual Studio.NET to make very easy to make applications and databases required in the deployment program. This article describes how to make an installer using Visual Studio.net, and how to deploy a database in the installer. This article assumes that you are familiar with ADO.NET.
1. General installation process
Visual Studio.net provides a powerful and very convenient installation program. The general installer can refer to the following steps: (1) Newly built a new installation item in Visual Studio.NET (for general Windows applications) or web installation items (for web-based applications), such as SETUPFACE. (2) On the SETUPFACE in the project inside the solution, click the following view: Figure 1 The item appearing in the view view in the installer is the item that needs to be involved in the production of the installer. a. The file system section provides how to pack the DLL, which need to be installed to the target, and the DLL needed to be installed to the installer, and also provides shortcuts and files on the desktop and program menus in the target machine. Clip, etc. b. The Registry section provides how to add related key values in the registry of the target. c. File Types provide a file type to create a file type with an application on the target, and can add relevant right-click menu features to registered file types. For example, register a .pdf file, use your own program to open. d. User interface provides interfaces during installation, or you can create functions such as readme files, registration code checks, etc. according to your needs. Some basic pictures have been provided when creating projects. e. Custom operation section mainly provides how to complete different work in different stages of installation. For example, create a database during the installation process, delete the database during the uninstall process. F. Starting Conditions section provides what conditions need to do before installing your application. Visual Studio.net provides a function of searching for start conditions on a user's computer. For example, a program is required to be installed, you can do it by searching the file system or search the registry. (3) Select the file system in the view, you will see the following screen: Figure 2 File System Interface If you need to complete a simple installer, select the file in the Add, select the application, Visual Studio.net The DLL you need to introduce the application (only for the application developed by Visual Studio.net, other programs are not tried), then create shortcuts in the user's "program" menu and user desktop, then Point shortcuts to choose to import applications imported in the application folder. (4) Finally, the project can be directly compiled and the installation procedure can be completed. If you need more control and modify the content during the installer, you can edit according to the different views mentioned in step (2). If you need to make a Web project installer, choose to create a web project when you create a project, and other parts are similar. You can also directly refer to MSDN as follows: http://msdn.microsoft.com/library/default.asp? Url = / library / en-us / vsintro7 / html / vxConatourofVisualstudio.asp
2. Deploy SQL Server Database in the installer
Through the above introduction, the production of general applications is very easy, basically the process of dragging your application into the installer. If your application runs requires support (such as SQL Server), the problem is more complicated. There are mainly aspects of the following aspects: (1) How to get the value of the installation screen, such as a username password entered. (2) How to design a program to interact with the database server, create a new database. How to get the value in the installer, please refer to the link below, here we don't do too much introduction.
From the above example we already know how to transfer values, then we only need to execute OSQL in blocks in the project file. Here is a question to find the script file of the database from where it is. We can place the script file into the file system, install directly to the user machine, and can get the location of the file by getting the following method. AskSEMBLY ASM = askAMBLY.GETEXECUTINGASSEMBLY (); string setuppath = asm.location; so we can know the position of the script file, the following file is how to start the OSQL program. We can complete the following code segments: process sqlprocess = new process (); sqlprocess.startinfo.FileName = "osql.exe"; sqlProcess.startinfo.arguments = string.format ("- u {0} -p {1 } -S {2} -i {3} ", this.uid, this.pwd, this.serverip, this.spath; // UID is the user name, the PWD is the password, Serverip is the IP of the target server, Spath is The path where the database script is sqlprocess.startinfo.windowstyle = processWindowStyle.hidden; sqlProcess.start (); sqlProcess.waitForexit (); // Waiting for SQLPRocess.close (); From above, this method must require the installer The client and the installed SQL Server can be used (OSQL is a program under the command line provided after SQL Server). In the actual test process, we found that if the user is selected to have a space for the installer , Such as; c: / program files / yourappliaction, installation failed. This is a relatively serious problem. 2.2 Load the script file as a resource file
From the previous discussion, we can see that it is more troublesome when using ADO.NET, requires a sentence to execute, if you use scripts, there is a "go" command in the script generated in the database, which will be executed in ADO.NET problem. Therefore, according to the analysis of the database installation script, we can use the following alternatives. During the installation of the database, there is no more than the following sections: (1) Creating a database (2) Creating a table (3) Creating a table (3) Creating a try or stored procedure and these three parts have the order, the order cannot be reversed, but each There is no order relationship during the process. During the test, we found that multiple tables or multiple stored procedures can be created in a statement. That is, we can perform the execution process in order to execute in the order. And we embed the database script as the resource file, as long as the execution is being executed, this will achieve the process of simplifying the creation of a command. Obtain a resource file code is as follows: (. "." Asm.GetManifestResourceStream (Asm.GetName () Name filename)) Assembly Asm = Assembly.GetExecutingAssembly ();; StreamReader str str = new StreamReader // filename you need to pick into the resource. As a resource file, simply import the file into your project, and change the generation operation to the embedded resource. Here we have a file stream of the file directly, so it can be read directly in the stream. We can complete the following: (1) Create a link to create a database. For example: Connectionstring = "Server = 127.0.0.1, uid = sa, pwd = pwd"; (2) Recreate a database link, the link Point to the created database. Connectionstring = "Server = 127.0.0.1, uid = sa, pwd = pwd, data = youdatabase"; (3) perform code for creating data tables and data stored procedures in new links. It should be noted that: Don't be named in your script, other commands can be executed directly. The advantage of this method is that there is no SQL Server on the target machine that can be installed, and there is no problem with space in the file path. Of course, if your database is Oracle or DB2, you can also use a similar approach to implement.
2.3 Creating a database via sp_attach_db
Through the discussion above, we have clear how to install the database. In the process of installing the database, in addition to creating a database through the database script, we can also attach the database through the system stored procedure sp_attach_db of SQL Server. Here we solve two questions: (1) Determine the location of the database file (.mdf and .ldf). (2) Execute the stored procedure.
For problem 1 we can learn the first method of mounting data, that is, install. Mdf and .ldf on the target machine through the file system, then pass Assembly ASMBLY.GETEXECUTISSEMBLY (); string setuppath = asm.location; get file path.
Finally, by calling sp_attach_db plus the corresponding parameters. It should be noted that this method can only be installed without installing the database.
to sum up
This article introduces three different ways to install the database at the installer, and also analyze the advantages and disadvantages of different methods, users can choose the installation method according to their actual needs. At the same time, by proposing this method, the user can also complete the configuration of the installer is the configuration of the database.