Tuesday, March 13, 2007

Update Connection String after Deployment

Code in VB.NET:
To update the ConnectionString in "App.Config" file after deploying a windows application and code to restore database.


Imports System.Configuration
Imports System.Configuration.Install
Imports System.ComponentModel
Imports System.XML
Imports System.Data.SqlClient

Public Class UpdateConnectionString
Inherits Installer

Public Sub New()

End Sub


Public Overrides Sub install(ByVal stateSaver As System.Collections.IDictionary)

MyBase.Install(stateSaver)

Try

Dim ServerName As String = Context.Parameters("PathValue")
'This comes from Set up page (Custom Actions)

Dim ConnectionString As String

If ServerName <> "" Then

Dim AuthenticationMode, UID, PWD As String
AuthenticationMode = Context.Parameters("AuthVal")

If AuthenticationMode = "2" Then
UID = Context.Parameters("UID")
PWD = Context.Parameters("PWD")
End If


Dim AssemblyPath As String = Context.Parameters("assemblypath")
Dim appConfigPath As String = AssemblyPath + ".config"

Dim doc As XmlDocument = New XmlDocument
doc.Load(appConfigPath)

Dim configuration As XmlNode = Nothing
For Each Node As XmlNode In doc.ChildNodes
If Node.Name = "configuration" Then
configuration = Node
Exit For
End If
Next

If Not configuration Is Nothing Then
Dim settingNode As XmlNode = Nothing
For Each ASNode As XmlNode In configuration.ChildNodes
If ASNode.Name = "appSettings" Then
settingNode = ASNode
Exit For
End If
Next

If Not settingNode Is Nothing Then
Dim NumNode As XmlNode = Nothing
For Each NNode As XmlNode In settingNode.ChildNodes
If Not NNode.Attributes("key") Is Nothing Then
If NNode.Attributes("key").Value "ConnectionString" Then
NumNode = NNode
Exit For
End If
End If
Next

If Not NumNode Is Nothing Then
Dim CnAttr As XmlAttribute = NumNode.Attributes("value")

If AuthenticationMode = "2" Then
ConnectionString = "Data Source=" + ServerName + "\SQLEXPRESS;Initial Catalog=SCSAdv1;User ID=" + UID + ";pwd=" + PWD

Else

ConnectionString = "Data Source=" + ServerName + "\SQLEXPRESS;Initial Catalog=SCSAdv1;Integrated Security=True"
End If

CnAttr.Value = ConnectionString
doc.Save(appConfigPath)
End If
End If
End If

Dim cnToRestore As String

If AuthenticationMode = "2" Then
cnToRestore = "Data Source=" + ServerName + "\SQLEXPRESS;Initial Catalog=master;User ID=" + UID + ";pwd=" + PWD

Else

cnToRestore = "Data Source=" + ServerName + "\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"

End If

Dim Sqlcn As New SqlConnection(cnToRestore)
Dim sqlcmd As New SqlCommand()
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = "RESTORE DATABASE [SCSAdv1] FROM DISK = N'" + AssemblyPath.Substring(0, AssemblyPath.IndexOf("SCSAdv1.exe")) + "\SCSAdv1.bak' WITH FILE = 1, MOVE N'SCS_Data' TO N'" + AssemblyPath.Substring(0, AssemblyPath.IndexOf("SCSAdv1.exe")) + "\SCSAdv1.mdf', MOVE N'SCS_Log' TO N'" + AssemblyPath.Substring(0, AssemblyPath.IndexOf("SCSAdv1.exe")) + "\SCSAdv1_log.ldf', NOUNLOAD, STATS = 10"

sqlcmd.Connection = Sqlcn
Try
Sqlcn.Open()
Catch sqlex As SqlException
MsgBox("We are unable to locate the SQL Server instance you have specified." + vbCrLf + "Inorder to run the 'Simulator' you need to manually restore the database after the completion of setup.", MsgBoxStyle.Critical, "")
End Try
sqlcmd.ExecuteNonQuery()
End If

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "")
End Try
End Sub



Public Overrides Sub Uninstall(
ByVal stateSaver As System.Collections.IDictionary)

MyBase.Uninstall(stateSaver)
End Sub

End Class

10 comments:

Anonymous said...

Today, I went to the beach front with my kids. I found a sea
shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed.
There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is totally off topic but I had to
tell someone!
Feel free to visit my web blog : www.teenpornsexpussy.com

Anonymous said...

Wonderful beat ! I wish to apprentice while you amend your site,
how can i subscribe for a blog site? The account helped me a
acceptable deal. I had been a little bit acquainted of this
your broadcast provided bright clear idea
My site ... daily-wet-tshirt.com

Anonymous said...

Nice response in return of this query with solid arguments and describing all concerning that.
Also visit my site ... drunk teens

Anonymous said...

Heya i'm for the first time here. I came across this board and I find It really useful & it helped me out a lot. I hope to give something back and aid others like you helped me.
My webpage ... Fucking Her On all 4S

Anonymous said...

Hi there, I discovered your website via Google at the same
time as searching for a similar topic, your web site came
up, it seems good. I have bookmarked it in my google bookmarks.

Hello there, just was aware of your weblog via Google, and found that
it's really informative. I'm going to be careful for brussels.
I will appreciate when you continue this in future. Numerous
other folks will be benefited out of your writing. Cheers!
Here is my webpage ... free teen porn videos

Anonymous said...

Hello! This post could not be written any better!
Reading this post reminds me of my previous room mate!
He always kept talking about this. I will forward this write-up to him.

Pretty sure he will have a good read. Thanks
for sharing!
Stop by my blog free chatlines

Anonymous said...

You actually make it seem so easy with your presentation but I find this topic to be really something that
I think I would never understand. It seems too complex and very broad for me.
I am looking forward for your next post, I will try to get the hang of it!
Here is my blog - seo services india

Anonymous said...

Nice post. I was checking continuously this blog and I
am impressed! Very useful information specifically the last part :) I care for such information a lot.
I was seeking this particular information for a long time.
Thank you and good luck.

Here is my site domain names for sale

Anonymous said...

I tend not to leave many responses, but I looked through a few comments
on this page "Update Connection String after Deployment".
I do have some questions for you if you do not mind.
Is it only me or does it look as if like a few of the responses
look like they are left by brain dead visitors?
:-P And, if you are writing on additional online social sites, I'd like to follow you. Could you list of all of your social community pages like your twitter feed, Facebook page or linkedin profile?

Also visit my homepage; free voyeur sex

Anonymous said...

Hello to every one, as I am in fact keen of reading
this webpage's post to be updated regularly. It carries pleasant information.

My homepage - cfnm Fever party