首页 > 解决方案 > 如何解决操作数类型冲突:varbinary(max) 与文本不兼容

问题描述

我正在尝试将文档插入数据库中的表中,但我继续收到此错误:

操作数类型冲突:varbinary (max) 与文本不兼容

任何方向都会非常有帮助。

Document 表有很多列,我只选择了这两个,认为我不需要所有 50 列,其中大多数为空;两列都设置为 (text, null),因为大多数列都是 (text, null)。

我希望我不需要对数据库进行任何更改,并且有一种方法可以更改代码,以便文件可以正确填充到 Documents 表中。

这是我的HttpPost:

    [HttpPost]
    public void UploadFiles()
    {
        if (!(Request.Files?.Count > 0)) return;
        var filesCount = Request.Files.Count;
        try
        {
            for (int i = 0; i < filesCount; i++)
            {
                var file = Request.Files[i];
                var fileName = Path.GetFileName(file?.FileName);
                if (fileName != null)
                {

                    var fileBytes = new byte[file.InputStream.Length];
                    file.InputStream.Read(fileBytes, 0, fileBytes.Length);
                    file.InputStream.Close();

                    var cmdStr = "insert into [dbo].[Documents] 
                    (DocumentName, DocumentLink) values(@val,@filename)";
                    using (var connection = new SqlConnection(con))
                    {
                        connection.Open();
                        var cmd = new SqlCommand(cmdStr, connection);
                        cmd.Parameters.AddWithValue("@val", fileBytes);
                        cmd.Parameters.AddWithValue("@filename", fileName);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

标签: c#sql-servermodel-view-controller

解决方案


推荐阅读