Solved

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

Posted on 2013-05-25
3
312 Views
Last Modified: 2013-09-16
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
Comment
Question by:GD_GRAY
  • 2
3 Comments
 
LVL 29

Expert Comment

by:Randy Downs
Comment Utility
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
 

Author Comment

by:GD_GRAY
Comment Utility
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
 
LVL 29

Accepted Solution

by:
Randy Downs earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now