Reading Binary Data From Database

Storing binary data directly in the database is a fairly common practice. I recently found myself wondering how to read binary data from the database and write it to the browser.

For example, suppose you have a PDF file stored in the database. How do you send it to the browser? I ended up with this:


protected void Page_Load(object sender, EventArgs e)
{
 using (SqlConnection connection = new SqlConnection("Connection string goes here"))
 {
  connection.Open();

using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "dbo.GetPdf"; command.CommandType = CommandType.StoredProcedure;

command.Parameters.AddWithValue("@Id", 1);

using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read() == false) { return; }

<span class="kwd">int</span> contentLength = Convert.ToInt32(reader.GetBytes(0, 0, <span class="kwd">null</span>, 0, <span class="kwd">int</span>.MaxValue));

<span class="kwd">byte</span>[] buffer = <span class="kwd">new byte</span>[contentLength];

reader.GetBytes(0, 0, buffer, 0, contentLength);

Response.ContentType = <span class="str">"application/pdf"</span>;

Response.BinaryWrite(buffer);

} } } }

This works, but I wonder if it's the most efficient way to solve the problem? For example, we're calling the GetBytes method twice, which means that we're actually reading the whole document just to get its length. If the document is huge, this is wasteful. In that situation, I'm guessing having a fixed-sized buffer and reading the data in a while loop would be the way to go.

Free Template: Sixpence

Sixpence Template Screenshot

2-column liquid width layout template. Made for 1280x1024 screens and modern browsers but it should degrade well for smaller screens and older browsers. The template is a result of my obsession with dark colors. Personally, I find dark-templates harder to create than the lighter counterparts, which is rather sad because I find it so much easier ( on my eyes ) to read light text on dark backgrounds than dark text on light backgrounds.

View the template online

Download the files

How to List All Tables in a Database

EXEC sys.sp_tables
But this will list every single table, including all the system tables, which we rarely ever want. To list only tables created by the user, add a @table_owner parameter. For most databases, this will be "dbo":

EXEC sys.sp_tables NULL, 'dbo', NULL, NULL, NULL

Free Template: Lifeline

Lifeline Template Screenshot

I thought long and hard about whether to make this a commercial template, because it involved a bit more work than the rest of them, but in the end decided that I'm going to keep it free. :-)

Enjoy!

View the template

Download the template files

In Memorium

Pluto No Longer a Full-Fledged Planet

For Pluto.

:-(

The COALESCE Function

COALESCE is a cool little function whose name/purpose I keep forgetting/confusing with other cool functions.

Anyway: COALESCE returns the first non-null value in a list:

  • PRINT COALESCE(900, NULL, 10, NULL, 40)

The result: 900.

  • PRINT COALESCE(NULL, NULL, 10, NULL, 40)

The result: 10.

Note that at least one value must be non-null. For example:

  • PRINT COALESCE(NULL, NULL, NULL)

will result in an error — "At least one of the arguments to COALESCE must be a typed NULL."

Free Template: Fivestar

I was dusting my hard drive the other day, and I came across a template I created way back. I did a couple of quick changes, and here you have it. Enjoy!

Fivestar Template Screenshot

As always, you can do whatever you want with it. A mention and a link isn't necessary but is very much appreciated. :-)

View the template

Download the template files

Whew!

After a marathon coding session, I finally have the new design online. I'm rather pleased with the design at the moment, and, hopefully, I'll remain just as pleased with in the coming months. You might have noticed ( if you didn't, notice it now ) the urls. I managed to get rid of the index.php in an extremely hackish way, but then, I tend to be anal-retentive about these kind of things. Also, the urls don't include subdomains anymore. While it seemed like a good idea back when it started, it started grating on my nerves the last couple of days. Undoubtedly, this is going to cause broken links and what-not, but hopefully everything will be back to normal in a couple of weeks.

There are a couple of things left to do still — namely, the contact form and the search results page...

14 of 19 pages « First  <  12 13 14 15 16 >  Last »

On the Side