I have several applications running on a workstation all pointing to the same
sql server. I would like a way to save the connectionstring once on the
machine and have all my applications be able to read it. In the event of a
problem where we have to change servers, I would like to be able to update
the connection string in this one place and proceed.

I have examined shared assemblies, and I have thought of storing the
connection string in machine.config. I have been to successful with either
of these methods.

In VB6, is used a dll and it worked fine.

What is the preferred way of doing this.
--
Thanks
Arnie

Re: Best way to store a connection string machine wide. by Alvin

Alvin
Wed Jan 16 19:27:09 CST 2008

Put it in the database. you'll then need to write a wrapper function to
retrieve it, but that's easily done.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------



"Arniec" <Arniec@discussions.microsoft.com> wrote in message
news:BBADF8DA-828E-4CCF-B4E9-85EC4D4193AF@microsoft.com...
>I have several applications running on a workstation all pointing to the
>same
> sql server. I would like a way to save the connectionstring once on the
> machine and have all my applications be able to read it. In the event of a
> problem where we have to change servers, I would like to be able to update
> the connection string in this one place and proceed.
>
> I have examined shared assemblies, and I have thought of storing the
> connection string in machine.config. I have been to successful with
> either
> of these methods.
>
> In VB6, is used a dll and it worked fine.
>
> What is the preferred way of doing this.
> --
> Thanks
> Arnie


Re: Best way to store a connection string machine wide. by Chris

Chris
Wed Jan 16 20:04:20 CST 2008

Alvin Bruney [ASP.NET MVP] wrote:
> Put it in the database. you'll then need to write a wrapper function to
> retrieve it, but that's easily done.
>
Clearly I missed something.... storing the database connection string in
the database? .... So what about the connectionstring to that database?

The way I'd do it is to create a DLL assembly and use the My.Settings to
store it. Then expose it as a shared property in the DLL's class. I've
used this technique to share app-level data among multiple assemblies.

This is what mine looks like:

Public Shared Property ConnectionString() As String
Get
Return My.Settings.ConnectionString
End Get
Set(ByVal value As String)
My.Settings.ConnectionString = value
My.Settings.Save()
End Set
End Property


It seems to work for me pretty well. To prevent it from being
accidentally written over, make it an Application Level setting, then
expose the property as readonly.

-ca

Re: Best way to store a connection string machine wide. by Scott

Scott
Wed Jan 16 20:53:21 CST 2008

Call me crazy, but "machine.config" sounds like a pretty good place for
machine-level configuration settings.


"Arniec" <Arniec@discussions.microsoft.com> wrote in message
news:BBADF8DA-828E-4CCF-B4E9-85EC4D4193AF@microsoft.com...
>I have several applications running on a workstation all pointing to the
>same
> sql server. I would like a way to save the connectionstring once on the
> machine and have all my applications be able to read it. In the event of a
> problem where we have to change servers, I would like to be able to update
> the connection string in this one place and proceed.
>
> I have examined shared assemblies, and I have thought of storing the
> connection string in machine.config. I have been to successful with
> either
> of these methods.
>
> In VB6, is used a dll and it worked fine.
>
> What is the preferred way of doing this.
> --
> Thanks
> Arnie


Re: Best way to store a connection string machine wide. by Ravi

Ravi
Wed Jan 16 20:53:28 CST 2008

I found this article helpful:

Encrypting Configuration Information in ASP.NET 2.0 Applications
http://aspnet.4guysfromrolla.com/articles/021506-1.aspx

/ravi

----------------------------------------------------------------
"Arniec" <Arniec@discussions.microsoft.com> wrote in message
news:BBADF8DA-828E-4CCF-B4E9-85EC4D4193AF@microsoft.com...
>I have several applications running on a workstation all pointing to the
>same
> sql server. I would like a way to save the connectionstring once on the
> machine and have all my applications be able to read it. In the event of a
> problem where we have to change servers, I would like to be able to update
> the connection string in this one place and proceed.
>
> I have examined shared assemblies, and I have thought of storing the
> connection string in machine.config. I have been to successful with
> either
> of these methods.
>
> In VB6, is used a dll and it worked fine.
>
> What is the preferred way of doing this.
> --
> Thanks
> Arnie



Re: Best way to store a connection string machine wide. by Alvin

Alvin
Thu Jan 17 18:04:19 CST 2008

