不允许从数据类型nvarchar隐式转换为varbinary(max)

本文关键字:varbinary max 转换 数据类型 nvarchar 不允许 | 更新日期: 2023-09-27 18:07:38

我从下面的代码中得到以下错误:

不允许从数据类型nvarchar隐式转换为varbinary(max)。使用CONVERT函数运行此查询。

 protected void btnOKImageUpload_Click(object sender, EventArgs e)
 {
     try
    {
        string filePath = "";
        string fileName = "";
        int UserId = Convert.ToInt32(hdnUserId.Value);
        if (fileImage.HasFile)
        {
            if (CheckFileType(fileImage.FileName))
            {
                filePath = Server.MapPath(Application["UploadFolder"].ToString());
                if (UserId > -1)
                {
                    fileName = "Image_" + UserId.ToString() + Path.GetExtension(fileImage.FileName);
                }
                else
                {
                    fileName = Path.GetFileName(fileImage.FileName);
                }
                string virFileName = Application["UploadFolder"].ToString() + "/" + fileName;
                string tmpFileName = Path.Combine(filePath, fileName);
                fileImage.SaveAs(tmpFileName);
                SessionData.LocationFloorPlanFile = tmpFileName;
                DataAccess.SaveEmployeeImage(UserId, fileName);
                hdnImageFileName.Value = fileName;
                txtImageUpload.Text = virFileName;
                //btnFloorPlanView.HRef = hdnFloorPlan.Value;
                btnImageUpload.Disabled = true;
                btnImageDelete.Enabled = true;
                hdnPostbackAction.Value = "UPLOAD";
            }
        }
   }
     catch (Exception ex)
     {
         hdnErrMsg.Value = ex.Message;
         //"An error has occurred while processing your request. Please contact support for further assistance.";
     }  
}                                                                 
public static void SaveEmployeeImage(int userId, string imageFilePath)
{
    ArrayList paramaters = getParamArray();
    paramaters.Add(getParam("@userId", DbType.Int32, userId));
    paramaters.Add(getParam("@imageFilePath", DbType.AnsiString, imageFilePath));
    executeNonQuery("xp_SaveEmployeeImage", paramaters);
}

我的过程将userId和image插入到表中。

我需要更改什么数据类型?

不允许从数据类型nvarchar隐式转换为varbinary(max)

嗯,你正在传递图像作为一个AnsiString数据类型,这就是问题发生的地方。

我认为你需要DbType.Binary.

但是,你的参数名是imageFilePath,所以你应该给它一个路径到一个文件作为一个字符串?这可能意味着你的xp实际上是错误的。