Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2004

Tip: Looking for answers? Try searching our database.

StreamReader - Losing Lines from File.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Say - 07 Oct 2004 08:30 GMT
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
Morten Wennevik - 07 Oct 2004 09:04 GMT
Hi Paul,

A StreamReader uses UTF-8 by default.  Try specifying another encoding.

Dim reader As StreamReader = New StreamReader(strm, Encoding.ASCII)
Dim reader As StreamReader = New StreamReader(strm, Encoding.Default)

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Jon Skeet [C# MVP] - 07 Oct 2004 10:44 GMT
> I am using the following code to load a T-SQL Script File during the
> instalation of an application. The File for example
[quoted text clipped - 5 lines]
> listed, as well as the source code I am using to greate the streamReader
> etc.

Your problem is this line:

       If Len(Trim(strLine)) > 1 Then

That's going to be false for a line containing only a bracket, and so
you're effectively ignoring it.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Paul Say - 07 Oct 2004 13:29 GMT
Thanks that does make sense will give it a try

Paul

>> I am using the following code to load a T-SQL Script File during the
>> instalation of an application. The File for example
[quoted text clipped - 15 lines]
> That's going to be false for a line containing only a bracket, and so
> you're effectively ignoring it.
Cowboy (Gregory A. Beamer) - MVP - 07 Oct 2004 14:39 GMT
If Len(Trim(strLine)) > 1 Then

should be

If Len(Trim(strLine)) > 1 Then

or better (.NET version)

If (strLine.Trim().Length > 0) Then
(OR)
'Will never be less than 0, so just test for empty lines
If (strLine.Trim().Length <> 0) Then

'     (' after Trim() = 1, so you are not going to run that line. If you
change to 0, you are better off.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

> I am using the following code to load a T-SQL Script File during the
> instalation of an application. The File for example
[quoted text clipped - 91 lines]
>     End Try
> End Sub

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.