Good catch, ha ha, I must ease off the glue sniffing. Your approach works
best.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------



"Chris Anderson [MVP-VB]" <tg-nospam@tannagh-dawt-com> wrote in message
news:%23qenH1KWIHA.1184@TK2MSFTNGP04.phx.gbl...
> Alvin Bruney [ASP.NET MVP] wrote:
>> Put it in the database. you'll then need to write a wrapper function to
>> retrieve it, but that's easily done.
>>
> Clearly I missed something.... storing the database connection string in
> the database? .... So what about the connectionstring to that database?
>
> The way I'd do it is to create a DLL assembly and use the My.Settings to
> store it. Then expose it as a shared property in the DLL's class. I've
> used this technique to share app-level data among multiple assemblies.
>
> This is what mine looks like:
>
> Public Shared Property ConnectionString() As String
> Get
> Return My.Settings.ConnectionString
> End Get
> Set(ByVal value As String)
> My.Settings.ConnectionString = value
> My.Settings.Save()
> End Set
> End Property
>
>
> It seems to work for me pretty well. To prevent it from being accidentally
> written over, make it an Application Level setting, then expose the
> property as readonly.
>
> -ca


Re: Best way to store a connection string machine wide. by Arniec

Arniec
Tue Jan 22 19:53:04 CST 2008

OK, this is what I was looking for, but.....
a) what happens if I want to point everything to a new server. I try to
change the dll and move it to the GAC, and I end up with two copies. I try
to remove the existing one first and I am told I need to unistall all
programs (i.e., application requires this dll, you cannot uninstall)

How Do I handle this?
--
Arnie


"Chris Anderson [MVP-VB]" wrote:

> Alvin Bruney [ASP.NET MVP] wrote:
> > Put it in the database. you'll then need to write a wrapper function to
> > retrieve it, but that's easily done.
> >
> Clearly I missed something.... storing the database connection string in
> the database? .... So what about the connectionstring to that database?
>
> The way I'd do it is to create a DLL assembly and use the My.Settings to
> store it. Then expose it as a shared property in the DLL's class. I've
> used this technique to share app-level data among multiple assemblies.
>
> This is what mine looks like:
>
> Public Shared Property ConnectionString() As String
> Get
> Return My.Settings.ConnectionString
> End Get
> Set(ByVal value As String)
> My.Settings.ConnectionString = value
> My.Settings.Save()
> End Set
> End Property
>
>
> It seems to work for me pretty well. To prevent it from being
> accidentally written over, make it an Application Level setting, then
> expose the property as readonly.
>
> -ca
>

Re: Best way to store a connection string machine wide. by Arniec

Arniec
Tue Jan 22 19:53:23 CST 2008

I agree with you, but I cannot figure out how to read from the machine.config
file.
Any input?
--
Arnie


"Scott Roberts" wrote:

> Call me crazy, but "machine.config" sounds like a pretty good place for
> machine-level configuration settings.
>
>
> "Arniec" <Arniec@discussions.microsoft.com> wrote in message
> news:BBADF8DA-828E-4CCF-B4E9-85EC4D4193AF@microsoft.com...
> >I have several applications running on a workstation all pointing to the
> >same
> > sql server. I would like a way to save the connectionstring once on the
> > machine and have all my applications be able to read it. In the event of a
> > problem where we have to change servers, I would like to be able to update
> > the connection string in this one place and proceed.
> >
> > I have examined shared assemblies, and I have thought of storing the
> > connection string in machine.config. I have been to successful with
> > either
> > of these methods.
> >
> > In VB6, is used a dll and it worked fine.
> >
> > What is the preferred way of doing this.
> > --
> > Thanks
> > Arnie
>
>

Re: Best way to store a connection string machine wide. by Scott

Scott
Tue Jan 22 22:06:52 CST 2008

Same way as web.config. In fact, the ConfigurationManager will check *both*
places.

string cstr =
ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;



