"Column name or number of supplied values does not match table definition" 尝试将数据上传到我的数据库时

"Column name or number of supplied values does not match table definition" when trying to upload data to my DB

这是查询的 C# 代码(试图制作它以便我可以将图像上传到我的数据库 table):

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>

这是我的 table 代码:

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)
);

当我尝试更新(上传图像的二进制代码)数据库时,出现此错误:

Column name or number of supplied values does not match table definition.

并且源错误是 bold 中的行:

conn.Open();
**int n = comm.ExecuteNonQuery();**
conn.Close();

怎么了?

首先,当您的命令是 insert 时,您说您正在尝试 update。假设您的意思是插入,您的模式中有 2 列不是 NULL,因此必须传递这些列的值。您目前只传递 'image' ,它是 'ImageData' 并且可以为 NULL 因此您还需要为 'Image' 和 'Description'.

传递数据

如果您确实希望 INSERT 数据,那么您需要将实际值传递给在 table 定义中标记为 NOT NULL 的所有列——除了像 [= 这样的自动生成的列32=].

因此您的插入查询应如下所示:

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);



或更新
如果您想更新现有值,您将需要一个标识符来定位您希望 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