locked
Read and Write SQL text RRS feed

  • Question

  • User1510859543 posted

    I am writing code to open one .sql file that was output from SQL Server, replace some text and write out a new .sql file.  The process works but the output file is losing all the formatting that the input .sql file had. The code I am using is below.

        Private Sub WriteSQLFile()
            Dim file As String = "C:\ketofiles\03trgPatientsAudit.sql"
            Dim fileout As String = "C:\ketofiles\03trgPatientsAuditNew.sql"
            Dim strTextout As String = ""
    
            If System.IO.File.Exists(file) Then
                Dim lines As String() = System.IO.File.ReadAllLines(file)
    
                For Each ln As String In lines
                    strTextout &= Replace(ln, "[ketodata]", "[kdctest]")
                Next
                System.IO.File.WriteAllText(fileout, strTextout)
            End If
        End Sub

    Below is a sample of the top of the input and output files.

    USE [ketodata]
    GO
    /****** Object:  Trigger [dbo].[trgPatientsAudit]    Script Date: 10/21/2019 5:32:15 PM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    
    
    
    --new file output
    USE [kdctest]GO/****** Object:  Trigger [dbo].[trgPatientsAudit]    Script Date: 10/21/2019 5:32:15 PM ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- 

    How can I get the output text file to be exactly like the format of the input file (which was output from SSMS).

    Wednesday, October 7, 2020 7:19 PM

Answers

  • User475983607 posted

    Put the lines break back.

    strTextout &= Replace(ln, "[ketodata]", "[kdctest]") & vbCrLf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2020 7:39 PM

All replies

  • User475983607 posted

    Put the lines break back.

    strTextout &= Replace(ln, "[ketodata]", "[kdctest]") & vbCrLf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2020 7:39 PM
  • User1510859543 posted

    That was too simple!

    Wednesday, October 7, 2020 8:07 PM