Best way to store a connection string machine wide.

A

Arniec

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.
 
A

Alvin Bruney [ASP.NET MVP]

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
 
C

Chris Anderson [MVP-VB]

Alvin said:
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
 
S

Scott Roberts

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

Alvin Bruney [ASP.NET MVP]

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
 
A

Arniec

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?
 
A

Arniec

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


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

Scott Roberts

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

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



Arniec said:
I agree with you, but I cannot figure out how to read from the
machine.config
file.
Any input?
 
A

Arniec

--
Arnie


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

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

Arniec

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 said:
Same way as web.config. In fact, the ConfigurationManager will check *both*
places.

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

Scott Roberts

Arniec said:
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.
 
S

shweta rai

All the time i have to change the connection string in my project whenever i change my PC. In my project there are man connection string.
so what should i do so that i don't have to change so plz reply regarding this problem as soon as possible



Arnie wrote:

I thank you all for your help.
23-Jan-08

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 O



Imports System.Security.Cryptograph

Imports System.Tex

Imports System.I



Public Class Form



'variable to store the path of the connection strin

Dim mStrConStringPath As Strin



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



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

mStrConStringPath = "d:\ConString.xml



'save the connection strin

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



'retrieve the connection strin

MsgBox(GetConnectionSting



End Su



Private Sub SaveConnectionSting(ByVal strConString As String

'Call this method to Save the Connection Strin



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

Dim xmlDoc As New System.Xml.XmlDocumen

Dim xNode As System.Xml.XmlNod



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 mean

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



xmlDoc.AppendChild(xNode

xmlDoc.Save(mStrConStringPath

Els

Dim xmlDoc As New System.Xml.XmlDocumen

Dim xNode As System.Xml.XmlNod



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 mean

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

xmlDoc.Save(mStrConStringPath

End I



End Su



Private Function GetConnectionSting() As Strin

'Call This method to Get the Connection Strin



Dim xmlDoc As New System.Xml.XmlDocumen



xmlDoc.Load(mStrConStringPath



Dim xNode As System.Xml.XmlNod

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 mean

Return Decrypt(xNode.InnerText, "23894239rsdjkfnasidufh2384712341234"



End Functio



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

'encrypts a piece of tex



Dim TripleDes As New TripleDESCryptoServiceProvider(

Dim MD5Crypto As New MD5CryptoServiceProvider(



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

TripleDes.Mode = CipherMode.EC



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 Functio



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

'decrypts a piece of tex



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


:

Previous Posts In This Thread:

Best way to store a connection string machine wide.
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

Put it in the database.
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
-------------------------------------------------------




Re: Best way to store a connection string machine wide.
Alvin Bruney [ASP.NET MVP] wrote:
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

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

Re: Best way to store a connection string machine wide.
I found this article helpful:

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

/ravi

----------------------------------------------------------------

Good catch, ha ha, I must ease off the glue sniffing. Your approach works best.
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
-------------------------------------------------------




OK, this is what I was looking for, but.....
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


:

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


:

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

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

Re: Best way to store a connection string machine wide.
--
Arnie


:

I thank you all for your help.
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


:

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


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.


Submitted via EggHeadCafe - Software Developer Portal of Choice
BizTalk Repeating Structures Table Looping and Table Extract
http://www.eggheadcafe.com/tutorial...0-a5704fe31a76/biztalk-repeating-structu.aspx
 
J

Jamal Samedov

Put it into settings

--

J.N. Samedov,

All the time i have to change the connection string in my project whenever
i change my PC. In my project there are man connection string.
so what should i do so that i don't have to change so plz reply regarding
this problem as soon as possible



Arnie wrote:

I thank you all for your help.
23-Jan-08

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


:

Previous Posts In This Thread:

Best way to store a connection string machine wide.
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

Put it in the database.
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
-------------------------------------------------------




Re: Best way to store a connection string machine wide.
Alvin Bruney [ASP.NET MVP] wrote:
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

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

Re: Best way to store a connection string machine wide.
I found this article helpful:

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

/ravi

----------------------------------------------------------------

Good catch, ha ha, I must ease off the glue sniffing. Your approach works
best.
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
-------------------------------------------------------




OK, this is what I was looking for, but.....
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


:

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


:

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

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

Re: Best way to store a connection string machine wide.
--
Arnie


:

I thank you all for your help.
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


:

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


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.


Submitted via EggHeadCafe - Software Developer Portal of Choice
BizTalk Repeating Structures Table Looping and Table Extract
http://www.eggheadcafe.com/tutorial...0-a5704fe31a76/biztalk-repeating-structu.aspx
 
S

sloan

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

You can refer to a SqlServer installation on your local PC as

localhost
or
..

or if you have an instance name

..\MyInstanceName
localhost\MyInstanceName


So if by "change my PC" you mean move from one development box to another,
then the above will help.

...........

If by "change my PC" you mean the database server PC, then a poor'mans
solution would be to keep the different environments in different files;


(app.config)
<?xml version = "1.0" encoding = "utf-8" ?>
<configuration>
<connectionStrings configSource="DeveloperExternalConnectionStrings.config"
/>
</configuration>


(DeveloperExternalConnectionStrings.config)
<connectionStrings>
<add name="MainDatabaseConnectionString"
connectionString="server=DEVSERVER;database=ZooropaDB"
providerName="System.Data.SqlClient"/>
</connectionStrings>

(QAExternalConnectionStrings.config)
<connectionStrings>
<add name="MainDatabaseConnectionString"
connectionString="server=QASERVER;database=ZooropaDB"
providerName="System.Data.SqlClient"/>
</connectionStrings>


And then flip flop this value in app.config:
So you would use
<connectionStrings configSource="DeveloperExternalConnectionStrings.config"
/>
**OR**
<connectionStrings configSource="QAExternalConnectionStrings.config" />


Note, you will have to create a POST-BUILD EVENT to copy
DeveloperExternalConnectionStrings.config
and
QAExternalConnectionStrings.config
They do not "auto copy" like app.config does.
to the output build directory...........or edit the "Copy to Output
Directory" property (in VS200_ or VS201_)

................



All the time i have to change the connection string in my project whenever
i change my PC. In my project there are man connection string.
so what should i do so that i don't have to change so plz reply regarding
this problem as soon as possible



Arnie wrote:

I thank you all for your help.
23-Jan-08

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


:

Previous Posts In This Thread:

Best way to store a connection string machine wide.
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

Put it in the database.
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
-------------------------------------------------------




Re: Best way to store a connection string machine wide.
Alvin Bruney [ASP.NET MVP] wrote:
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

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

Re: Best way to store a connection string machine wide.
I found this article helpful:

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

/ravi

----------------------------------------------------------------

Good catch, ha ha, I must ease off the glue sniffing. Your approach works
best.
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
-------------------------------------------------------




OK, this is what I was looking for, but.....
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


:

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


:

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

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

Re: Best way to store a connection string machine wide.
--
Arnie


:

I thank you all for your help.
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


:

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


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.


Submitted via EggHeadCafe - Software Developer Portal of Choice
BizTalk Repeating Structures Table Looping and Table Extract
http://www.eggheadcafe.com/tutorial...0-a5704fe31a76/biztalk-repeating-structu.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top