Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 13th, 2006, 08:51 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
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

jake
  #2 (permalink)  
Old December 14th, 2006, 03:11 AM
Friend of Wrox
Points: 793, Level: 10
Points: 793, Level: 10 Points: 793, Level: 10 Points: 793, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2005
Location: OKC, OK, USA.
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

Jake:
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.
  #3 (permalink)  
Old December 14th, 2006, 02:50 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"

'Response.BinaryWrite(b1)
'Response.Flush()
'Response.Close()

'save file to DB

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

cn.Open()

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

"'" & strFile & "','" & str & "')", cn)
cmd.ExecuteNonQuery()

...........
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.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.BinaryWrite(b)
Response.Flush()
Response.Close()
End If
  #4 (permalink)  
Old December 14th, 2006, 03:09 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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.Close()
    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]
sqlConn.Open()

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

sqlCmd.ExecuteNonQuery()
sqlConn.Close()
sqlConn.Dispose()
sqlCmd.Dispose()

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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:35 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.