Showing posts with label binary. Show all posts
Showing posts with label binary. Show all posts

Tuesday, March 27, 2012

BinToBit function

Anyone happen to have a function that does the following:

I have an integer for example 57.

This translates to binary: 111001

I'm looking for a function like this:

Code Snippet

CREATE FUNCTION [dbo].[BinToBit] (@.ValueCol int, @.Number TinyInt)

RETURNS bit AS

If I would call the function:

select dbo.BinToBit(57, 0) it should return 1

select dbo.BinToBit(57, 1) it should return 0

select dbo.BinToBit(57, 2) it should return 0

select dbo.BinToBit(57, 3) it should return 1

select dbo.BinToBit(57, 4) it should return 1

select dbo.BinToBit(57, 5) it should return 1

I've been looking on the net, because I'm convinced someone must have this kind of function, unfortunately haven't been able to find it.

CREATE FUNCTION [dbo].[BinToBit] (@.ValueCol int, @.Number TinyInt)

RETURNS bit AS

BEGIN

RETURN (@.ValueCol & POWER(2,@.Number))

END

|||Ha! Perfect thanks!

Sunday, March 25, 2012

Binary with XML into db using NS/filesystemwatcher?

Have a network share that will routinely get an xml file and a pdf dropped together. The xml file contains metadata about the pdf. Want to insert the various metadata elements in the xml file and the blob'd pdf as a varBinary into a table row as they are dropped.

Would like the Watcher to fire C# code to insert the xml elements and blob the pdf into the table (or can this be set up declaratively?)

Assuming not, best NS architecture to do this?

Thanks, Dan

Sorry, the SQL NS has absolutely nothing to do with this scenario.

Recommendation: use the FileSystemWatcher in a custom Windows Service.

binary varbinary question

What is the documented and known behavior of inserting hex constants into a
binary/varbinary column from a client to a server which have different code
pages? Will any code page / character set conversion take place?
eg: insert into t1 values ('AA') or insert into t1 values(x'AA')
Thanks
AakashYou can specify a binary (hex) constant as 0x followed by the character
representation of the hex string. These are not enclosed in quotes.
For example:

INSERT INTO t1 VALUES(0xAA)

This is documented in the SQL Server 2000 Books Online
<tsqlref.chm::/ts_ca-co_1n1v.htm>. Since the value is not a character
value, no character conversion occurs.

--
Hope this helps.

Dan Guzman
SQL Server MVP

--------
SQL FAQ links (courtesy Neil Pike):

http://www.ntfaq.com/Articles/Index...epartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
--------

"Aakash Bordia" <a_bordia@.hotmail.com> wrote in message
news:bf4j62$688$1@.hanover.torolab.ibm.com...
> What is the documented and known behavior of inserting hex constants
into a
> binary/varbinary column from a client to a server which have different
code
> pages? Will any code page / character set conversion take place?
> eg: insert into t1 values ('AA') or insert into t1 values(x'AA')
> Thanks
> Aakash

Binary Value Manipulation

I would like to drop the leading 0x on a binary value so I can do a
bitwise operation.
Here is simplified code:
select right(0x88186000,8)
I expected to get back 88186000, this was not the case. The command
returned some wierd characters.
Am I missing something?In this context 0x88186000 is a binary literal. There is no leading zero.
The "0x" is just part of the syntax for binary literals.
What bitwise operation are you trying to do? Lookup the bitwise operators in
Books Online. You probably won't get the answer you want by using string
operators such as RIGHT().
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||On 1 May 2007 12:01:27 -0700, tfeller wrote:
>I would like to drop the leading 0x on a binary value so I can do a
>bitwise operation.
(snip)
Hi tfeller,
I just replied to a copy of this message in comp.databases.ms-sqlserver.
In the future, please limit yoour questions to a single newsgroup - or
if you feel you really have to post to more than one group, use your
news program's crossposting function to post one copy to multiple groups
instead of posting independant copies. That prevents people from
spending time on a question that has already been answered in another
group.
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Binary Value Manipulation

I would like to drop the leading 0x on a binary value so I can do a
bitwise operation.

Here is simplified code:

select right(0x88186000,8)

I expected to get back 88186000, this was not the case. The command
returned some wierd characters.

Am I missing something?tfeller <ToddFeller@.gmail.comwrote in news:1178044017.983887.201130
@.o5g2000hsb.googlegroups.com:

Quote:

Originally Posted by