"Arniec" <Arniec@discussions.microsoft.com> wrote in message
news:0BE2FCBF-E898-4FDF-AD4D-83C911779E68@microsoft.com...
>I agree with you, but I cannot figure out how to read from the
>machine.config
> file.
> Any input?
> --
> Arnie
>
>
> "Scott Roberts" wrote:
>
>> Call me crazy, but "machine.config" sounds like a pretty good place for
>> machine-level configuration settings.
>>
>>
>> "Arniec" <Arniec@discussions.microsoft.com> wrote in message
>> news:BBADF8DA-828E-4CCF-B4E9-85EC4D4193AF@microsoft.com...
>> >I have several applications running on a workstation all pointing to the
>> >same
>> > sql server. I would like a way to save the connectionstring once on
>> > the
>> > machine and have all my applications be able to read it. In the event
>> > of a
>> > problem where we have to change servers, I would like to be able to
>> > update
>> > the connection string in this one place and proceed.
>> >
>> > I have examined shared assemblies, and I have thought of storing the
>> > connection string in machine.config. I have been to successful with
>> > either
>> > of these methods.
>> >
>> > In VB6, is used a dll and it worked fine.
>> >
>> > What is the preferred way of doing this.
>> > --
>> > Thanks
>> > Arnie
>>
>>


Re: Best way to store a connection string machine wide. by Arniec

Arniec
Wed Jan 23 12:40:01 CST 2008


--
Arnie


"Scott Roberts" wrote:

> Same way as web.config. In fact, the ConfigurationManager will check *both*
> places.
>
> string cstr =
> ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
>
>
>
> "Arniec" <Arniec@discussions.microsoft.com> wrote in message
> news:0BE2FCBF-E898-4FDF-AD4D-83C911779E68@microsoft.com...
> >I agree with you, but I cannot figure out how to read from the
> >machine.config
> > file.
> > Any input?
> > --
> > Arnie
> >
> >
> > "Scott Roberts" wrote:
> >
> >> Call me crazy, but "machine.config" sounds like a pretty good place for
> >> machine-level configuration settings.
> >>
> >>
> >> "Arniec" <Arniec@discussions.microsoft.com> wrote in message
> >> news:BBADF8DA-828E-4CCF-B4E9-85EC4D4193AF@microsoft.com...
> >> >I have several applications running on a workstation all pointing to the
> >> >same
> >> > sql server. I would like a way to save the connectionstring once on
> >> > the
> >> > machine and have all my applications be able to read it. In the event
> >> > of a
> >> > problem where we have to change servers, I would like to be able to
> >> > update
> >> > the connection string in this one place and proceed.
> >> >
> >> > I have examined shared assemblies, and I have thought of storing the
> >> > connection string in machine.config. I have been to successful with
> >> > either
> >> > of these methods.
> >> >
> >> > In VB6, is used a dll and it worked fine.
> >> >
> >> > What is the preferred way of doing this.
> >> > --
> >> > Thanks
> >> > Arnie
> >>
> >>
>
>

Re: Best way to store a connection string machine wide. by Arniec

Arniec
Wed Jan 23 12:42:07 CST 2008

I thank you all for your help. I actually broke down and spent an incident
with MS and as I like to find a pot of gold at the end of a rainbow when I
search these posts, the following is the solution we arrived at.


Option Explicit On



Imports System.Security.Cryptography

Imports System.Text

Imports System.IO



Public Class Form1



'variable to store the path of the connection string

Dim mStrConStringPath As String



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load



'set the path according to the setup of your applications or as
required

mStrConStringPath = "d:\ConString.xml"



'save the connection string

SaveConnectionSting("Data
Source=testServer;Database=testDb;uid=;pwd=1;")



'retrieve the connection string

MsgBox(GetConnectionSting)



End Sub



Private Sub SaveConnectionSting(ByVal strConString As String)

'Call this method to Save the Connection String



If System.IO.File.Exists(mStrConStringPath) = False Then

Dim xmlDoc As New System.Xml.XmlDocument

Dim xNode As System.Xml.XmlNode



xNode = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element,
"ConnectionString", "")

'key is hardcoded.

'but in actual application please have the user supply the key
in form of the password or by other means

xNode.InnerText = Encrypt(strConString,
"23894239rsdjkfnasidufh2384712341234")



xmlDoc.AppendChild(xNode)

xmlDoc.Save(mStrConStringPath)

Else

Dim xmlDoc As New System.Xml.XmlDocument

Dim xNode As System.Xml.XmlNode



xmlDoc.Load(mStrConStringPath)



xNode = xmlDoc.SelectSingleNode("ConnectionString")

'key is hardcoded.

'but in actual application please have the user supply the key
in form of the password or by other means

xNode.InnerText = Encrypt(strConString,
"23894239rsdjkfnasidufh2384712341234")

