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

时间:2011-09-06 19:21:55

标签: c# tsql

我从以下代码中收到以下错误:

  

不允许从数据类型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,插入表格。

我需要更改哪种数据类型?

1 个答案:

答案 0 :(得分:0)

好吧,您将该图像作为AnsiString数据类型传递,这是发生问题的地方。

我认为你需要DbType.Binary。

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