簡體   English   中英

C#自動遞增主鍵

[英]C# Auto increment primary key

我已經在數據庫中將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();
            }

我們可以看到您正在自動遞增的視圖嗎? 應將其設置為+1或標識(1,1)

嘗試在項目中打開EDMX,單擊該列並查看屬性(F4)。 將StoreGeneratedPattern設置為Identity。

您必須在表定義中設置以下值:

圖片

樣本:不適用於實體框架

如果要插入新行,只需忽略您的Identity-Row,並使用以下方法獲取自動生成的Identity: @@ 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();
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM