Using psql options in npgsql

131 views Asked by At

I'm using npgsql in a small app used in the library I work for. Originally my app (vb.net) was using sendkeys.send and sendkeys.sendwait to type psql commands in cmd. This works but its slow and inefficient (ugly, buggy, etc.).

I typically use the \o and \H options in psql to send my query results to an output file formatted as HTML and display this in my app.

I'm wondering if npgsql can handle some of the psql options available... specifically the output file and the format of the output (to html).

psql page of the postgreSQL documentaiton: http://www.postgresql.org/docs/devel/static/app-psql.html

How I'm doing it now:

    SendKeys.Send("SET client_encoding = 'UTF8';" & "{ENTER}")
    SendKeys.Send("\o htmlout.html" & "{ENTER}")
    SendKeys.Send("\H" & "{ENTER}")
    SendKeys.Send("\T bgcolor=""#E4F1E4""" & "{ENTER}")**
    SendKeys.Send("SELECT * FROM public." & gdxfilebox.Text.Remove(5))
    SendKeys.Send("{ENTER}")
    SendKeys.Send("WHERE gid IN {(}" & gidbox.Text.Trim() & "{)};")
    SendKeys.SendWait("{ENTER}")
    SendKeys.SendWait("\q" & "{ENTER}")
    SendKeys.SendWait("exit" & "{ENTER}")

Thanks!

1

There are 1 answers

0
Shay Rojansky On BEST ANSWER

Npgsql is an simply a driver for interacting with the database, HTML formatting of results is completely out of its scope (unlike psql, which is a user-facing console app). Note that there wouldn't be any "universal" way to format the HTML in a way that would satisfy everyone.

However, it should be very easy to read the query from the database and format the results in HTML, simply use NpgsqlDataReader's API to read the columns from each result row and format HTML from there.