c# - 尝试将数据上传到我的数据库时,“列名或提供的值的数量与表定义不匹配”
问题描述
这是查询的 C# 代码(尝试制作它以便我可以将图像上传到我的数据库表):
protected void ButtonInsert_Click(object sender, EventArgs e)
{
insert();
}
public void insert()
{
if (FileUpload1.PostedFile.FileName != "")
{
Byte[] image;
Stream s = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(s);
image = br.ReadBytes((Int32)s.Length);
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\kfirm\Desktop\KEY_web_v1\KEY_web_v1\App_Data\Database1.mdf;Integrated Security=True";
SqlConnection conn = new SqlConnection(connectionString);
//SqlConnection conn = new SqlConnection("data source=.\\sqlexpress; initial catalog=SlideShow; integrated security=true");//ORIGINAL - "data source=.\\sqlexpress; initial catalog=SlideShow; integrated security=true" //I think he's just making a dataset, I can edit later if it doesnt work or add this to the global.asax
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "INSERT into Portfolio values(@ImageData)"; //I changed stuff!
comm.Parameters.AddWithValue("@ImageData", image); //I changed stuff!
conn.Open();
int row = comm.ExecuteNonQuery();
conn.Close();
if (row > 0)
{
LabelError.Text = "success";
}
}
else LabelError.Text = "please upload an image";
}
这是 HTML 代码:
<form name="AddSiteToPortfolio" action="AddSiteToPortfolio.aspx" method="post" runat="server">
<table>
<tr>
<td>ImageData: </td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="ButtonInsert" runat="server" Text="Upload Image"
onclick="ButtonInsert_Click" />
<asp:Label ID="LabelError" runat="server" Text=""></asp:Label>
</tr>
</table>
</form>
这是我的表格代码:
CREATE TABLE [dbo].[Portfolio] (
[Image] NVARCHAR (50) NOT NULL,
[Description] NVARCHAR (50) NOT NULL,
[ImageData] VARBINARY (max) NULL,
[id] INT IDENTITY (1, 1) NOT NULL,
PRIMARY KEY CLUSTERED ([id] ASC)
);
当我尝试更新(上传图像的二进制代码)数据库时,我收到此错误:
列名或提供的值的数量与表定义不匹配。
源错误是粗体行:
conn.Open();
**int n = comm.ExecuteNonQuery();**
conn.Close();
怎么了?
解决方案
如果您确实希望INSERT
数据,那么您需要将实际值传递给NOT NULL
表定义中标记的所有列 - 除了自动生成的列,如“ID”。
因此,您的插入查询应如下所示:
comm.CommandText = "INSERT INTO Portfolio (Image, Description, ImageData) VALUES(@Image, @Description, @ImageData)";
和参数:
comm.Parameters.AddWithValue("@Image", “myImage”);
comm.Parameters.AddWithValue("@Description", “WhateverYouNeed”);
comm.Parameters.AddWithValue("@ImageData", image);
OR FOR UPDATE
如果你想更新一个现有的值,你需要一个标识符来定位你想要的行和字段UPDATE
。在这种情况下,您需要要更新的图像的“ID”。
comm.CommandText = “UPDATE [Portfolio] SET [ImageData] = @Image WHERE [ID] = @ID”;
和参数:
comm.Parameters.AddWithValue("@Image", “myImage”);
comm.Parameters.AddWithValue(“@ID”, 123); // or whatever the ID
推荐阅读
- php - Woocommerce - 如何有条件地更改可变产品页面上的按钮文本?
- python - 在 Python 中并排绘制线性图和对数图。类似于 R 中的 mfrow=c(2,1)
- python - 无法在pygame中加载图像
- python - 删除不是一周开始或结束的所有行
- javascript - 使用 node.js 将行插入 MySQL 数据库时出现 ER_PARSE_ERROR
- ios - UITableViewCell 中单个 UIImageView 应该有哪些约束?
- flutter - Instagram API 弃用
- spring - Spring WebFlux WebClient 构建器设置请求正文
- sql - Oracle:列出特定日期范围内具有 column_value = '%value%' 的数据库中的所有表?
- docker - 如何使容器根文件系统 r/o 但允许写入 WORKDIR