Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

How do I setup and compile a C# windows form to connect to a sql database

I know very little about C# but have managed to build a Windows Form Application and now I would like to deploy it to the clients. The application connection string points to my local server and database so what I need to know now is what do I need to compile and include with the project so that the user gets a prompt to  type in his or her connection string, aka "Server Name, Database, User, Password" and it will replace the connection string and allow the Form to work. Is this something that can be done with the project I have now or will I need to start over and build it first ? I don't know how to add the scripts to built the tables in the setup but I can send them to the local DBA and we can do that just before we install the application. Does anyone know of a link to a simple to understand how to doc or tutorial. Or even tell me what it is I need to lookup. If by chance you have or know of a sample project that does this and I can use as a demo FREE/ FEE either or no matter I just need a starting point. Thanks for you time !
0
GD_GRAY
Asked:
GD_GRAY
  • 2
1 Solution
 
Randy DownsOWNERCommented:
Try this but you need to pass the variables from your form.

http://stackoverflow.com/questions/7424091/how-to-connect-database-by-using-username-and-password-in-net-c-sharp

<connectionStrings>
     <add name="aConnectionString" connectionString="Data
 Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name;User Id=myUsername;Password=myPassword"
 providerName="System.Data.SqlClient"/>   
</connectionStrings>

Open in new window

0
 
GD_GRAYAuthor Commented:
Any way I could get anyone to elaborate on this ? I'm not sure if what I need to do here is build an input form and pass the variables from the input box to the form with the code above or what ? I would also like to add a twist, if at all possible the database connection need only be done once at setup and won't need to be ran each time a user opens the application. I am trying a few things with the  Microsoft.Data.ConnectionUI but so far it's all been way over my head.
0
 
Randy DownsOWNERCommented:
Maybe this approach would work for you

http://stackoverflow.com/questions/11028468/change-connection-string-after-creating-setup-file-in-c-net

Suppose you deploy your app.config with this connectionstring

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\yourFile.accdb;"
In a WinForms application the |DataDirectory| shortcut represent your application working folder, but you can change at runtime where it points to using this code.

// appdomain setup information
AppDomain currentDomain = AppDomain.CurrentDomain;
//Create or update a value pair for the appdomain
currentDomain.SetData("DataDirectory", "Your user choosen path");
It eliminates the need to hard-code the full path which, has you have discovered, leads to several problems to resolve during install. Of course your setup should deliver your database in your user choosen path.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now