【问题标题】:"Cannot insert explicit value for identity column in table 'tbl Fingerprint' when IDENTITY_INSERT is set to OFF“当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'tbl Fingerprint' 中为标识列插入显式值
【发布时间】:2014-01-31 14:37:53
【问题描述】:
[WebMethod]
        public bool AddStudent(Student student)
        {             bool UploadSuccess = false;
            cn.Open();
            int StudentID = 0;
            using (SqlCommand com = new SqlCommand("INSERT into tblStudent (StudentNumber, Name, Surname, DOB, Gender, EmailAddress, Address1, Address2, City, Postcode, Username, Password, Course) values ('" + student.StudentNumber + "' ,'" + student.Name + "' ,'" + student.Surname + "' ,'" + student.DOB + "', '" + student.Gender + "' ,'" + student.EmailAddress + "' ,'" + student.Address1 + "' ,'" + student.Address2 + "' ,'" + student.City + "' ,'" + student.Postcode + "' ,'" + student.Username + "' ,'" + student.Password + "' ,'" + student.Course + "')", cn))
            {
                int i = com.ExecuteNonQuery();
                StudentID = (int)com.ExecuteScalar();
                cn.Close();
                if (i != 0)

                    UploadSuccess = true;

                return UploadSuccess;
            }

我正在尝试将数据插入到具有四列的指纹表中 - ID(主键) - 链接到学生表的 StudentID(外键) - 描述 - 模板

但以下错误不断出现。我无法关闭 ID 的 IDENTITY,因为我希望它自动递增。我还有一个学生表来存储信息。我想要实现的是,在输入学生详细信息后,我想将之前生成的学生ID复制到指纹表 - 学生ID列。我的代码如下所示。

private void btnSave_Click(object sender, EventArgs e)
        {
            fgrTemplate template = new fgrTemplate();
            template.StudentID = std.StudentID;
            template.Description = fngDes.Text;
            template.Template = m_StoredTemplate;
            if (upload.InsertTemplate(template))
            {
                MessageBox.Show("Student Successfully Added!");
            }
            else
            {
                MessageBox.Show("Student Not Successfully Added!");
            }

using (SqlCommand com = new SqlCommand("INSERT INTO tblFingerprint (StudentID, Description, Template) values ('" + template.StudentID + "' ,'" + template.Description + "' ,@Template)", cn))

这就是我的网络服务上的内容。但是它给了我错误

【问题讨论】:

标签: c# sql sql-server sql-server-2008 asmx


【解决方案1】:

你的第一个错误:

您正在尝试插入StudentID,这似乎是一个IDENTITY 类型字段(自动增量),您不必在INSERT 语句中传递它,SQL 服务器将为您生成一个。

您的第二个问题是:您的查询未正确参数化,您正在使用字符串连接和参数的组合。你的查询和语句应该是这样的:

using (SqlCommand com = new SqlCommand(@"INSERT INTO tblFingerprint (Description, Template) 
                                         values (@Description, @Template)", cn))
{
    com.Parameters.AddWithValue("@Description", template.Descriptio);
    com.Parameters.AddWithValue("@Template", template.Value); //what ever value is
    //....rest of your code

}

如果您已经有StudentID 并且想要更新现有记录,请使用UPDATE 语句。

如果你想手动插入StudentID(覆盖自动增量ID)那么你必须使用SET IDENTITY_INSERT

【讨论】:

  • 我的指纹表中有四列 - ID(主键)、链接到学生表的学生 ID(外键)、描述和模板。输入学生信息后,我想将学生 ID 从 tblstudent 复制到 tblfingeprint。
【解决方案2】:

删除或评论此行

//template.StudentID = std.StudentID;

同时从 INSERT

中删除 StudentID
"INSERT INTO tblFingerprint (Description,....

【讨论】:

  • 如果这样做,如何复制链接指纹模板到学生的学生证。
  • 这似乎是身份字段,当您向表中插入新记录时,它会自动递增。如果你想在这个字段中插入你自己的值,他们从数据库结构中删除 IDENTITY 然后不需要更改你的代码它会起作用。
  • template.StudentID = std.StudentID 正在将信息从前一个表单传递到当前表单。它加载信息并将其保存到 std,以便我可以将 studentID 保存到 tblFingeprint 上。我已经有一个名为 ID 的 tblFingerprint 主键。 StudentID 是链接到 tblStudent 的外键。我只想复制那个 StudentID 以将模板链接到他们的信息
  • 只需仔细检查您的数据库表 tblFingerprint,确保身份为“否”。如果为“否”,则不应引发错误。如果是“是”,请将其更改为“否”并保存表格,然后使用您的代码它将起作用。
【解决方案3】:

您的代码可以使用SQL INJECTION 进行攻击

在这种情况下,请尝试使用sql command parameters

【讨论】:

    猜你喜欢
    • 2014-02-24
    • 2016-05-13
    • 2019-03-10
    • 2016-08-18
    • 2010-11-22
    • 2018-06-29
    • 2017-10-26
    相关资源
    最近更新 更多