I would like to drop the leading 0x on a binary value so I can do a
bitwise operation.
>
Here is simplified code:
>
select right(0x88186000,8)
>
I expected to get back 88186000, this was not the case. The command
returned some wierd characters.
>
Am I missing something?
>


What do you get back from SELECT 0x88186000 ?

I don't expect it will be 0x88186000

Try SELECT CAST(0x41424344 as varchar) and you should get back ABCD

The _representation_ (note emphasis) of a binary value _in T-SQL_ is a
sequence of characters that begin with 0x - but the 0x does not form part
of the _actual binary value_, any more than the starting and ending quotes
form part of a character value.|||The expressions in the bitwise operations are treated as binary numbers, and
one of the expressions can be a binary data type. So, you do not have to
convert your binary value, just use it directly. The results is of data type
integer and you can convert it back to binary. Try this:

SELECT CAST(0x88186000 ^ 2 AS binary(4)),
CAST(0x88186000 | 2 AS binary(4)),
CAST(0x88186000 & 2 AS binary(4))

HTH,

Plamen Ratchev
http://www.SQLStudio.com|||On 1 May 2007 11:26:58 -0700, tfeller wrote:

Quote:

Originally Posted by

>I would like to drop the leading 0x on a binary value so I can do a
>bitwise operation.
>
>Here is simplified code:
>
>select right(0x88186000,8)
>
>I expected to get back 88186000, this was not the case. The command
>returned some wierd characters.
>
>Am I missing something?


Hi tfeller,

Maybe the functions Peter DeBetta describes will help you to achieve
what you need:
http://sqlblog.com/blogs/peter_debe...-varbinary.aspx
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelissql

Binary Value Manipulation

I would like to drop the leading 0x on a binary value so I can do a
bitwise operation.
Here is simplified code:
select right(0x88186000,8)
I expected to get back 88186000, this was not the case. The command
returned some wierd characters.
Am I missing something?
On 1 May 2007 12:01:27 -0700, tfeller wrote:

>I would like to drop the leading 0x on a binary value so I can do a
>bitwise operation.
(snip)
Hi tfeller,
I just replied to a copy of this message in comp.databases.ms-sqlserver.
In the future, please limit yoour questions to a single newsgroup - or
if you feel you really have to post to more than one group, use your
news program's crossposting function to post one copy to multiple groups
instead of posting independant copies. That prevents people from
spending time on a question that has already been answered in another
group.
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Binary Value Manipulation

I would like to drop the leading 0x on a binary value so I can do a
bitwise operation.
Here is simplified code:
select right(0x88186000,8)
I expected to get back 88186000, this was not the case. The command
returned some wierd characters.
Am I missing something?In this context 0x88186000 is a binary literal. There is no leading zero.
The "0x" is just part of the syntax for binary literals.
What bitwise operation are you trying to do? Lookup the bitwise operators in
Books Online. You probably won't get the answer you want by using string
operators such as RIGHT().
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||On 1 May 2007 12:01:27 -0700, tfeller wrote:

>I would like to drop the leading 0x on a binary value so I can do a
>bitwise operation.
(snip)
Hi tfeller,
I just replied to a copy of this message in comp.databases.ms-sqlserver.
In the future, please limit yoour questions to a single newsgroup - or
if you feel you really have to post to more than one group, use your
news program's crossposting function to post one copy to multiple groups
instead of posting independant copies. That prevents people from
spending time on a question that has already been answered in another
group.
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Binary Tree Problem in SQLServer

Hi Members
I m new in this forum, I have have Binary Tree database in SQL Server with
table fields(Mcode,Mname,LeftMCode,RightMCode). i want to display downline and pairs of each member.
can any one help me out?
Regards
Amirbkkread this.
is for OLAP, but, maybe help you.

http://www.codeproject.com/cs/database/tree_olap.asp|||From your column names, I'm guessing your are dealing with a Nested Set model ala' Joe Celko, rather than the more common Adjencency Model.
You should be able to find some code samples here:
http://www.intelligententerprise.com/001020/celko.jhtml?_requestid=1266295
...or by googling "Celko Nested Sets".|||Thanx for reply

But i have little prob that all nodes will be freely put on any side means any member can be put on left or right side and my db will grow very fast and estimated records will be more than 100000 with in few months

so if i use lineage and depth methods can i store path in one fields
and if use set model than i think , left node must be fill first and right node must be +1 than its left node.
and in my db any node(member) should be put on any side

