首页 > 解决方案 > 尝试将数据上传到我的数据库时,“列名或提供的值的数量与表定义不匹配”

问题描述

这是查询的 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();

怎么了?

标签: c#sqlsql-serverserver-error

解决方案


如果您确实希望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

推荐阅读