How to add my SQL Server 2008 R2 database to my Vi

2019-09-13 07:37发布

问题:

I want install my Windows application at my client location. It is a multiuser application. 10 systems with 1 server. So I install client setup on all the 10 systems and service setup on the server system.

How to secure my database in client server? I am using VS2010 and SQL Server 2008 R2. With out installing SQL Server Management Studio Express, how to install my database? And how to add my database in service setup? Can anybody show me an example for this type of application?

回答1:

windows installer does not have any out of the box support to deploy database with along with the application, You will have to write your own component which creates the database during the deployment and plug it in with the MSI. I have done the same in my project. follow the steps.

Creating database with Console App

  1. First forget about the Windows Installer.
  2. Create a console application.
  3. Add method which uses either SMO(SQL server Management Object) or simple Sql Command to execute script.
  4. Check my answer here which could read a .sql file and execute it on SQL server.
  5. What you have to do is keep a .sql script file which could create the database when executed.
  6. You have to pass that file path to the method that you have in the console app.

Executing console app during the installation

  1. Now you have a console app which could create a database when executed, Provided that right scrip file path is passed to the method.
  2. Add this console exe and the sql script file to your MSI package. So both of these will be placed under the application installation folder.
  3. Add a installer class to your project.
  4. within the install method simply execute the console exe file.

Code

public override void Install(IDictionary stateSaver)
{
   base.Install(stateSaver);

   Process process = new Process();
   ProcessStartInfo info = new ProcessStartInfo();
   process.StartInfo = info;

   info.FileName = Path.Combine(this.Context.Parameters["targetdir"], "ConsoleApp.exe");
   info.UseShellExecute = false;

   process.Start();

}