so guys and suggestion will be higly appreciated.
i m going to build db with in few days , plz help me out.

Regards,

Amir|||Thanx for reply

But i have little prob that all nodes will be freely put on any side means any member can be put on left or right side and my db will grow very fast and estimated records will be more than 100000 with in few months

so if i use lineage and depth methods can i store path in one fields
and if use set model than i think , left node must be fill first and right node must be +1 than its left node.
and in my db any node(member) should be put on any side

so guys and suggestion will be higly appreciated.
i m going to build db with in few days , plz help me out.

Regards,

Amir

Binary to Varchar

I am trying to conver a binary column in the source table to a varchar as sh
own
select convert(varchar(32),0x00000000000003F3) it retunrns a null
hoe do i go about doing it?
Thankssatya wrote:
> I am trying to conver a binary column in the source table to a varchar as
shown
> select convert(varchar(32),0x00000000000003F3) it retunrns a null
> hoe do i go about doing it?
The following works for me:
select convert(varchar(32), convert(int, 0x00000000000003F3))

binary sort versus DOCI

Hello:
Whether you are using SQL 2000 or SQL 2005, what is the best script to run
in Query Analyzer to determine the sort order that SQL was originally
installed under (binary versus dictionary order case insensitivity)?
Thanks!
childofthe1980sOn 27 Oct, 17:41, childofthe1980s
<childofthe19...@.discussions.microsoft.com> wrote:
> Hello:
> Whether you are using SQL 2000 or SQL 2005, what is the best script to run
> in Query Analyzer to determine the sort order that SQL was originally
> installed under (binary versus dictionary order case insensitivity)?
> Thanks!
> childofthe1980s
SERVERPROPERTY('Collation')
returns the name of the server's default collation.
--
David Portas|||Thank you, David!
childofthe1980s
"David Portas" wrote:
> On 27 Oct, 17:41, childofthe1980s
> <childofthe19...@.discussions.microsoft.com> wrote:
> > Hello:
> >
> > Whether you are using SQL 2000 or SQL 2005, what is the best script to run
> > in Query Analyzer to determine the sort order that SQL was originally
> > installed under (binary versus dictionary order case insensitivity)?
> >
> > Thanks!
> >
> > childofthe1980s
>
> SERVERPROPERTY('Collation')
> returns the name of the server's default collation.
> --
> David Portas
>sql

Binary sort order and clustering

I am trying to create a four node cluster. One of my instances has a Binary
sort order. The SQL Server cluster install and SP4 worked. Now I am tryin
g
to add additional drives for SQL Server to use. I am able to do this for my
case insensitive instances, and those SQL Server instances can see the
drives.
On the Binary instance, once I add the drive and try to bring the instance
back online, SQL Server will not come back up. In cluster manager, the
status just says "Online Pending".
If I go to the services applet and try to start it, I get a message the the
service started and stopped. The instance will failover to another node.
The error log actually looks like SQL Server is still running, and I can see
sqlservr.exe in Task Manager. I can also connect to it using Query Analyzer
.
When I run this: "select * from ::fn_servershareddrives()", the correct
drive letters show up in the results. From Enterprise Manager, It looks lik
e
the instance is stopped. I can also navigate to the drive.
Has anyone successfully clustered a case sensitive instance? What could I
be doing wrong?Never mind. It was something besides adding the drive and nothing to do with
the collation.
"Kathi Kellenberger" wrote:

> I am trying to create a four node cluster. One of my instances has a Bina
ry
> sort order. The SQL Server cluster install and SP4 worked. Now I am try
ing
> to add additional drives for SQL Server to use. I am able to do this for
my
> case insensitive instances, and those SQL Server instances can see the
> drives.
> On the Binary instance, once I add the drive and try to bring the instance
> back online, SQL Server will not come back up. In cluster manager, the
> status just says "Online Pending".
> If I go to the services applet and try to start it, I get a message the th
e
> service started and stopped. The instance will failover to another node.
> The error log actually looks like SQL Server is still running, and I can s
ee
> sqlservr.exe in Task Manager. I can also connect to it using Query Analyz
er.
> When I run this: "select * from ::fn_servershareddrives()", the correct
> drive letters show up in the results. From Enterprise Manager, It looks l
ike
> the instance is stopped. I can also navigate to the drive.
> Has anyone successfully clustered a case sensitive instance? What could I
> be doing wrong?
>

Binary sort order and clustering

