Solved

Connection string for .NET Windows forms

Posted on 2006-11-27
8
246 Views
Last Modified: 2011-10-03
Hi,

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.

Thanks.
0
Comment
Question by:cindyrod
  • 3
  • 3
8 Comments
 
LVL 10

Expert Comment

by:tpwells
ID: 18018734
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.
0
 
LVL 1

Author Comment

by:cindyrod
ID: 18019850
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!
0
 
LVL 10

Expert Comment

by:tpwells
ID: 18020675
Here is a tutorial on how to create a Web Service:
VB.NET: http://abstractvb.com/code.asp?A=1004
C#: http://www.codeproject.com/cs/webservices/myservice.asp

Here is how to link your windows application to a WebService:
In VB but works same in C#:
http://www.codeguru.com/vb/vb_internet/webservices/article.php/c4813/
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 10

Expert Comment

by:Kinger247
ID: 18027780
One bit of advice, make sure your enrypt the connection string.
Anyone will be able to view the soap message ;)
0
 
LVL 1

Author Comment

by:cindyrod
ID: 18078578
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?

Thanks!!
0
 
LVL 10

Accepted Solution

by:
tpwells earned 500 total points
ID: 18084165
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.
0
 
LVL 1

Author Comment

by:cindyrod
ID: 18176427
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.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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