sql-server – 如何使用过程保存byte []?

前端之家收集整理的这篇文章主要介绍了sql-server – 如何使用过程保存byte []?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个存储过程不保存数据,这似乎是VARBINARY的一个问题.我传递一个byte [],但它不起作用.如果我将此参数发送为NULL,则可以正常工作.

我用以下代码调用该过程:

public Community AddCommunity(string name,string description,byte[] picture,User owner,int? venue,int communityID) 
{
    using (var database = new Database())
    {
        return database.Scope.GetsqlQuery<Community>("QP_AddCommunity ?,?,?","VARCHAR Name,VARCHAR Description,VARBINARY Picture,INTEGER Owner,INTEGER Venue,INTEGER ID").GetResult(name,description,picture,owner.ID,venue,communityID);
    }
}

程序如下:

CREATE PROCEDURE [dbo].[QP_AddCommunity]
    @Name VARCHAR(120),@Description VARCHAR(MAX),@Picture VARBINARY(MAX),@Owner INTEGER,@Venue INTEGER,@ID INTEGER

AS
BEGIN
    SET NOCOUNT ON;

    IF(SELECT COUNT(*) FROM QT_Community WHERE ID = @ID) = 0
        INSERT INTO QT_Community(Name,[Description],Picture,[Owner],Venue) VALUES(@Name,@Description,@Picture,@Owner,@Venue);

    ELSE
        UPDATE QT_Community SET Name = @Name,[Description] = @Description,Picture = @Picture,[Owner] = @Owner,Venue = @Venue WHERE ID = @ID;

    SELECT * FROM QT_Community WHERE ID = @@IDENTITY;


END

这段代码出了什么问题? VARBINARY不是一个字节[]?

代码sql Server Management Studio上执行时有效.

DECLARE @X varbinary(20)
Set @X = CAST('Testing' As varbinary(20))
EXECUTE [QP_AddCommunity] 'aaaaa','descricao',@X,216,NULL,0;

但是当使用getsqlQuery方法调用byte []上的某些东西时,事务表明它不是活动的而且不是脏的.但是如果byte []为null,它就可以正常工作.

解决方法

我发现这是不可能的,如 this answer所示

Hello gaurav,currently our GetsqlQuery method cannot operate properly with parameters of type LongVarBinary or VarBinary,thus making it impossible for the stored procedure to work as expected. We are aware of this problem and we are working on fixing it. As a work around you should try and use Linq to achieve your goal. Greetings,Petar the Telerik team

原文链接:https://www.f2er.com/mssql/78706.html

猜你在找的MsSQL相关文章