I am trying to create a four node cluster. One of my instances has a Binary
sort order. The SQL Server cluster install and SP4 worked. Now I am trying
to add additional drives for SQL Server to use. I am able to do this for my
case insensitive instances, and those SQL Server instances can see the
drives.
On the Binary instance, once I add the drive and try to bring the instance
back online, SQL Server will not come back up. In cluster manager, the
status just says "Online Pending".
If I go to the services applet and try to start it, I get a message the the
service started and stopped. The instance will failover to another node.
The error log actually looks like SQL Server is still running, and I can see
sqlservr.exe in Task Manager. I can also connect to it using Query Analyzer.
When I run this: "select * from ::fn_servershareddrives()", the correct
drive letters show up in the results. From Enterprise Manager, It looks like
the instance is stopped. I can also navigate to the drive.
Has anyone successfully clustered a case sensitive instance? What could I
be doing wrong?Never mind. It was something besides adding the drive and nothing to do with
the collation.
"Kathi Kellenberger" wrote:
> I am trying to create a four node cluster. One of my instances has a Binary
> sort order. The SQL Server cluster install and SP4 worked. Now I am trying
> to add additional drives for SQL Server to use. I am able to do this for my
> case insensitive instances, and those SQL Server instances can see the
> drives.
> On the Binary instance, once I add the drive and try to bring the instance
> back online, SQL Server will not come back up. In cluster manager, the
> status just says "Online Pending".
> If I go to the services applet and try to start it, I get a message the the
> service started and stopped. The instance will failover to another node.
> The error log actually looks like SQL Server is still running, and I can see
> sqlservr.exe in Task Manager. I can also connect to it using Query Analyzer.
> When I run this: "select * from ::fn_servershareddrives()", the correct
> drive letters show up in the results. From Enterprise Manager, It looks like
> the instance is stopped. I can also navigate to the drive.
> Has anyone successfully clustered a case sensitive instance? What could I
> be doing wrong?
>

Binary sort order

Binary sort order on SQL 7 is NOT the same as binary sort order in SQL 2000. SQL 2000 shows the databases brought over from the SQL 7 server generates a collation compatability error. I am seeing errors when running reports in Great Plains Dynamics.

Is there a work around to this?

Also why would Microsoft recommend DOCI over Binary sort order for their e-product offerings.The CP437_Binary sort order is exactly the same in SQL 2000 as it is in SQL 7.0.

The reason Microsoft recommends DOCI over binary sort order is because binary is not compatible with anything and makes everything difficult. We use it where I work because the old DBA believed it was still more efficient. What a pain!!!

You need to check your database to make sure it is using CP437. You also need to do a quick scan of syscolumns to make sure each column in there is CP437. If anyone creates a table or alter statement on their box and puts it on your binary box, it's very likely they scripted out the default collation which is not binary.

What are the exact errors you are receiving from the reports? I should be able to help you out quite a bit more if you can tell me the error. Unfortunately I have way more experience with binary on SQL 2000 then I ever wanted to have. I thought we were done with it after 6.5.|||Originally posted by derrickleggett
The CP437_Binary sort order is exactly the same in SQL 2000 as it is in SQL 7.0.

The reason Microsoft recommends DOCI over binary sort order is because binary is not compatible with anything and makes everything difficult. We use it where I work because the old DBA believed it was still more efficient. What a pain!!!

You need to check your database to make sure it is using CP437. You also need to do a quick scan of syscolumns to make sure each column in there is CP437. If anyone creates a table or alter statement on their box and puts it on your binary box, it's very likely they scripted out the default collation which is not binary.

What are the exact errors you are receiving from the reports? I should be able to help you out quite a bit more if you can tell me the error. Unfortunately I have way more experience with binary on SQL 2000 then I ever wanted to have. I thought we were done with it after 6.5.

Binary Serialization

Hello,

I am trying to serialize (binary) a class and save it in a database. I followed a few examples I found in internet and this is what I came up with:

