用c#转换varchar和varbinary

时间:2013-05-15 18:33:46

标签: asp.net sql varchar varbinary

这是按下按钮时的c#代码:

                Int32 fileLen;

            fileLen = uplImage.PostedFile.ContentLength;


            Byte[] input = new Byte[fileLen];

            myStream = uplImage.FileContent;

            myStream.Read(input, 0, fileLen);

            DALBio bio = new DALBio();
            bio.PlayerID = Session["playerID"].ToString();
            bio.Pending = 'Y';
            bio.Photo = input;
            DALBio.insertImage(bio);

这是我调用存储过程的c#代码

public static bool insertImage(DALBio bio)
{
    string sql = string.Format("EXECUTE insertPlayerImage @playerID = '{0}', @profileImage = '{1}', @pending = '{2}'", bio.playerID, bio.Photo, bio.pending);

    SqlCommand insertPhoto = new SqlCommand(sql, baseballConnection);

    try
    {
        baseballConnection.Open();
        insertPhoto.ExecuteNonQuery();
        return true;
    }
    catch
    {
        return false;
    }
    finally
    {
        baseballConnection.Close();
    }
}

我收到以下错误:不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。

所以我试图将我的存储过程改为:

ALTER PROCEDURE insertPlayerImage @playerID varchar(9), @profileImage varchar(max), @pending char(1)
AS

CONVERT(varbinary(max), @profileImage)

INSERT INTO PlayerImage(
playerID,profileImage, pending)
VALUES(
@playerID, @profileImage, @pending)
GO

并且它不喜欢转换线。真的不知道该怎么做。

1 个答案:

答案 0 :(得分:-1)

试试这个: -

更改PROCEDURE insertPlayerImage(@playerID varchar(9),@ profileImage varchar(max),@pending char(1))

AS

SET @profileImage = CONVERT(varbinary(max),@ profileImage)