Question

In SQL Server management studio, you can output a queries result to a grid (the default setting) such that when you run the query, the query results show up directly underneath it. Alternatively, you can output the query results to a file, which generates an .RPT file wherever you like.

sql server results to file

The report file looks something like this (when opened in notepad);

report file

My question is, how do I create the same type of file (the same file format, spacing and all) in .NET (I am using VB.Net).

This is what I have thus far, using enterprise libraries, but it returns a dataset, from which I extract a dataview. I can convert the dataview to the text file, but that text file doesnt look anything like this report file, of which I don't even know how its formatting is decided.

`Dim dbCommandWrapper As DBCommandWrapper

            dbCommandWrapper = GlobalDatabase.objDatabase.GetStoredProcCommandWrapper("My_StoredProcedure_Report")

            If IsNothing(objGlobalDatabase.objTransaction) Then
                Return GlobalDatabase.objDatabase.ExecuteDataSet(dbCommandWrapper).Tables(0).DefaultView()
            Else
                Return GlobalDatabase.objDatabase.ExecuteDataSet(dbCommandWrapper, objGlobalDatabase.objTransaction).Tables(0).DefaultView()
            End If`
Was it helpful?

Solution

The Management result view is nothing more than a formatted text. You need to receive DataTable, then

dim columns as new Dictionary(of int, int) ' save col index / col width info
for i as integer = 0 to DataTable.Columns.count - 1
    columns.Add(i, 1)
next

dim data as new List(of List(of string))

' preload you values and calculate width
For rows as int = 0 to DataTable.Rows.Count-1

    for cols as integer = 0 to DataTable.Columns.Count - 1

        dim cl as new List(of string) ' Save your string values here
        if not isDbnull(DataTable.Rows(rows)(cols)) Then
            dim val as string = convert.toString()(DataTable.Rows(rows)(cols))
            if val.Length > columns(cols) Then 
                columns(cols) = val.Length 
            end if
            cl.Add(val)
        else
            cl.Add(string.empty)
        end if    
    next
    data.Add(cl)
next

Now, print your output

dim sb as new StringBuilder()

' Do the header
for i as integer = 0 to Datatable.Columns.count -1
    sb.Append(Datatable.Columns(i).ColumnName.PadRight(columns(i), "_"))
    sb.Append(" ") ' space between columns 
next
sb.Append(Environment.NewLine)

' Now Data
for each l as list(of list(of string)) in data
    for i as integer = 0 to l.count
        sb.Append(l(i).Padright(columns(i)))
        sb.Append(" ") ' space between columns 
    next
    sb.Append(Environment.NewLine)
next        

Now you have the sb object with the data. What you need now is to display it using some font in which each character takes up same width, for example Courier New

Warning: code typed from memory and nit checked but should represent what you're looking for

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top