【问题标题】:C# Auto increment primary keyC# 自动递增主键
【发布时间】:2013-09-28 03:47:29
【问题描述】:

我已经将数据库中的IDENTITY 设置为yes,并且每当我运行应用程序时,键不会自动递增,给出关于不允许空值的错误

private void button8_Click(object sender, EventArgs e)
        {

            string fname = textBox1.Text;
            string lname = textBox2.Text;
            //int idnum = Convert.To(textBox3.Text);
            int mobnum = Convert.ToInt32(maskedTextBox1.Text);
            string email = textBox4.Text;

            int EdInst = comboBox1.SelectedIndex+1;
            string EdLev = comboBox3.SelectedText;
            string EdName = comboBox2.SelectedText;
            Boolean Valid = true;
            Valid = Validation(Valid);

            if (Valid == true)
            {
                IS2Team1_TriplexDBDataSet.ApplicantRow NewApplicantRow = iS2Team1_TriplexDBDataSet1.Applicant.NewApplicantRow();

                IS2Team1_TriplexDBDataSet.ApplicationRow NewApplicationRow = iS2Team1_TriplexDBDataSet1.Application.NewApplicationRow();

                //NewApplicantRow.Applicant_ID = ??;  // What do i do here for the ID to auto increment?
                NewApplicantRow.First_Name = fname;
                NewApplicantRow.Last_Name = lname;
                //NewApplicantRow.ID_Number =Convert.ToInt32(textBox3.Text);
                NewApplicantRow.Contact_Number = mobnum;
                NewApplicantRow.Email_Address = email;
                NewApplicantRow.University_ID = EdInst;

              //NewApplicationRow.Application_ID = ??; // What do i do here for the ID to auto increment?
                NewApplicationRow.Application_Status = "Recieved";
                NewApplicationRow.Application_Date = DateTime.Today;



                iS2Team1_TriplexDBDataSet1.Applicant.Rows.Add(NewApplicantRow);
                iS2Team1_TriplexDBDataSet1.Application.Rows.Add(NewApplicationRow);

                MessageBox.Show("Application Submitted", "Application Submitted");

                this.applicantTableAdapter.Update(this.iS2Team1_TriplexDBDataSet1);


                //Hide();
                //Form4 frmUpdate = new Form4();
                //frmUpdate.Show();

            }
                else if (Valid == false)
            {
                    MessageBox.Show("Required Information Missing");
                    textBox1.Focus();
            }

【问题讨论】:

  • 你的数据库结构是什么样的?
  • 看看你的适配器InsertCommand

标签: c# sql-server auto-increment


【解决方案1】:

您必须在表定义中设置以下值:

示例:不适用于实体框架

如果您想插入一个新行,只需忽略您的 Identity-Row 和 获取自动生成的身份:@@Identity

希望对您有所帮助!

这是一个未经测试的示例:

public static void Test()
    {
        int cIdentity = 0;

        SqlConnection cConnection = new SqlConnection("...");
        cConnection.Open();

        SqlCommand cCommand = new SqlCommand("Insert INTO TestTable (Name) VALUES ('Test'); "+
            "SELECT @@IDENTITY AS Ident", cConnection);

        IDataReader cReader = null;

        try
        {
            cReader = cCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (cReader.Read())
            {
                cIdentity = cReader.GetInt32(0);
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
            if (cReader != null)
            {
                cReader.Close();
            }
        }
        finally
        {
            if (cConnection != null)
            {
                cConnection.Close();
            }
        }
    }

【讨论】:

  • 我究竟如何使用@@Identity 获得自动生成的身份?你能给我一个如何做的例子吗?谢谢:)
  • 示例现在在我的帖子中 :)
【解决方案2】:

尝试在您的项目中打开 EDMX,单击该列并查看属性 (F4)。将 StoreGeneratedPattern 设置为 Identity。

【讨论】:

  • StoreGeneratedPattern 已设置为 Identity。我以为我不必在该列中插入一个值,但它不断出现错误申请人 ID 不能为空
  • @user2806570 身份本身应在数据库中设置。试试看。
  • 我已经在数据库中设置了。 (是身份) - 是,身份增量 - 1 ,身份种子 - 1
  • @user2806570 你能发布错误消息和内部异常消息吗?
【解决方案3】:

我们可以看到你的视图是自动递增的吗?它应该设置为 +1 或标识 (1,1)

【讨论】:

  • @HamletHakobyan,更新时不应编辑 ID,但仍取决于场景,但自动增量仅适用于插入新详细信息而不更新。除非您在更新时更改了 ID 本身。
猜你喜欢
  • 1970-01-01
  • 2017-02-10
  • 2018-11-23
  • 2019-08-07
  • 1970-01-01
  • 2015-05-11
  • 2012-12-08
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多