how to specify connectionstring of sql server whil

2019-02-11 03:54发布

问题:

I am installing c# windows application in client place. In the application, I need a connection string. How can it be configured while installation. Also I want to pack the sql express setup and .Net Framework setup with my application setup. Is this posssible?

回答1:

If you're using the Visual Studio Installer, you can add a custom page to the installer and add a custom action that handles the result of that custom page. For example, in VS2010 Professional:

  1. Create a Visual Studio Installer setup project
  2. Add the project output to the installer, add shortcuts and otherwise configure the installer to your liking. (i.e. Assume for now that it won't configure the connection string for you and get everything else configured the way you want it. Now commit to your source control of choice so you can try again if you have problems)
  3. Right click on the setup project in Solution Explorer and choose View > User Interface
  4. Right click on "Start" (in the "User Interface" window that appeared as a result of step 3) and choose "Add Dialog"
  5. Choose one of "Textboxes (A)", "Textboxes (B)" or "Textboxes (C)" and click OK (as far as I'm aware it doesn't matter which of A/B/C you choose).
  6. Click on "Textboxes (A)" in the "User Interface" window and drag it up above "Confirm Installation", as you probably want the connection string dialog to appear before the "we're ready to install, are you sure" window.
  7. Right-click on "Textboxes (A)" and choose "Properties Window"
  8. Customise the properties (they'll appear in the "Properties" tab that sits underneath / next to Solution Explorer to:
    1. Set Edit2Visible, Edit3Visible and Edit4Visible to false
    2. Edit1Label to "Connection String:" (or wording of your choice)
    3. Edit1Property to "SQL_CONNECTION_STRING" (or a name of your choice - this is the name that a custom action will refer to)
  9. Build and run the installer to ensure it still installs correctly.

The next thing you'll need to do is write a custom action to store the connection string to the app.config file. The question Setup App.Config As Custom Action in Setup Project covers it so I'll leave it to you to read that and make use of it.

As far as I'm aware there's no way to embed dependencies inside a Visual Studio Installer, but I'd suggest hiving it off as another question as you really have two questions in your question =)



回答2:

Rob's answer was extremely helpful to get 2/3 of the way to what you need. I just spent some time finding my way the final 1/3rd of the way, so I hope this post helps you figure it out faster than I did:

This MSDN article helps explain how to create a Custom Action: https://msdn.microsoft.com/en-us/library/d9k65z2d(v=vs.100).aspx

To tie it all together, once you've created the Custom Action project as described in the MSDN article, you'll need to add it as an action for your deployment project:

  1. Right-click on your deployment project and select View -> Custom Actions

  2. Right-click on Install and Add Custom Action

  3. Double-click on Application Folder
  4. Click Add Output and select your Custom Action project output
  5. Right-click on Commit and repeat ("Add Custom Action", select your Custom Action project's output). Repeat for Rollback and Uninstall if needed.

To link the data entered in the install wizard to your code: For each Custom Action defined above (under the Install folder, Commit folder, etc.), select the Custom Action item. In the Properties window, Populate CustomActionData. For example, I specified some text box fields in my UI to have Edit1Property and Edit2Property values CONFIGURATION_SERVER and DATABASE_NAME. To make them available to my custom action code, my CustomActionData is set to: /cfgServer=[CONFIGURATION_SERVER] /dbName=[DATABASE_NAME]

Then, in my code, I can get the values by referring to: Context.Parameters["cfgServer"] or Context.Parameters["dbName"] (both return strings containing whatever the user entered).

My code runs during installation, and I wrote a method to modify the .CONFIG file that stores Application Settings to use the values specified by the user.



回答3:

Robs answer is right then you have to pass custom action data to the primary output of your custom action.This is the part of the installer.Now in the installer class override the BeforeInstall method of the installer class.Then get the location of the executing file by GetExecutingAssembly() method.After getting the executing assemble you need application.exe.config file read this file in by File.ReadAllText and change the connection string parameters using Replace method of string by but you have to add unique placeholders in your default connection string for replacing with this place holders.Then again write this file with File.WriteAllText method.This will successfully change your connection string while installation and your application will run with this.



回答4:

For Visual studio installer.

  1. Add Visual Studio Installer web set up project to your solution.
  2. When you right click on the project, then you can see option View there are options to select File System, UI interface, Custom Actions.
  3. Initially you have to select File System then right click on Web Application Folder then add select project output and then Primary Output(make sure to select project to deploy).
  4. Then you have to select Content Files just similar to how we selected form project output.
  5. Add UI Fields
  6. Right click on the custom action add new custom action, then pass parameters to it.

I have done small demo for it. This will be useful Visual studio installer for web