Discussion:
unlimited Varbinary parameter Size in Sql 2005
(too old to reply)
dvarma
2007-02-26 10:03:15 UTC
Permalink
Hi

I am executing a Sql Server SP from my C# app
One of the parameters is of type varbinary. Since the content length of this
parameter is variable I cannot set it to a pre-defined length. What do I set
the parameter size to ?
I can set it to -1 but does -1 means variable upto maximum size of Varbinary
type ?

Regards
Dilip
ML
2007-02-26 10:52:08 UTC
Permalink
In T-SQL that would be varbinary(max). On how to declare that in C# you
should ask in a newsgroup dedicated to C#.


ML

---
http://milambda.blogspot.com/
Stefan Delmarco
2007-02-26 21:58:13 UTC
Permalink
Hi Dilip,

You don't mention whether the varbinary(max) is an input or an output parameter. There are subtle differences:

Input Parameter:
drop procedure dbo.usp_VarBinaryIn
go
create procedure dbo.usp_VarBinaryIn
@binaryin_1 varbinary(max),
@binaryin_2 varbinary(max),
@binaryin_3 varbinary(max)
as
select datalength(@binaryin_1) + datalength(@binaryin_2) + datalength(@binaryin_3);
go

[Test]
public void VarBinaryIn()
{
using(SqlConnection conn = new SqlConnection("Server=tcp:chenbro,18980;Database=tempdb;Integrated Security=SSPI"))
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "dbo.usp_VarBinaryIn";
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();

byte[] data = Convert.FromBase64String("TWFuIGlzIGRpc3Rpbmd1aXNo");
// Let SqlParameter class figure out the length...
SqlParameter varBinaryParam1 = new SqlParameter("@binaryin_1", data);
cmd.Parameters.Add(varBinaryParam1);

// Or, be explicit...
SqlParameter varBinaryParam2 = new SqlParameter("@binaryin_2", SqlDbType.VarBinary, data.Length);
varBinaryParam2.Value = data;
cmd.Parameters.Add(varBinaryParam2);

// Or, we don't have to send the whole byte array...
SqlParameter varBinaryParam3 = new SqlParameter("@binaryin_3", SqlDbType.VarBinary, 10);
varBinaryParam3.Value = data;
cmd.Parameters.Add(varBinaryParam3);

long length = (long)cmd.ExecuteScalar();
Assert.AreEqual(46, length);
}
}


Output Parameter:
drop procedure dbo.usp_VarBinaryOut
go
create procedure dbo.usp_VarBinaryOut
@binaryout varbinary(max) output
as
set context_info 0x1256698456;
select @binaryout = substring(context_info(), 0, 5)
go

[Test]
public void VarBinaryOut()
{
using(SqlConnection conn = new SqlConnection("Server=tcp:chenbro,18980;Database=tempdb;Integrated Security=SSPI"))
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "dbo.usp_VarBinaryOut";
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();

// Don't need to be more explicit than this. Size -1 will return the entire byte array exactly...
SqlParameter varBinaryParam = new SqlParameter("@binaryout", SqlDbType.VarBinary, -1);
varBinaryParam.Direction = ParameterDirection.Output;
cmd.Parameters.Add(varBinaryParam);

cmd.ExecuteNonQuery();
byte[] contextInfo = (byte[])varBinaryParam.Value;
Assert.AreEqual("ElZphA==", Convert.ToBase64String(contextInfo));
}
}
--
Cheers,
Stefan Delmarco

http://www.fotia.co.uk
dvarma
2007-02-27 05:36:08 UTC
Permalink
Hi Stefan

Thanks for the reply.
Your reply does answer my original query.

It is an output parameter. Is there a way to find the actual size of the
varbinary
parameter content after query execution. .Size gives -1. So dont know what
is the content size each time.
Post by Stefan Delmarco
Hi Dilip,
drop procedure dbo.usp_VarBinaryIn
go
create procedure dbo.usp_VarBinaryIn
@binaryin_1 varbinary(max),
@binaryin_2 varbinary(max),
@binaryin_3 varbinary(max)
as
go
[Test]
public void VarBinaryIn()
{
using(SqlConnection conn = new SqlConnection("Server=tcp:chenbro,18980;Database=tempdb;Integrated Security=SSPI"))
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "dbo.usp_VarBinaryIn";
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
byte[] data = Convert.FromBase64String("TWFuIGlzIGRpc3Rpbmd1aXNo");
// Let SqlParameter class figure out the length...
cmd.Parameters.Add(varBinaryParam1);
// Or, be explicit...
varBinaryParam2.Value = data;
cmd.Parameters.Add(varBinaryParam2);
// Or, we don't have to send the whole byte array...
varBinaryParam3.Value = data;
cmd.Parameters.Add(varBinaryParam3);
long length = (long)cmd.ExecuteScalar();
Assert.AreEqual(46, length);
}
}
drop procedure dbo.usp_VarBinaryOut
go
create procedure dbo.usp_VarBinaryOut
@binaryout varbinary(max) output
as
set context_info 0x1256698456;
go
[Test]
public void VarBinaryOut()
{
using(SqlConnection conn = new SqlConnection("Server=tcp:chenbro,18980;Database=tempdb;Integrated Security=SSPI"))
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "dbo.usp_VarBinaryOut";
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
// Don't need to be more explicit than this. Size -1 will return the entire byte array exactly...
varBinaryParam.Direction = ParameterDirection.Output;
cmd.Parameters.Add(varBinaryParam);
cmd.ExecuteNonQuery();
byte[] contextInfo = (byte[])varBinaryParam.Value;
Assert.AreEqual("ElZphA==", Convert.ToBase64String(contextInfo));
}
}
--
Cheers,
Stefan Delmarco
http://www.fotia.co.uk
Stefan Delmarco
2007-02-27 07:11:37 UTC
Permalink
Hi Dilip,

In the sample I set the SqlParameter's Size to -1 to retrieve the entire byte array:

// Don't need to be more explicit than this. Size -1 will return the entire byte array exactly...
SqlParameter varBinaryParam = new SqlParameter("@binaryout", SqlDbType.VarBinary, -1);

Now, once the question has completed execution you can get the length of the varbinary by retrieving the length of the
byte[] that's returned as the SqlParameters's Value property:

byte[] varBinary = (byte[])varBinaryParam.Value;
Console.WriteLine("Varinary(Max) length is {0}", varBinary.Length);

Does that answer your question?
--
Cheers,
Stefan Delmarco

http://www.fotia.co.uk
Stefan Delmarco
2007-02-27 07:26:48 UTC
Permalink
Sorry, that should read: "Now, once the query has completed execution you can get the length of the varbinary by from
the byte[] that's returned as the SqlParameters's Value property:"
dvarma
2007-02-27 09:13:00 UTC
Permalink
Thanks Stefan...
Post by Stefan Delmarco
Hi Dilip,
// Don't need to be more explicit than this. Size -1 will return the entire byte array exactly...
Now, once the question has completed execution you can get the length of the varbinary by retrieving the length of the
byte[] varBinary = (byte[])varBinaryParam.Value;
Console.WriteLine("Varinary(Max) length is {0}", varBinary.Length);
Does that answer your question?
--
Cheers,
Stefan Delmarco
http://www.fotia.co.uk
Loading...