PC Review


Reply
Thread Tools Rate Thread

Binary data stored in SQL Server: can't read from ASP.NET, *can* read from Access?

 
 
Doug
Guest
Posts: n/a
 
      3rd Nov 2005
Working on converting an Access front-end/SQL Server back-end to a .NET
front/SQL Server back-end. We are also redesiging the SQL Server
database.

We have many Word/Excel documents that have been stored as OLE Objects
in the old SQL Server database. We can move them over to the new
database okay. We can "read" the files okay when accessing them via
Access (design mode/open table or runtime via double-clicking on an
object frame directly bound to the appropriate column).

However, we cannot read the files from ASP.NET. If we upload it from
ASP.NET, we can read it fine. This leads us to believe that MS Access
is "adding" something to the file header, or performing some other
weird trick.

This is the code we're using (sorry for the formatting):

Dim clsCR As New CommonRoutines
Dim clsHTML As New HTMLWrite

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim cmdGetFile As New System.Data.SqlClient.SqlCommand

cmdGetFile.CommandText = "SELECT Datalength(FileData) As
FileSize,FileName,FileData,ContentType " & _
"FROM tblCMDocument " & _
"WHERE CMDocumentID = @CMDocumentID"
cmdGetFile.Connection = clsCR.conConnection
cmdGetFile.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@CMDocumentID",
System.Data.SqlDbType.Int, 4, "CMDocumentID"))
Dim dr As System.Data.SqlClient.SqlDataReader

cmdGetFile.Parameters("@CMDocumentID").Value =
Request("CMDocumentID").ToString

dr = cmdGetFile.ExecuteReader

If dr.Read Then
Response.ContentType = dr("ContentType").ToString
Response.OutputStream.Write(CType(dr("FileData"), Byte()),
0, CInt(dr("FileSize")))
Response.AddHeader("Content-Disposition",
"attachment;filename=" + dr("FileName").ToString())
Else
Response.Write("File Not Found.")
End If
End Sub

 
Reply With Quote
 
 
 
 
Bruce Barker
Guest
Posts: n/a
 
      3rd Nov 2005
you output is merged with what ever html/controls are on the page.

try:

Response.Clear()
Response.ContentType = dr("ContentType").ToString
Response.AddHeader("Content-Disposition","attachment;filename=" +
dr("FileName").ToString())
Response.OutputStream.Write(CType(dr("FileData"), Byte()),0,
CInt(dr("FileSize")))
Response.End()

add Response.Clear() at the tsrat and Response.End()

"Doug" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Working on converting an Access front-end/SQL Server back-end to a .NET
> front/SQL Server back-end. We are also redesiging the SQL Server
> database.
>
> We have many Word/Excel documents that have been stored as OLE Objects
> in the old SQL Server database. We can move them over to the new
> database okay. We can "read" the files okay when accessing them via
> Access (design mode/open table or runtime via double-clicking on an
> object frame directly bound to the appropriate column).
>
> However, we cannot read the files from ASP.NET. If we upload it from
> ASP.NET, we can read it fine. This leads us to believe that MS Access
> is "adding" something to the file header, or performing some other
> weird trick.
>
> This is the code we're using (sorry for the formatting):
>
> Dim clsCR As New CommonRoutines
> Dim clsHTML As New HTMLWrite
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> Dim cmdGetFile As New System.Data.SqlClient.SqlCommand
>
> cmdGetFile.CommandText = "SELECT Datalength(FileData) As
> FileSize,FileName,FileData,ContentType " & _
> "FROM tblCMDocument " & _
> "WHERE CMDocumentID = @CMDocumentID"
> cmdGetFile.Connection = clsCR.conConnection
> cmdGetFile.Parameters.Add(New
> System.Data.SqlClient.SqlParameter("@CMDocumentID",
> System.Data.SqlDbType.Int, 4, "CMDocumentID"))
> Dim dr As System.Data.SqlClient.SqlDataReader
>
> cmdGetFile.Parameters("@CMDocumentID").Value =
> Request("CMDocumentID").ToString
>
> dr = cmdGetFile.ExecuteReader
>
> If dr.Read Then
> Response.ContentType = dr("ContentType").ToString
> Response.OutputStream.Write(CType(dr("FileData"), Byte()),
> 0, CInt(dr("FileSize")))
> Response.AddHeader("Content-Disposition",
> "attachment;filename=" + dr("FileName").ToString())
> Else
> Response.Write("File Not Found.")
> End If
> End Sub
>



 
Reply With Quote
 
Doug
Guest
Posts: n/a
 
      4th Nov 2005
Thanks. Tried it. Did not work.

 
Reply With Quote
 
Bruce Barker
Guest
Posts: n/a
 
      4th Nov 2005
then you probably did not convert the ole object to binary data when you
moved them to sqlserver. when access stores ole data in a database, it uses
that component to serialize the data to a binary stream. this is not the
same format as a file. to read the data, you need to create an instance of
the component, load the stream, then have the component write to a file
format.

-- bruce (sqlwork.com)


"Doug" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks. Tried it. Did not work.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change binary data buffer read from a binary file to string format Anderson Microsoft VC .NET 1 21st Jul 2006 11:35 AM
How can i read binary data? =?Utf-8?B?YmFzdWxhc3o=?= Microsoft Dot NET Framework 2 3rd Apr 2006 12:30 PM
MS Query to read data from SQL server stored procedure =?Utf-8?B?U291cmlz?= Microsoft Excel Programming 0 11th May 2005 06:34 PM
how do i read/open a file stored in a long binary format in a rec. =?Utf-8?B?YWNjZXNzIHF1ZXJ5?= Microsoft Access VBA Modules 1 26th Feb 2005 08:00 AM
How to read/write binary data from/to SQL server? Kevin Dai Microsoft ADO .NET 3 30th Mar 2004 09:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:45 AM.