Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 13th, 2006, 08:51 PM
Authorized User
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default storing and retrieving pdf files in an sql databas

I'd be grateful for any pointers please

Old December 14th, 2006, 03:11 AM
Friend of Wrox
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts

At the time of ".pdf" creation, save the document to a folder in a virtual directory.

In the sql database, store the url by docID (Primary Key) in the sql database. Also keep in mind, documents are updated so be aware of versioning.

Hope this helps.
Old December 14th, 2006, 02:50 PM
Authorized User
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts

Probably the easiest way. I was trying this out.
Dim mf As HttpPostedFile = myfile.PostedFile
Dim strFile As String = myfile.Value
Dim nFileLen As Integer = mf.ContentLength
Dim b(nFileLen) As Byte

mf.InputStream.Read(b, 0, nFileLen)
Dim str As String = Encoding.Unicode.GetChars(b)
str = Replace(str, "'", "''")
'str = Replace(str, "''", "'")
'Dim b1() As Byte
'b1 = Encoding.Unicode.GetBytes(str)
'Response.ContentType = "application/pdf"


'save file to DB

Dim cn As New SqlClient.SqlConnection("server=(local);initial
catalog=atest;integrated security=SSPI;")


Dim cmd As New SqlClient.SqlCommand("insert into cust (name, info) values("
& _

"'" & strFile & "','" & str & "')", cn)

Dim cmd As New SqlCommand("select name,info from cust where nume='" &
cboFis.SelectedValue & "'", cn)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
Dim str As String
Dim b() As Byte
If dr.Read() Then
str = dr("info")
str = Replace(str, "''", "'")
b = Encoding.Unicode.GetBytes(str)
Response.ContentType = "application/pdf"
End If
Old December 14th, 2006, 03:09 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

If you want to store the binary data in an image field you have to use a paramertized query and some IO calls like this:

First get your byte stream:
Private Function getByteStream(ByVal sFile as String) as Byte()
    Dim bTmpFile() as Byte
    Dim iFileLength as Integer
    Dim fInfo as FileInfo
    Dim fs as FileStream

    fInfo = New FileInfo(Server.MapPath(sFile))
    iFileLength = fInfo.Length

    bTmpFile = New Byte(iFileLength) {}

    fs = New FileStream(Server.MapPath(sFile), FileMode.Open, FileAccess.Read)
    fs.Read(bTmpFile, 0, bTmpFile.Length)

    fs = Nothing

    return bTmpFile
End Function

Now that you have your byte stream setup your query:
Dim bFile() as Byte = getByteStream("./myfile.pdf")

sSqlCmd = "insert into TABLE(file, length)"
sSqlCmd += "VALUES(@byteStream, @fileLength)"

sqlConn = New SqlConnection
sqlCmd = New SqlCommand(sSqlCmd, sqlConn)
sqlCmd.Connection = sqlConn
sqlConn.ConnectionString = [Connection string]

sqlCmd.Connection = sqlConn
sqlCmd.Parameters.Add(New SqlParameter("@byteStream", bFile))
sqlCmd.Parameters.Add(New SqlParameter("@fileLength", bFile.Length))


You can delete the file off the physical disk when your done.

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature

Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing & retrieving swf file from sql server tarduk2004 ASP.NET 2.0 Basics 2 October 30th, 2007 02:31 AM
ANN: VistaDB 3.0 world's first managed SQL databas vistadb General .NET 0 August 6th, 2006 11:24 PM
Storing/Retrieving jpgs from MySQL flippyn5 PHP Databases 1 December 9th, 2005 08:15 AM
Storing E-Invoices (pdf-files) in SQL-Server Gert SQL Server 2000 1 September 17th, 2004 09:07 AM
Problem with downloading files from access databas iniro Classic ASP Databases 5 September 7th, 2004 03:53 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.