Solved

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

Posted on 2013-05-25
3
325 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
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 29

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

832 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