如何使用过程保存byte[]?

发布于 2024-09-24 22:49:09 字数 1699 浏览 5 评论 0原文

这个存储过程不保存数据,看来是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二智少女 2024-10-01 22:49:10

我发现这是不可能的 此答案 显示

您好,gaurav,目前是我们的
GetSqlQuery方法无法操作
正确使用类型参数
LongVarBinary 或 VarBinary,因此
使得无法存储
程序按预期工作。我们是
意识到这个问题,我们正在
正在努力修复它。作为解决方法
你应该尝试使用 Linq 来实现
你的目标。问候,佩塔尔
Telerik 团队

i found that it is impossible as this answer shows

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

相思故 2024-10-01 22:49:10

根据此表,似乎BLOBBINARYVARBINARY 将是原始类型的 [] 的有效类型。

您可以尝试在他们的论坛上提问,也许有人能够帮助你。

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.

我是男神闪亮亮 2024-10-01 22:49:10

尝试使用.WRITE方法。在 INSERT 上,插入 0x 作为图片,然后独立更新。

UPDATE QT_Community 
    SET Picture.Write (@Picture, 0, DATALENGTH(Picture))
    WHERE ID = @ID

Try using the .WRITE method. On your INSERT, insert 0x for Picture, then update independently.

UPDATE QT_Community 
    SET Picture.Write (@Picture, 0, DATALENGTH(Picture))
    WHERE ID = @ID
执手闯天涯 2024-10-01 22:49:10

示例(Ado.Net):

byte[] ba = UlongsToBytes(ul);
try
{
string source = @"packet size=4096;integrated security=SSPI;data source=MyPC\MyNamedInstance;persist security info=False;initial catalog=Sandbox";
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand a = new SqlCommand("INSERT BigintsTarget(bi) SELECT * FROM dbo.ParseImageIntoBIGINTs(@BIGINTs)", conn);
a.CommandType = System.Data.CommandType.Text;
a.Parameters.Add(new SqlParameter("@BIGINTs", System.Data.SqlDbType.Image,2147483647));
for(int q=0; q<10; q++)
{
a.Parameters[0].Value = ba;
int res = a.ExecuteNonQuery();
}
d2 = DateTime.Now;
SqlCommand b = new SqlCommand("INSERT BigintsTarget1(bi) SELECT * FROM dbo.ParseVarcharMAXIntoBIGINTs(@BIGINTs)", conn);
b.CommandType = System.Data.CommandType.Text;
b.Parameters.Add(new SqlParameter("@BIGINTs", System.Data.SqlDbType.VarChar,2147483647));
for(int q=0; q<10; q++)
{
b.Parameters[0].Value = sss;
int res = b.ExecuteNonQuery();
}
//b.ExecuteNonQuery();
conn.Close();
}
catch(Exception ex)
{
string s = ex.Message;
int t=0;
t++;
}

} 

Example (Ado.Net):

byte[] ba = UlongsToBytes(ul);
try
{
string source = @"packet size=4096;integrated security=SSPI;data source=MyPC\MyNamedInstance;persist security info=False;initial catalog=Sandbox";
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand a = new SqlCommand("INSERT BigintsTarget(bi) SELECT * FROM dbo.ParseImageIntoBIGINTs(@BIGINTs)", conn);
a.CommandType = System.Data.CommandType.Text;
a.Parameters.Add(new SqlParameter("@BIGINTs", System.Data.SqlDbType.Image,2147483647));
for(int q=0; q<10; q++)
{
a.Parameters[0].Value = ba;
int res = a.ExecuteNonQuery();
}
d2 = DateTime.Now;
SqlCommand b = new SqlCommand("INSERT BigintsTarget1(bi) SELECT * FROM dbo.ParseVarcharMAXIntoBIGINTs(@BIGINTs)", conn);
b.CommandType = System.Data.CommandType.Text;
b.Parameters.Add(new SqlParameter("@BIGINTs", System.Data.SqlDbType.VarChar,2147483647));
for(int q=0; q<10; q++)
{
b.Parameters[0].Value = sss;
int res = b.ExecuteNonQuery();
}
//b.ExecuteNonQuery();
conn.Close();
}
catch(Exception ex)
{
string s = ex.Message;
int t=0;
t++;
}

} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文