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