如何使用过程保存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
不是 byte[]
吗?
此代码在 SQL Server Management Studio 上执行时有效。
DECLARE @X varbinary(20)
Set @X = CAST('Testing' As varbinary(20))
EXECUTE [QP_AddCommunity] 'aaaaa', 'descricao', @X, 216, NULL, 0;
但是,当使用 byte[]
上的内容从 GetSqlQuery
方法调用时,事务会显示它不活动且不脏。但如果 byte[]
为 null
,它就会按预期工作。
This stored procedure does not save the data, it seems to be a problem with the VARBINARY
. I am passing a byte[]
to it, but then it doesn't work. If I send this parameter as NULL
it works.
I'm calling the procedure with the following code:
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);
}
}
The procedure is the following:
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
What's wrong with this code? Isn't VARBINARY
a byte[]
?
This code works when executing on SQL Server Management Studio.
DECLARE @X varbinary(20)
Set @X = CAST('Testing' As varbinary(20))
EXECUTE [QP_AddCommunity] 'aaaaa', 'descricao', @X, 216, NULL, 0;
But when calling from the GetSqlQuery
method with something on the byte[]
the transaction says it's not active and not dirty. BUT if the byte[]
is null
it works as it should.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现这是不可能的 此答案 显示
i found that it is impossible as this answer shows
根据此表,似乎
BLOB
、BINARY
、VARBINARY
将是原始类型的[]
的有效类型。您可以尝试在他们的论坛上提问,也许有人能够帮助你。
Accordingly to this table it seems either
BLOB
,BINARY
,VARBINARY
would be valid types for[] of primitive type
.You could try to ask on their forums, maybe someone will be able to help you.
尝试使用.WRITE方法。在 INSERT 上,插入 0x 作为图片,然后独立更新。
Try using the .WRITE method. On your INSERT, insert 0x for Picture, then update independently.
示例(Ado.Net):
Example (Ado.Net):