1' Rows2<Serializable()> _3Public Class Rows4Implements ISerializable56Private _RowsAs New Generic.List(Of Row)7Public Property Rows()As Generic.List(Of Row)8Get9 Return _Rows10End Get11 Set(ByVal valueAs Generic.List(Of Row))12 _Rows = value13End Set14 End Property' Rows1516 ' New17Public Sub New()18End Sub' New1920 ' New21Public Sub New(ByVal siRowsAs SerializationInfo,ByVal scRowsAs StreamingContext)22End Sub' New2324 ' GetObjectData25Public Sub GetObjectData(ByVal siRowsAs SerializationInfo,ByVal scRowsAs StreamingContext)Implements ISerializable.GetObjectData2627 siRows.AddValue("Rows",Me.Rows)2829End Sub' GetObjectData3031Public Sub Serialize(ByVal filenameAs String)3233Dim sRowsAs Stream = Stream.Null34Try35 sRows = File.Open(filename, FileMode.Create, FileAccess.ReadWrite)36Dim bfRowsAs New BinaryFormatter37 bfRows.Serialize(sRows,Me)38Finally39 sRows.Close()40End Try4142 End Sub' Serialize4344Public Shared Function Deserialize(ByVal filenameAs String)As Rows4546Dim sRowsAs Stream = Stream.Null47Try48 sRows = File.Open(filename, FileMode.Open, FileAccess.Read)49Dim bfRowsAs New BinaryFormatter50Return CType(bfRows.Deserialize(sRows), Rows)51Finally52 sRows.Close()53End Try5455 End Function' Deserialize5657End Class' Rows

After serializing the class I need to save it in an a SQL 2005 database.
But all the examples I followed use the filename ...
Anyway, do I need to do something to save this into an SQL 2005 database or can I use it as follows? And how can I use this?
This is the first time I do something like this so I am a little bit confused.
Thanks,
Miguel 


well all u have to do is add "srows" which is the binary serialize stream to ur table, of course u must have a field in atable that is of type image.

Just include a simple insert as ur inserting regular Data to ur table, and here's the code to get the byte array to insert into the image field

byte

[] data;

srows.Read(data,0,

int.MaxValue);

like this just add data to ur image field in ur tabel.

and to read it back again, do the opposite

srows.Write(data, 0, data.Length); //ull have ur binary wrote back to the stream which is SRows

Hope this helps

Binary Problem

Hy friends!

I'm new in WebApplications that have SQL SERVER DB.

My problem is that...

In the DataBase I have a table with varbinary column and in the program I want save in this column a value of binary[] variable! How I can make it?

Another question... If I want select (by a query in the application) the value of the binary column, where I store it? because if I want select an integer value I take this in a int variable, and if I want select an string value from a DataBase I take this in a string variable..but How I can make it with binary column?

Thanks in advance!!

The following Scott Mitchell tutorial should help you

http://aspnet.4guysfromrolla.com/articles/081705-1.2.aspx

sql

Binary Primary Key

I have a 256 bit hash value to identify chunks of data. Would it be a good idea to create a 32-byte binary field as the primary key or encode the bytes to a string?

Well, to me if your natural key really is binary then that is the way I would designate the primary key -- as binary. I'd still like to hear other opinions, though

Thursday, March 22, 2012

Binary parameters to stored procedures?

I have a stored procedure that takes a byte string as an argument:
CREATE PROCEDURE Reporting_TicketSelectGroups
@.publicPart NVARCHAR(400),
@.checkField BINARY(46),
@.langCode VARCHAR(9)
AS
...
I've created a DataSet with the name of the stored proc as as its query
string and with this expression as its parameter value for @.checkField:
=Code.CheckField(Parameters!ticket.Value)
This in turn refers to a function in the Code tab of the Report
Properties property sheet:
Public Function CheckField(ByVal aTicket As String) As Byte()
...
Return Convert.FromBase64String(...)
End Function
The idea is that there is a parameter called ticket and it is split in
to two parts, one part being in binary, and these two parts are then
used as parameters to the various queries used in the report. When I
attempt to preview this report, I get this error message:
An error has occurred during report processing.
Query execution failed for data set 'Groups'.
Implict conversion from data type nvarchar to binary is not allowed.
Use the CONVERT function to run this query.
I'm assuming the last two sentences come from SQL Server and indicate
that my byte[] value is being converted to string on the way -- either
that or I have stuffed it up in some way. Can anyone tell me whether
this approach should work, or is simply not possible to pass binary
parameters from RS?
--
Damian CugleyI wrote:
> I have a stored procedure that takes a byte string as an argument:
> [...] Can anyone tell me whether
> this approach should work, or is simply not possible to pass binary
> parameters from RS?
I gather from the deafening silence that it is possible to pass neither
binary parameters nor other formats like UUIDs.
My workaround was straightforward enough, once I had decided to do it: I
wrote a base64 (RFC 1521) codec in T-SQL so I can pass the data safely
as a character string.

Binary or Byte data linked to Checkbox