xmlDoc.Save(mStrConStringPath)

End If



End Sub



Private Function GetConnectionSting() As String

'Call This method to Get the Connection String



Dim xmlDoc As New System.Xml.XmlDocument



xmlDoc.Load(mStrConStringPath)



Dim xNode As System.Xml.XmlNode

xNode = xmlDoc.SelectSingleNode("ConnectionString")



'key is hardcoded.

'but in actual application please have the user supply the key in
form of the password or by other means

Return Decrypt(xNode.InnerText, "23894239rsdjkfnasidufh2384712341234")



End Function



Private Function Encrypt(ByVal strText As String, ByVal strKey As
String) As String

'encrypts a piece of text



Dim TripleDes As New TripleDESCryptoServiceProvider()

Dim MD5Crypto As New MD5CryptoServiceProvider()



TripleDes.Key =
MD5Crypto.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey))

TripleDes.Mode = CipherMode.ECB



Dim TripleDesEncrypt As ICryptoTransform = TripleDes.CreateEncryptor()

Dim MyASCIIEncoding = New ASCIIEncoding()

Dim bytBuffer() As Byte = ASCIIEncoding.ASCII.GetBytes(strText)



Return
Convert.ToBase64String(TripleDesEncrypt.TransformFinalBlock(bytBuffer, 0,
bytBuffer.Length))

End Function



Private Function Decrypt(ByVal strText As String, ByVal strKey As
String) As String

'decrypts a piece of text



Dim TripleDes As New TripleDESCryptoServiceProvider()

Dim MD5Crypto As New MD5CryptoServiceProvider()



TripleDes.Key =
MD5Crypto.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey))

TripleDes.Mode = CipherMode.ECB



Dim TripleDesDecryptor As ICryptoTransform =
TripleDes.CreateDecryptor()

Dim bytBuffer() As Byte = Convert.FromBase64String(strText)



Return
ASCIIEncoding.ASCII.GetString(TripleDesDecryptor.TransformFinalBlock(bytBuffer, 0, bytBuffer.Length))



End Function



End Class


--
Arnie


"Scott Roberts" wrote:

> Same way as web.config. In fact, the ConfigurationManager will check *both*
> places.
>
> string cstr =
> ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
>
>
>
> "Arniec" <Arniec@discussions.microsoft.com> wrote in message
> news:0BE2FCBF-E898-4FDF-AD4D-83C911779E68@microsoft.com...
> >I agree with you, but I cannot figure out how to read from the
> >machine.config
> > file.
> > Any input?
> > --
> > Arnie
> >
> >
> > "Scott Roberts" wrote:
> >
> >> Call me crazy, but "machine.config" sounds like a pretty good place for
> >> machine-level configuration settings.
> >>
> >>
> >> "Arniec" <Arniec@discussions.microsoft.com> wrote in message
> >> news:BBADF8DA-828E-4CCF-B4E9-85EC4D4193AF@microsoft.com...
> >> >I have several applications running on a workstation all pointing to the
> >> >same
> >> > sql server. I would like a way to save the connectionstring once on
> >> > the
> >> > machine and have all my applications be able to read it. In the event
> >> > of a
> >> > problem where we have to change servers, I would like to be able to
> >> > update
> >> > the connection string in this one place and proceed.
> >> >
> >> > I have examined shared assemblies, and I have thought of storing the
> >> > connection string in machine.config. I have been to successful with
> >> > either
> >> > of these methods.
> >> >
> >> > In VB6, is used a dll and it worked fine.
> >> >
> >> > What is the preferred way of doing this.
> >> > --
> >> > Thanks
> >> > Arnie
> >>
> >>
>
>

Re: Best way to store a connection string machine wide. by Scott

Scott
Wed Jan 23 13:13:12 CST 2008

"Arniec" <Arniec@discussions.microsoft.com> wrote in message
news:48D68855-8809-421C-91EC-AFBB2E40597E@microsoft.com...
>I thank you all for your help. I actually broke down and spent an incident
> with MS and as I like to find a pot of gold at the end of a rainbow when I
> search these posts, the following is the solution we arrived at.


Using ConfigurationManager sure seems easier, and you can encrypt part/all
of your web.config/machine.config (or not) and it continues to work the
same.

Regardless, I'm glad you found a solution that works for you.