【问题标题】:How to save byte[] using a procedure?如何使用程序保存 byte[]?
【发布时间】:2011-04-16 19:39:39
【问题描述】:

这个存储过程没有保存数据,好像是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 它应该可以正常工作。

【问题讨论】:

  • 定义“不起作用”。你有错误吗?插入/更新是否有效但图片丢失?插入/更新是否有效并且图片已损坏?顺便说一句,检查 COUNT(*) 然后插入或更新的代码在并发下被破坏。你应该使用MERGEtechnet.microsoft.com/en-us/library/bb510625.aspx
  • CommandParameter 在哪里?

标签: sql-server telerik telerik-open-access


【解决方案1】:

我发现这是不可能的,就像this answer 显示的那样

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

【讨论】:

    【解决方案2】:

    根据this table 看来BLOBBINARYVARBINARY 将是[] of primitive type 的有效类型。

    您可以尝试ask on their forums,也许有人可以帮助您。

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 如果我错了,我不介意投反对票。然而,我不喜欢没有任何解释的匿名投票。你能解释一下你的反对意见吗?
    • 这似乎与问题完全无关。这不是 SQLServer 的问题,而是 Telerik 的 ORM 的问题。他不能将byte[] 传递给程序。但是,他可以发送null,证明问题出在 ORM 上。
    • @BrunoLM:感谢您的反馈。我当然有可能误解了这个问题。这将由 OP 来确定。我认为我提供了一个合理的解决方法,至少可以尝试
    • 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.。如果方法不起作用,他应该如何通过@Picture
    • 他实际上在编辑之前描述了这种行为。 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.
    【解决方案4】:

    示例(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++;
    }
    
    } 
    

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2014-06-09
      • 2012-11-20
      • 1970-01-01
      相关资源
      最近更新 更多