Results 1 to 4 of 4

Thread: fixed width text document

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    2

    fixed width text document

    Is there a way to export data that is returned in a datareader to a fixed width text document? The data in the reader is from a stored procedure that is being ran.

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    yes, here is how to do it
    VB Code:
    1. ' Imports statements
    2. Imports System.IO
    3. Imports System.Text
    4. Imports System.Data
    5. Imports System.Data.SqlClient
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         ' Declarations section, I hope most of these are self explanatory.
    9.         Dim cn As New SqlConnection("packet size=4096;integrated security=SSPI;data source=(local);" & _
    10.                                     "persist security info=False;initial catalog=northwind")
    11.         Dim cmd As New SqlCommand("SELECT * FROM Customers", cn)
    12.         Dim dr As SqlDataReader
    13.         ' These are the known column widths for the table I am reading from.
    14.         ' The columns are all varchar data
    15.         Dim Widths() As Int32 = {0, 5, 40, 30, 30, 60, 15, 15, 10, 15, 24, 24}
    16.         Dim Start As Int32
    17.         Dim Indx As Int32
    18.         Dim size As Int64 = 0
    19.         Dim strBuff() As Byte
    20.         ' File to write results to
    21.         Dim fsFile As New StreamWriter("C:\Temp\Customers.txt")
    22.  
    23.         cn.Open()
    24.         ' Open our reader
    25.         dr = cmd.ExecuteReader
    26.         While dr.Read
    27.             ' need to reset the start value each pass
    28.             Start = 0
    29.             ' declare our stringbuilder with the length and maxlength values set to the sum of our column widths
    30.             ' plus room for carriagereturn and linefeed terminators
    31.             Dim sbRowData As New StringBuilder(270, 270)
    32.             ' loop through our fields in the datareader
    33.             For Indx = 0 To dr.FieldCount - 1
    34.                 ' set the starting point to insert each column in our stringbuilder
    35.                 Start += Widths(Indx)
    36.                 ' test for nulls, can't perform string functions on nulls
    37.                 If dr.IsDBNull(Indx) Then
    38.                     ' got a null, pad an empty string
    39.                     sbRowData.Insert(Start, "".PadRight(Widths(Indx + 1)))
    40.                 Else
    41.                     sbRowData.Insert(Start, dr.GetString(Indx).PadRight(Widths(Indx + 1)))
    42.                 End If
    43.             Next
    44.             ' add our line terminators
    45.             sbRowData.Insert(268, vbCrLf, 1)
    46.             ' write our data to a fixedwidth text file.
    47.             fsFile.Write(sbRowData.ToString)
    48.         End While
    49.         'cleanup
    50.         fsFile.Flush()
    51.         fsFile.Close()
    52.         dr.Close()
    53.         cn.Close()
    54.     End Sub

    This code works and produces a fixed width text file that contains all the rows in the Customers table of the Northwind database.
    Whadayamean it doesn't work....
    It works fine on my machine!

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    2
    Thanks..... Worked like a champ...

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477


    what does your name imply, strat (short for stratavarius)?
    Whadayamean it doesn't work....
    It works fine on my machine!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width