Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Connection string for .NET Windows forms


I'm working on an ASP.NET app but some parts of this application may be used for Windows forms later on too (objects, etc.). How can I specify the connection string to the SQL Server database in a single place so that if the SQL server database is moved to a different location, I only have to change the connection string in one place? That is, I wouldn't have to update the connection string for every single computer that uses the Windows forms app. I haven't worked with Windows forms before, so I'm not sure if this is even possible, but any help will be appreciated. Please let me know if you need more information or if this doesn't make sense at all.

  • 3
  • 3
1 Solution
You could create a web service that the Forms applications would consume to retrieve their connection string.

You could also store the connection string in an configuration file on the user machine that you can easily update without re-compiling your programs.

With the scenario you have suggested the application is intended for a network environment so it would be possible to place the connection string into a file that is on a network share and load the it from there.

The first thing to keep in mind, since this is a connection string, is to keep it secured so not just anyone can read it and get into your databases with other applications. I would advise using an encryption method to encrypt the Connection String in any of the above methods to help ensure that the string is kept safe from all but the most determined evil-doers.
cindyrodAuthor Commented:
I've never worked with Web Services. How could I write one to just return the connection string? How would I access it from the Windows forms?

Thank you!
Here is a tutorial on how to create a Web Service:

Here is how to link your windows application to a WebService:
In VB but works same in C#:
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

One bit of advice, make sure your enrypt the connection string.
Anyone will be able to view the soap message ;)
cindyrodAuthor Commented:
Thanks for the info. After talking to someone here, I won't be able to use Web Services because they may consume many resources on the server, especially since it would be used every time anyone wants to connect to the database. Would it be possible to have an XML file on the server and just use it for both web applications and windows forms? Would this affect performance?

You can place it on a server.

It will have to be in a place that all users and systems have access to it.

You will definately want to secure the file with encryption to protect your database credentials.

I would also make the file ReadOnly and when opening it with a FileStream to open it with FileMode.Open, FileAccess.Read, FileShare.Read.

If you go with this approach you can set the web applications to load the file from other servers. However, I would recommend setting those settings on the web application's server so that the web app is not dependent on another system and will still function as long as the database and web servers are running.

Performance will only be affected by the speed of your network. The only other factors to increase speed would be storing the file locally on each system or compiling the connection string into the application and as long as your network is fairly quick the end users will not notice much of a difference. If it ends up taking time to retrieve the settings you can give the appearance of speed by using a splash screen that says that it is setting up the connections while you are retrieving the data.
cindyrodAuthor Commented:
Thanks for the info. I don't have time to try it now but I'll let you know as soon as I get some time.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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