C#自动增加主键

时间:2013-09-23 09:35:54

标签: c# sql-server auto-increment

我已经将数据库中的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();
            }

3 个答案:

答案 0 :(得分:0)

我们能看到你的自动增量视图吗?它应该设置为+1或identity(1,1)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

Image

示例:不适用于实体框架

如果要插入新行,只需忽略您的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();
            }
        }
    }