Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “等于”运算符中的数据类型image和varbinary(max)不兼容_C#_Sql Server_Linq_Asp.net Mvc 5 - Fatal编程技术网

C# “等于”运算符中的数据类型image和varbinary(max)不兼容

C# “等于”运算符中的数据类型image和varbinary(max)不兼容,c#,sql-server,linq,asp.net-mvc-5,C#,Sql Server,Linq,Asp.net Mvc 5,作为我的asp.net mvc项目之一,我正在尝试将映像保存到SQL Server数据库表: 在我的C#控制器中,我上传了一张图片并将其转换为byte[]数组,以便将其保存到数据库中。我的代码如下所示 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "CustIDNO, EDate,LetterNo,LetterFrom,LetterType,Comments

作为我的asp.net mvc项目之一,我正在尝试将映像保存到SQL Server数据库表:

在我的C#控制器中,我上传了一张图片并将其转换为byte[]数组,以便将其保存到数据库中。我的代码如下所示

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "CustIDNO, EDate,LetterNo,LetterFrom,LetterType,Comments, ValueTime")] WarningPic warningPic, HttpPostedFileBase img)
    {
        byte[] fileData = null;

        if (img != null)
        {
            //Here i am converting image to byte array
            using (var binaryReader = new BinaryReader(img.InputStream))
            {
                fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
            }

        }

        if (ModelState.IsValid)
        {
            warningPic.FileName = warningPic.CustIDNO.ToString();
            warningPic.Picture = fileData;
            warningPic.SlNo = 9;

            db.WarningPics.Add(warningPic);

            db.SaveChanges();


            return RedirectToAction("Index");
        }

        return View(warningPic);
    }
一切都会好起来的,但我得到了一个神秘的错误,比如:


描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

尽管名称不同,
image
并不是这里要使用的合适数据类型。除此之外:“在SQL Server的未来版本中,将删除ntext、text和image数据类型。避免在新的开发工作中使用这些数据类型”。那么应该使用什么数据类型在数据库表上存储图像?我将数据类型从image更改为varbinary(max),问题就解决了。多亏了你。实际上,我使用的是一个已有的旧数据库和已有的旧数据。因此,我必须三思而后行才能更改数据类型。@mAnup,旧的
image
类型可以存储与
varbinary(MAX)
相同的二进制值,但请注意,使用
WRITETEXT
UPDATETEXT
的现有代码,或者
READTEXT
将不能与
varbinary(MAX)
一起使用。
The data types image and varbinary(max) are incompatible in the equal to operator.