Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

820 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