473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

custom action to modify a database with web project installer

Hi, I'm trying to use a custom action to modify a database (rather than
create one) using the VS.NET '03's help example called "Custom Action to
Create Database During Installation".

I've made two modifications to the sample in the document...both are in the
"Protected Sub AddDBTable" (towards the bottom).

I've changed

' Creates the database.
ExecuteSql("master", "CREATE DATABASE" + strDBName)

to

' Changes the database.
ExecuteSql("master", "USE " + strDBName)

The error I'm getting on installation is: "in exception handler: incorrect
syntax near 'USE'."

If there's an easier way to do this, please let me know. Basically, I'm
simply wanting to include a database change script that runs along with a
web project installer.

Here is all my code:

Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
<RunInstaller(True)> Public Class DBChangeCustomAction
Inherits System.Configuration.Install.Installer

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Installer overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation
id=""MyWorkstation"";packet size=4096;integrated security=SSPI;data
source=My" & _
"Server;persist security info=False;initial catalog=master"

End Sub

#End Region

Private Function GetSql(ByVal Name As String) As String
Try

' Gets the current assembly.
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()

' Resources are named using a fully qualified name.
Dim strm As Stream =
Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name)

' Reads the contents of the embedded file.
Dim reader As StreamReader = New StreamReader(strm)
Return reader.ReadToEnd()
Catch ex As Exception
MsgBox("In GetSQL: " & ex.Message)
Throw ex
End Try

End Function

Private Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As
String)
Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)

Command.Connection.Open()
Command.Connection.ChangeDatabase(DatabaseName)
Try
Command.ExecuteNonQuery()
Finally
' Finally, blocks are a great way to ensure that the connection
' is always closed.
Command.Connection.Close()
End Try
End Sub

Protected Sub AddDBTable(ByVal strDBName As String)
Try
' Creates the database.
ExecuteSql("master", "USE " + strDBName)

' Creates the tables.
ExecuteSql(strDBName, GetSql("sqlChange.txt"))

Catch ex As Exception
' Reports any errors and abort.
MsgBox("In exception handler: " & ex.Message)
Throw ex
End Try
End Sub

Public Overrides Sub Install(ByVal stateSaver As
System.Collections.IDictionary)
MyBase.Install(stateSaver)
AddDBTable(Me.Context.Parameters.Item("dbname"))
End Sub
End Class

Nov 20 '05 #1
3 2310
Cor
Hi DC,

Although I am one of the worst in SQL,

I use it as (rougly pasted and corrected watch typoes)
\\\
Dim Conn As String = "Server=(local); DataBase=; Integrated Security=SSPI"
Dim strSQL As String = "CREATE DATABASE MyDatabase"
Dim cmd As New SqlCommand(strSQL, Conn)
Conn.Open()
cmd.ExecuteNonQuery()
cmd.CommandText = _
"USE MyDatabase" & vbCrLf & _
"CREATE TABLE tbl..................
///

I hope this helps?

Cor

Nov 20 '05 #2
Cor,

Thanks for the response.

Actually, I'm able to have the package create a new database and then run a
script to create and/or alter objects. What I'm trying to do here is simple
skip the CREATE DATABASE statement, and instead simple go right to the
creation/alteration of database objects.

_____
DC G

"Cor" <no*@non.com> wrote in message
news:uE****************@TK2MSFTNGP12.phx.gbl...
Hi DC,

Although I am one of the worst in SQL,

I use it as (rougly pasted and corrected watch typoes)
\\\
Dim Conn As String = "Server=(local); DataBase=; Integrated Security=SSPI"
Dim strSQL As String = "CREATE DATABASE MyDatabase"
Dim cmd As New SqlCommand(strSQL, Conn)
Conn.Open()
cmd.ExecuteNonQuery()
cmd.CommandText = _
"USE MyDatabase" & vbCrLf & _
"CREATE TABLE tbl..................
///

I hope this helps?

Cor

Nov 20 '05 #3
Since you are not creating the database you should just be able to use it,

The Sub ExecuteSql contains these calls

Command.Connection.Open()
Command.Connection.ChangeDatabase(DatabaseName)

That should be all you need. You can get rid of the ExecuteSql("master",
"CREATE DATABASE/USE" + strDBName)

See of this works

Private Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As
String)
Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)

Command.Connection.Open()
Command.Connection.ChangeDatabase(DatabaseName)
Try
Command.ExecuteNonQuery()
Finally
' Finally, blocks are a great way to ensure that the connection
' is always closed.
Command.Connection.Close()
End Try
End Sub

Protected Sub AddDBTable(ByVal strDBName As String)
Try
' Creates the tables.
ExecuteSql(strDBName, GetSql("sqlChange.txt"))

