Solved

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

Posted on 2013-05-25
3
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:Randy Downs
ID: 39196628
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
ID: 39199103
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 30

Accepted Solution

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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

635 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