簡體   English   中英

只有在使用列列表並且 IDENTITY_INSERT 為 ON 時,才能為表“Fee”中的標識列指定顯式值

[英]An explicit value for the identity column in table 'Fee' can only be specified when a column list is used and IDENTITY_INSERT is ON

我在 SQL 服務器中創建了這個表來存儲有關學生費用的數據:

create table Fee(
    feeID int primary key identity(1,1),
    classID int foreign key references Class(classID),
    nameID int foreign key references Students(studentsID),
    firstNameID int foreign key references Students(studentsID),
    dateOfAdmission varchar(50),
    monthKey int foreign key references Month(monthID), sum int
)

在 Visual Studio 中,我嘗試使用以下方法將數據保存在表中:

private void btn_fee_Save_Click(object sender, EventArgs e)
{
    int Class = Convert.ToInt32(cmbFeeClass.SelectedValue.ToString());
    int Name = Convert.ToInt32(cmbFeeName.SelectedValue.ToString());
    int firstName = Convert.ToInt32(cmbFeeFirstName.SelectedValue.ToString());
    int Month = Convert.ToInt32(cmbMonth.SelectedValue.ToString());
    try { 
    if (cmbFeeClass.Text != "" && cmbFeeName.Text != "" && cmbFeeFirstName.Text != "" && cmbMonth.Text != "" && txtSum.Text != "")
    {
        cmd = new SqlCommand("insert into Fee values('"+ Class + "', '" + Name + "', '" + firstName + "', " +
            "'" + dtDateOfAdmission.Text + "', '" + Month + "', '" + "', '" + txtSum.Text + "')", con.ConnectionOpening());

        cmd.ExecuteNonQuery();

        MessageBox.Show("The data has been saved", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        MessageBox.Show("Please fill in all the fields!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    con.CloseConnection();
}

如何更改代碼以便不再出現此錯誤:

只有在使用列列表並且 IDENTITY_INSERT 為 ON 時,才能為表“Fee”中的標識列指定顯式值

您收到錯誤是因為您沒有在INSERT語句中指定列。 如果沒有這些,SQL 將以先到先得的方式將值分配給列,並且在您的表定義中, feeID列是第一位的。 feeID列被定義為主鍵標識列,並且該列的值始終是自動生成的。

下面的代碼顯示了如何使用 Columns 部分構造 Sql 語句,並且還使用了參數。 這將幫助您防止將來出現重大錯誤。

using (SqlConnection con = new SqlConnection(<__ADD_YOUR_CONNECTION_STRING_HERE__>))
{
con.open()
using (SqlCommand cmd = con.CreateCommand())
{
    String sql = "INSERT INTO FEE ( classID,  nameID,  firstNameID,  dateOfAdmission,  monthKey)" +
                 "VALUES          (@classID, @nameID, @firstNameID, @dateOfAdmission, @monthKey)";

    cmd.CommandText = sql;
    cmd.CommandType = CommandType.Text;

    cmd.Parameters.Add(new SqlParameter("classID", SqlDbType.Int) { Value = Class });
    cmd.Parameters.Add(new SqlParameter("nameID", SqlDbType.Int) { Value = Name });
    cmd.Parameters.Add(new SqlParameter("firstnameID", SqlDbType.Int) { Value = firstName });
    cmd.Parameters.Add(new SqlParameter("dateOfAdmission", SqlDbType.VarChar) { Value = dtDateOfAdmission.Text });
    cmd.Parameters.Add(new SqlParameter("monthKey", SqlDbType.Int) { Value = Month });

    command.ExecuteNonQuery();
}
}

筆記:

您應該認真考慮將dateOfAdmission列的類型從VARCHAR(50)更改為DATE - 這會將日期值存儲為正確的日期,而不是字符串。 這也將使您將來的工作更加輕松。

建議using (...)的代碼,以確保在不需要時釋放數據庫資源。

暫無
暫無

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

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