Catch ex As Exception
' Reports any errors and abort.
MsgBox("In exception handler: " & ex.Message)
Throw ex
End Try
End Sub

Patrick Baker - Visual Basic/Deployment Quality Assurance Team
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Reply-To: "DC Gringo" <dc******@visiontechnology.net>
From: "DC Gringo" <dc******@visiontechnology.net>
Subject: custom action to modify a database with web project installer
Date: Mon, 29 Dec 2003 18:20:34 -0500
Lines: 142
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <u6**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: user98.chemonics.net 63.66.50.98
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GXS01.phx.gbl!TK2MSFTNGXA0
5.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gb lXref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:168236
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi, I'm trying to use a custom action to modify a database (rather than
create one) using the VS.NET '03's help example called "Custom Action to
Create Database During Installation".

I've made two modifications to the sample in the document...both are in the
"Protected Sub AddDBTable" (towards the bottom).

I've changed

' Creates the database.
ExecuteSql("master", "CREATE DATABASE" + strDBName)

to

' Changes the database.
ExecuteSql("master", "USE " + strDBName)

The error I'm getting on installation is: "in exception handler: incorrect
syntax near 'USE'."

If there's an easier way to do this, please let me know. Basically, I'm
simply wanting to include a database change script that runs along with a
web project installer.

Here is all my code:

Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
<RunInstaller(True)> Public Class DBChangeCustomAction
Inherits System.Configuration.Install.Installer

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Installer overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation
id=""MyWorkstation"";packet size=4096;integrated security=SSPI;data
source=My" & _
"Server;persist security info=False;initial catalog=master"

End Sub

#End Region

Private Function GetSql(ByVal Name As String) As String
Try

' Gets the current assembly.
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()

' Resources are named using a fully qualified name.
Dim strm As Stream =
Asm.GetManifestResourceStream(Asm.GetName().Nam e + "." + Name)

' Reads the contents of the embedded file.
Dim reader As StreamReader = New StreamReader(strm)
Return reader.ReadToEnd()
Catch ex As Exception
MsgBox("In GetSQL: " & ex.Message)
Throw ex
End Try

End Function

Private Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As
String)
Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)

Command.Connection.Open()
Command.Connection.ChangeDatabase(DatabaseName)
Try
Command.ExecuteNonQuery()
Finally
' Finally, blocks are a great way to ensure that the connection
' is always closed.
Command.Connection.Close()
End Try
End Sub

Protected Sub AddDBTable(ByVal strDBName As String)
Try
' Creates the database.
ExecuteSql("master", "USE " + strDBName)

' Creates the tables.
ExecuteSql(strDBName, GetSql("sqlChange.txt"))

Catch ex As Exception
' Reports any errors and abort.
MsgBox("In exception handler: " & ex.Message)
Throw ex
End Try
End Sub

Public Overrides Sub Install(ByVal stateSaver As
System.Collections.IDictionary)
MyBase.Install(stateSaver)
AddDBTable(Me.Context.Parameters.Item("dbname"))
End Sub
End Class


Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Craig | last post by:
I have added a 'Textboxes (A)' to my UI installer project along with a custom action to pass the value back to a class I've written to override the void Install function. As long as the text is...
6
by: | last post by:
Hi, I added dotnetfx.exe to a setup project as a custom action to be installed before the main app is installed. I set the Arguments property to /q:a /c:"install /l /q" for a silent install...
0
by: Ron Andersen | last post by:
I have C# custom actions and merge modules created in Visual Studio to install some components of the application I am working on. The merge modules are used with an Install Shield 9 project. ...
2
by: weichung | last post by:
I had build an installer project for my ASP.NET project. And found that several fields need to be configured, like the web.config files and etc, which requires user input. Also I need to attach my...
5
by: Simon Verona | last post by:
I have an installer project for my VB.net application which as a custom action runs a silent installation of a third party software product. This however, generates an error if the application is...
0
by: Kemin | last post by:
Recently I built an ASP.NET application Installation program for deployment. The ASP.NET app use a Sql Server db, I package it into the installation file too. The general step of building an...
1
by: Gene | last post by:
I would like to know if the following is even possible with the visual studio.net Setup and Deployment project. 1. During the deployment after creation of application directory the setup needs...
10
by: =?Utf-8?B?SmFjayBTbWl0aA==?= | last post by:
Hello, I have a solution that contains a C# application that is installed and runs as a service. The solution also contains several DLLs, some of which are C# assemblies and some are C++ DLLs. ...
3
by: KennethLundin | last post by:
Hi, i'm developing a solution in VS9/CSharp. Now I'm constructing a deployment project for my appliation (thick, database driven, winforms, standard, good old type of application). During...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.