【问题标题】:Can not Insert the NULL value into Auto Increment PrimaryKey Column无法将 NULL 值插入 Auto Increment PrimaryKey 列
【发布时间】:2014-12-13 16:45:01
【问题描述】:

我在 C# 数据库中创建了表Items,因为主键的属性是 AutoIncrement=True, AutoIncrementStep=1, Unique=True, AutoIncrementSeeds=0;

CREATE TABLE [dbo].[Items] 
(
    [item_id]       INT          NOT NULL,
    [item_name]     VARCHAR (50) NULL,
    [item_model]    VARCHAR (50) NULL,
    [item_price]    VARCHAR (50) NULL,
    [item_quantity] VARCHAR (50) NULL,
    [entry_date]    VARCHAR (50) NULL,
    [user_id]       VARCHAR (50) NULL,

    PRIMARY KEY CLUSTERED ([item_id] ASC)
);

我正在使用以下代码将数据插入Items 表:

string query = @"Insert into Items(item_name, item_model, item_price, item_quantity, entry_date, user_id)" +
                "VALUES(@name, @model, @price, @quantity, @date, @user)";

using (SqlConnection c = new SqlConnection(Properties.Settings.Default.Database1ConnectionString))
{
    try
    {
        c.Open();

        if (c.State == ConnectionState.Open)
        {
            SqlCommand command1 = new SqlCommand(query, c);

            command1.Parameters.AddWithValue("@name", name.Text.ToString());
            command1.Parameters.AddWithValue("@model", model.Text.ToString());
            command1.Parameters.AddWithValue("@price", price.Text.ToString());
            command1.Parameters.AddWithValue("@quantity", quantity.Text.ToString());
            command1.Parameters.AddWithValue("@date", dateTimePicker1.Text.ToString());
            command1.Parameters.AddWithValue("@user", added_by.Text.ToString());

            int k = command1.ExecuteNonQuery();

            if (k > 0)
            {
                MessageBox.Show("Data Added Successfully");
                c.Close();
                return;
            }
            else 
            {
                MessageBox.Show("Data was not added successfully - try again later");
                c.Close();
                return;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed To Open Connection :." + ex);
        return;
    }
}

当我运行代码并尝试插入数据时出现错误

无法将 NULL 值插入到列“item_id”表中

【问题讨论】:

  • @Dan Guzman - SQL Server MVP 我希望你能提供帮助:)
  • 我很想知道您是如何确定 AutoIncrement 为真的。 (顺便说一下,在SqlServer中叫做IDENTITY)

标签: c# sql sql-server primary-key


【解决方案1】:

NOT NULL 表示您无法将NULL 插入需要显式插入的列中。

设置IDENTITY(1,1) 会自动插入item_id,种子为1。

像这样改变你的定义

CREATE TABLE [dbo].[Items] (
[item_id]       INT          NOT NULL IDENTITY(1, 1),
[item_name]     VARCHAR (50) NULL,
[item_model]    VARCHAR (50) NULL,
[item_price]    VARCHAR (50) NULL,
[item_quantity] VARCHAR (50) NULL,
[entry_date]    VARCHAR (50) NULL,
[user_id]       VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([item_id] ASC)
);

将其显式插入数据库

string query = @"Insert into Items(item_id,item_name,item_model,item_price,item_quantity,entry_date,user_id)" +
                "VALUES(@item_id,@name, @model, @price, @quantity, @date,@user)";

停止使用.AddWithValues Source

【讨论】:

  • @YounasBangash 欢迎您
  • 还有一件事是成功添加数据,但是当我关闭 C# 应用程序而不是我保存在数据库中的所有数据时,它消失意味着数据库为空,我还通过右键单击表检查数据,然后点击显示表为空的数据
  • @Ganesh_Devlekar 你能解释一下为什么你说停止使用.AddWithValue吗?谢谢。
  • @andrew.cuthbert 答案中添加的来源
猜你喜欢
  • 1970-01-01
  • 2013-02-27
  • 2021-10-31
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多