Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6891907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:28:06+00:00 2026-05-27T06:28:06+00:00

I have a stored procedure: CREATE PROCEDURE [dbo].[brbackup] @dataID [nvarchar](max), @backupdata [varbinary](max) OUTPUT WITH

  • 0

I have a stored procedure:

CREATE PROCEDURE [dbo].[brbackup]
    @dataID [nvarchar](max),
@backupdata [varbinary](max) OUTPUT
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SQLBackupRestore].[StoredProcedures].[BackupStuff]
GO

Which maps back to this CLR Stored procedure code:

public class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void BackupStuff(string dataID, out byte [] backupdata)
    {
       [..body omitted..]
    }
 }

This works just fine, I can call it in t-sql on the server just fine like this:

declare @backupdata varbinary(max);
exec brbackup "dataIDNumber", @backupdata output;

I get the expected output (several megabytes of data in @backupdata). What I’d like to do is call this from C# through a client, but this doesn’t work:

    static void Main(string[] args)
    {
        SqlConnection conn = new SqlConnection("myConnString");
        conn.Open();
        SqlCommand cmd = new SqlCommand("brbackup", conn);
        cmd.CommandTimeout = 0;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@dataID", "dataIDNumber");
        SqlParameter p = new SqlParameter("@backupdata", 
                                           SqlDbType.VarBinary, -1);
        p.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(p);
        cmd.ExecuteNonQuery();
    }

This query doesn’t give an error, and it runs for a while (as expected), but p.Value has nothing in it but a byte[0]. I’ve tried variations of setting the length to something other than -1, and setting p.Value to a byte[] large enough to hold the results, but no joy.

However, strangely enough creating a small t-sql stored procedure to wrap around the CLR stored procedure DOES work:

CREATE PROCEDURE AA_JUST_TESTING
@stuff varbinary(max) output
AS
BEGIN
declare @backupdata varbinary(max);
exec brbackup 'dataIDNumber', @backupdata output;
set @stuff = @backup;
END
GO

And then calling the wrapper AA_JUST_TESTING with this C# code:

        SqlConnection conn = new SqlConnection("myConnString");
        conn.Open();
        SqlCommand cmd = new SqlCommand("AA_JUST_TESTING", conn);
        cmd.CommandTimeout = 0;
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter p = new SqlParameter("@stuff",
                                          SqlDbType.VarBinary, -1);
        p.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(p);
        cmd.ExecuteNonQuery();

Works just peachy. p.Value winds up with a byte [] of just the right size filled with the correct data.

So… I don’t see the difference other than one calls a CLR stored procedure and the other calls a T-SQL stored procedure, even though they’re both using a varbinary(max) output to return the value. I’m looking for:

  • A plausible explanation, pointer to documentation, etc… that tells me why this works like it does.

  • Some kind of work-around so I don’t have to have the wrapper stored procedure, and can maybe just accomplish what I need calling the CLR SP directly from C#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:28:07+00:00Added an answer on May 27, 2026 at 6:28 am

    Strange. Try changing the C# code from :

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void BackupStuff(string dataID, out byte [] backup)
    {
       [..body omitted..]
    }
    

    To :

    using System.Data.SqlTypes;
    ...
    
        [Microsoft.SqlServer.Server.SqlProcedure]
        public static void BackupStuff(string dataID, out SqlBinary backup)
        {
           [..body omitted..]
           byte[] result = ... // get byte array to return
           backup = new SqlBinary(result);
        }
    

    Using SqlBinary as the return type of the C# routine works for me.

    EDIT :
    Well, using either SqlBinary or byte[] both work for me.
    Is your varbinary(max) parameter called “@backup” or “@backupdata” ? Looks like the sample code you show uses both.
    i.e. try changing :

      SqlParameter p = new SqlParameter("@backupdata", 
                                               SqlDbType.VarBinary, -1);
    

    to :

      SqlParameter p = new SqlParameter("@backup", 
                                               SqlDbType.VarBinary, -1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stored procedure like CREATE procedure [dbo].[spAddItem] ( @itemId nvarchar(50), @itemName nvarchar(500), @itemDescription
I have a stored procedure: CREATE PROCEDURE [dbo].[BrandDrugDetailsInsert] @BrandDrugDetailsID uniqueidentifier OUTPUT, @BrandDrugID uniqueidentifier AS
I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
I have sql stored procedure which pass and return some values: CREATE PROCEDURE [dbo].[GetMoney]
I have a stored procedure as follows: CREATE PROC [dbo].[Incidents] (@SiteName varchar(200)) AS SELECT
I have a stored procedure CREATE procedure [dbo].[get_unique_identifier] AS DECLARE @ret_val INT UPDATE seq
Probably an easy-to-answer question. I have this procedure: CREATE PROCEDURE [dbo].[AccountExists] @UserName nvarchar(16) AS
I have created a stored procedure similar to this simplified example: CREATE PROCEDURE dbo.sp_MyStoredProcedure
I have a stored Procedure called Active and the code is: CREATE PROCEDURE dbo.Active

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.