c# - 如何解决操作数类型冲突: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;
}
}
解决方案
推荐阅读
- json - 追加到 jq 中嵌套元素的数组但返回根对象
- sql - 多少行有不同的值
- flutter - Flutter:> 无法通知项目评估侦听器。org.gradle.api.file.ProjectLayout.directoryPropertyLorg/gradle/api/file/DirectoryProperty;
- c++ - Qt 读取样式表中的动态 qproperties
- css - 使用未知数量的项目重复网格布局
- android - 更新 MPAndroidChart 中的 XAxis 值
- elasticsearch - 如何访问弹性搜索脚本中的数组项
- javascript - 如何在浏览器应用程序的后端读取文件?
- python - 为缺少数据的行创建标志
- python - 抓取可点击的链接或 xpath