P
Paul Say
I am using the following code to load a T-SQL Script File during the
instalation of an application. The File for example
CreateDatabaseObjects.Sql is as embedded resource within a installation
custom action. The Problem I am having is that I am unable to read lines
that contain only a single Bracket ')' or '(' these lines appear empty. I
this normal? Is there somthing I should so that these lines can be read. an
examply of what I am trying to read sample A and what gets read sample B is
listed, as well as the source code I am using to greate the streamReader
etc.
Paul
Sample A (What the file contains)
Insert Table A
(
UserName,
User Id,
) Values (
'Sad Programmer',
1,
)
Sample B (What I Get, Of cause when I try to ececute this against the
database it throws an error)
Insert Table A
UserName,
User Id,
) Values (
'Sad Programmer',
1,
Sample Code
Protected Sub ExecuteScript(ByVal strDBName As String, ByVal ScriptFile As
String)
Dim scriptString As StreamReader = GetSqlScript(ScriptFile)
Dim strCommand As String = String.Empty
Dim strLine As String = String.Empty
strLine = scriptString.ReadLine()
While Not strLine Is Nothing
If Trim(strLine) = "GO" Then
ExecuteSql(strDBName, strCommand)
strCommand = ""
Else
If Len(Trim(strLine)) > 1 Then
If Len(strCommand) > 1 Then
strCommand = strCommand & vbCrLf
End If
strCommand = strCommand & strLine
End If
End If
strLine = scriptString.ReadLine()
End While
scriptString.Close()
End Sub
Private Function GetSqlScript(ByVal Name As String) As StreamReader
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
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
instalation of an application. The File for example
CreateDatabaseObjects.Sql is as embedded resource within a installation
custom action. The Problem I am having is that I am unable to read lines
that contain only a single Bracket ')' or '(' these lines appear empty. I
this normal? Is there somthing I should so that these lines can be read. an
examply of what I am trying to read sample A and what gets read sample B is
listed, as well as the source code I am using to greate the streamReader
etc.
Paul
Sample A (What the file contains)
Insert Table A
(
UserName,
User Id,
) Values (
'Sad Programmer',
1,
)
Sample B (What I Get, Of cause when I try to ececute this against the
database it throws an error)
Insert Table A
UserName,
User Id,
) Values (
'Sad Programmer',
1,
Sample Code
Protected Sub ExecuteScript(ByVal strDBName As String, ByVal ScriptFile As
String)
Dim scriptString As StreamReader = GetSqlScript(ScriptFile)
Dim strCommand As String = String.Empty
Dim strLine As String = String.Empty
strLine = scriptString.ReadLine()
While Not strLine Is Nothing
If Trim(strLine) = "GO" Then
ExecuteSql(strDBName, strCommand)
strCommand = ""
Else
If Len(Trim(strLine)) > 1 Then
If Len(strCommand) > 1 Then
strCommand = strCommand & vbCrLf
End If
strCommand = strCommand & strLine
End If
End If
strLine = scriptString.ReadLine()
End While
scriptString.Close()
End Sub
Private Function GetSqlScript(ByVal Name As String) As StreamReader
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
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