Dear Group,
I'm using Access 2003 linked to an SQL Server data table via an ODBC
connection.
On one form I have several check boxes which want data mapped to a
TRUE/FALSE datatype. Is there a direct way to bind a checkbox to a
bit, a binary, or a tinyint? I've only been able to get it to work
with a smallint which is 2 bytes long.
Thanks,
Max
Arg. Access wants to insert -1 for true and 0 for false.
So sorry the type will need to support negative numbers.
Matt Neerincx [MSFT]
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
"Max Yaffe" <myaffe@.not.gamry.com> wrote in message
news:0f1ji1p22uleclnn5t4lka3vnta48bda3k@.4ax.com...
> Dear Group,
> I'm using Access 2003 linked to an SQL Server data table via an ODBC
> connection.
> On one form I have several check boxes which want data mapped to a
> TRUE/FALSE datatype. Is there a direct way to bind a checkbox to a
> bit, a binary, or a tinyint? I've only been able to get it to work
> with a smallint which is 2 bytes long.
> Thanks,
> Max

Binary or Byte data linked to Checkbox

Dear Group,
I'm using Access 2003 linked to an SQL Server data table via an ODBC
connection.
On one form I have several check boxes which want data mapped to a
TRUE/FALSE datatype. Is there a direct way to bind a checkbox to a
bit, a binary, or a tinyint? I've only been able to get it to work
with a smallint which is 2 bytes long.
Thanks,
MaxArg. Access wants to insert -1 for true and 0 for false.
So sorry the type will need to support negative numbers.
Matt Neerincx [MSFT]
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
"Max Yaffe" <myaffe@.not.gamry.com> wrote in message
news:0f1ji1p22uleclnn5t4lka3vnta48bda3k@.
4ax.com...
> Dear Group,
> I'm using Access 2003 linked to an SQL Server data table via an ODBC
> connection.
> On one form I have several check boxes which want data mapped to a
> TRUE/FALSE datatype. Is there a direct way to bind a checkbox to a
> bit, a binary, or a tinyint? I've only been able to get it to work
> with a smallint which is 2 bytes long.
> Thanks,
> Max

Binary files in DB : set name on retrieve

Hello,

I have a table that stores binary files. When I serve them up to the user, I call the following page (serveDocument.aspx?DocumentID=xxx) which holds only the following code and pass the document ID:

Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load
Dim DocumentIDAs Integer = Convert.ToInt32(Request.QueryString("DocumentID"))

'Connect to the database and bring back the image contents & MIME type for the specified picture Using myConnectionAs New SqlConnection(ConfigurationManager.ConnectionStrings("kelly_lmConnectionString1").ConnectionString)

Const SQLAs String ="SELECT [Content_Type], [Document_Data] FROM [lm_Service_Detail_Documents] WHERE [Document_id] = @.Document_id"Dim myCommandAs New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@.Document_id", DocumentID)

myConnection.Open()
Dim myReaderAs SqlDataReader = myCommand.ExecuteReader

If myReader.ReadThen Response.ContentType = myReader("Content_Type").ToString()
Response.BinaryWrite(myReader("Document_Data"))
End If myReader.Close() myConnection.Close()End Using
End Sub

It works perfectly. But heres the thing: I want users to be able to right click on the link and chose 'Save Target As'. In IE 7, this works fine. In FireFox it saves the document as 'serveDocument.aspx'. Is there any way toinjectthe filename that I want to save the document as?

Thank you.

Add this Header also

context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename+".txt" + "\"");

------------------

Mark as Answer if you feel

|||

What does 'context' refer to?

And, are you saying I shouldn't set the ContentType to the actual type of file?


|||

REmove the context

it uses to html encode when you are using http handler

|||

Well, that partially works. When a user clicks the linkthen saves the file, the correct name is present. But, my issue isn't that... I want the user to be able to right click the link and 'Save Link As...' with the correct filename. When I do that, it still says 'documentServe.aspx'.

|||

Hi,

But heres the thing: I want users to be able to right click on the link and chose 'Save Target As'. In IE 7, this works fine. In FireFox it saves the document as 'serveDocument.aspx'.

From your description, it seems that when you right click on the link and choose the 'save target as' in IE7, you can got the whole url with parameters, but not in FireFox, right?

If so, I'm afraid that the behavior is related to the Explorer, and based on my knowledge, there's not any workaround to fix the problem, since working mechanism for IE and FireFox is just not the same.

Thanks.

sql